Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Retrieve output from telnet command
#1
Hey, everyone. I'm a networking student who has recently began to dip his toe into learning python. I'm having fun learning it but i'm definitely a beginner and have found myself unable to solve a problem and was hoping some of you coding wizards can help point me in the right direction.
Essentially, I am trying to write a script that will be able to poll an interface for its IP address, and save that output in some sort of variable(?) that I can then manipulate. I'm configuring subinterfaces, so I'd like to be able to poll an interface so if the input came back as "192.168.5.1" I could parse out that information "192" "168" and "1" and just change the 3rd octet. So depending on whatever interface I configured both subinterfaces followed a similar format.

I've been using Regex to parse out information in my testing and it seems to work fine:

import re


phrase= "Internet address is 192.168.5.1/31"

patterns= [r'\d+']


for p in patterns:
    match = re.findall(p, phrase)
    print("ip add " + str(match[0] + "." + str(match[1]) + ".30." + str(match[3])))
However, that requires that I manually type in the phrase.
I can get the input I am looking for when deploying a script like:

import getpass
import sys
import telnetlib


user = raw_input("Please enter your Username: ")
password = getpass.getpass()

for z in range (0, 11, 2):


		HOST = "192.168.1." + str(z)

		tn = telnetlib.Telnet(HOST)

		tn.read_until("Username: ")
		tn.write(user + "\n")
		if password:
			tn.read_until("Password: ")
			tn.write(password + "\n")

		tn.write("conf t\n")
		tn.write("do show ip interface f0/0\n")


		tn.write("end\n")
		tn.write("exit\n")

		print tn.read_all()
However, I'd like to to able to save the output of the "do show ip interface f0/0" command which will output something like:
Internet address is 192.168.5.1/31

So I can then parse numbers out and manipulate them.
I hope I'm made some type of sense, since this certainly isn't my strong point.
Any help at all would be greatly appreciated!
Quote
#2
you can use something like this to find only the ip address from the output.

re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})/\d+',showint)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  3.6 telnet eyler 3 3,921 Jun-28-2019, 05:22 AM
Last Post: Khanhamid90
  Any suggestion on python library to use for both ssh and telnet? lord_mani 4 415 Jun-25-2019, 04:07 PM
Last Post: gb74razor
  telnet question jacklee26 2 354 Mar-30-2019, 06:45 AM
Last Post: jacklee26
  GNS3 telnet from bash terminal to virtual cisco router marienbad 2 414 Feb-24-2019, 07:55 AM
Last Post: marienbad
  Telnet to switch from jump start server(linux) searching1 0 403 Jan-09-2019, 10:03 PM
Last Post: searching1
  Issue when running telnet program using python3 searching1 11 1,826 Dec-22-2018, 12:48 AM
Last Post: searching1
  Aggregate multiple telnet connections Jibeji 1 1,450 Mar-02-2018, 07:21 PM
Last Post: mpd
  mysql connector/telnet issue (re: text game) rebubula76 1 650 Feb-06-2018, 08:00 PM
Last Post: rebubula76
  Multithread telnet not working Parallel anna 7 1,959 Feb-05-2018, 01:17 PM
Last Post: anna
  telnetlib.Telnet timeout issue.. taking much time anna 1 2,672 Jan-19-2018, 01:52 PM
Last Post: devs4p

Forum Jump:


Users browsing this thread: 1 Guest(s)