Retrieve output from telnet command - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Retrieve output from telnet command (/thread-16405.html) |
Retrieve output from telnet command - Networker - Feb-26-2019 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! RE: Retrieve output from telnet command - searching1 - Mar-12-2019 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) |