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

Possibly Related Threads...
Thread Author Replies Views Last Post
  telnet from ssh tunnel oldfart 1 137 Aug-10-2019, 06:44 PM
Last Post: DeaD_EyE
  3.6 telnet eyler 3 4,375 Jun-28-2019, 05:22 AM
Last Post: Khanhamid90
  Any suggestion on python library to use for both ssh and telnet? lord_mani 4 492 Jun-25-2019, 04:07 PM
Last Post: gb74razor
  telnet question jacklee26 2 410 Mar-30-2019, 06:45 AM
Last Post: jacklee26
  GNS3 telnet from bash terminal to virtual cisco router marienbad 2 476 Feb-24-2019, 07:55 AM
Last Post: marienbad
  Telnet to switch from jump start server(linux) searching1 0 457 Jan-09-2019, 10:03 PM
Last Post: searching1
  Issue when running telnet program using python3 searching1 11 2,113 Dec-22-2018, 12:48 AM
Last Post: searching1
  Aggregate multiple telnet connections Jibeji 1 1,543 Mar-02-2018, 07:21 PM
Last Post: mpd
  mysql connector/telnet issue (re: text game) rebubula76 1 677 Feb-06-2018, 08:00 PM
Last Post: rebubula76
  Multithread telnet not working Parallel anna 7 2,068 Feb-05-2018, 01:17 PM
Last Post: anna

Forum Jump:

Users browsing this thread: 1 Guest(s)