Python Forum
Retrieve output from telnet command
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!
Reply
#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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  telnet from ssh tunnel oldfart 3 7,138 Jul-17-2020, 02:28 PM
Last Post: noobami99
  telnet to a device under tacacs management kang18 0 1,559 Jun-05-2020, 06:11 AM
Last Post: kang18
  3.6 telnet eyler 3 11,254 Jun-28-2019, 05:22 AM
Last Post: Khanhamid90
  Any suggestion on python library to use for both ssh and telnet? lord_mani 4 3,715 Jun-25-2019, 04:07 PM
Last Post: gb74razor
  telnet question jacklee26 2 2,482 Mar-30-2019, 06:45 AM
Last Post: jacklee26
  Aggregate multiple telnet connections Jibeji 1 4,250 Mar-02-2018, 07:21 PM
Last Post: mpd
  Multithread telnet not working Parallel anna 7 7,438 Feb-05-2018, 01:17 PM
Last Post: anna
  Python telnet script for IP list mangesh 1 58,724 Jun-26-2017, 11:12 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020