LinuxBill
LinuxBill

Reputation: 415

Telnet to router in Python Script

import getpass
import sys
import telnetlib

tn = telnetlib.Telnet("xxxxxxxx")
tn.write("xxxxxx" + "\n")
tn.write("xxxxxx" + "\n")

tn.write("show version\n")
tn.write("exit\n")

print tn.read_all()

Trying to telnet to a router and the script is hanging.... Not sure whats up. Have tried a debug but cant make head or toe of the output.

telnetlib.py(511):         if self.irawq >= len(self.rawq):
telnetlib.py(512):             self.rawq = ''
telnetlib.py(513):             self.irawq = 0
telnetlib.py(516):         buf = self.sock.recv(50)

This is where it hangs in the debug.

Thanks William

Upvotes: 0

Views: 4064

Answers (1)

LinuxBill
LinuxBill

Reputation: 415

I can answer this one. Managed to get it fixed.

The python script was going off the rails when the router asked for user input to see more information on the command.

So when i asked for a "show version" it would give out a lot of information onto the command line and then ask you hit space to see more.

To solve this the first command i execute is "term len 0". Stops it asking for any user input and just fires it all out at once.

below is the final script. Changed quite a bit but its works.

import sys
import telnetlib

tn = telnetlib.Telnet("xxxxxxxx")

TELNET_PROMPT="xxxxxxxxxx"
TIMEOUT=1
tn.write("xxxxxxx"+"\n")
tn.write("xxxxxxx"+"\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("term len 0" + "\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("show version" + "\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("exit"+"\n")
tn.close()

Upvotes: 1

Related Questions