3

I am trying to read from a cisco router using telnetlib

import telnetlib
tn = telnetlib.Telnet(’10.106.218.50’, 17280)
cmd1=”enable”
cmd2=”show run”
#session.write("command".encode('ascii') + b"\r")
tn.write(cmd1.encode('ascii') + b"\r")
tn.write(cmd2.encode('ascii') + b"\r")
#op=tn.read_very_eager()
#op=tn.read_some()
#op=tn.read_until('#')
op=tn.read_all()
print op

I am able to write to the console of the router successfully However the system just hangs,when i try to read from the console of the router. When i use read_some(), i get a part of the output.But read_all() just hangs and gives no response Please suggest a solution

fsociety
  • 977
  • 3
  • 12
  • 23

3 Answers3

3

the

read_all()

command in python's telnetlib module will block if there's no timeout specified when you make your connection.

your invocation command should look like

tn = telnetlib.Telnet('10.106.218.50', 17280, timeout = 1)

you can also substitute your own timeout value.

Jiynx
  • 306
  • 1
  • 6
3

Solution for me was using tn.red_until(). This code works for me. And If you don't need to send the output to text file, then modify:

data = data + tn.read_until(b"FIN\n", timeout = TIMEOUT).decode('ascii')

by:

tn.read_until(b"FIN\n", timeout = TIMEOUT)

Here is my code:

import sys
import telnetlib
import time

username = "admin"
password = "admin"
command = "show version"
TIMEOUT = 3

router_list = open("hostlist.txt")
for line in router_list:
    tn = telnetlib.Telnet(line.rstrip())
    tn.set_debuglevel(1)
    time.sleep(2)
    data = data + tn.read_until(b"Username: ").decode('ascii')
    tn.write(username.encode('ascii') + b"\n")
    time.sleep(2)
    data = data + tn.read_until(b"Password: ").decode('ascii')
    tn.write(password.encode('ascii') + b"\n")
    time.sleep(2)
    tn.write(command.encode('ascii') + b"\n")
    time.sleep(2)
    tn.write(b"\n")
    time.sleep(2)
    tn.write(b"echo FIN\n")
    time.sleep(2)
    data = data + tn.read_until(b"FIN\n", timeout = TIMEOUT).decode('ascii')
    print("Imprimiendo:" + data)
    op=open ("output.txt", "a").write(data)
    tn.close()
juanpb12
  • 125
  • 1
  • 9
0

I have met the same issue.

import socket, telnetlib

def telnet(ip_address,user_name,password):

    tn = telnetlib.Telnet(ip_address,23,1)
    # tn.set_debuglevel(2)

    tn.write(user_name + '\n')
    tn.write(password + '\n')
    tn.write('show version\n')
    tn.write('show power\n')

    print tn.read_all()
    tn.close()

telnet('10.236.0.19','who','who')

Error log was the same here:

Traceback (most recent call last):
  File "telnet.py", line 41, in <module>
    telnet('10.236.0.19','who','who')
  File "telnet_tn.py", line 23, in telnet
    print tn.read_all()
  File "C:\Python27\lib\telnetlib.py", line 385, in read_all
    self.fill_rawq()
  File "C:\Python27\lib\telnetlib.py", line 576, in fill_rawq
    buf = self.sock.recv(50)
socket.timeout: timed out

Then I tried to modify the function read_all() in C:\Python27\lib\telnetlib.py. And after that it worked as my expectation. You can try that..

Before:

def read_all(self):
    """Read all data until EOF; block until connection closed."""
    self.process_rawq()
    while not self.eof:
        self.fill_rawq()
        self.process_rawq()
    buf = self.cookedq
    self.cookedq = ''
    return buf

After (Add an exception for socket.timeout):

def read_all(self):
    """Read all data until EOF; block until connection closed."""
    self.process_rawq()
    while not self.eof:
        try:
            self.fill_rawq()
            self.process_rawq()
        except:
            break
    buf = self.cookedq
    self.cookedq = ''
    return buf
Tan Pham
  • 1
  • 2