Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Retrieve output from telnet command
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 "" 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"

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")


		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

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!
you can use something like this to find only the ip address from the output.


Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)