Deepak
Deepak

Reputation: 1

Python telnet read a long output issue

Here is my telnet script as:

import sys
import getpass
import telnetlib
import time

HOST = "192.168.182.129"

user = input("Enter your remote  telnet account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
 tn.read_until(b"Password: ")
 tn.write(password.encode('ascii') + b"\n")

tn.write(b"show version\n")
time.sleep(5)
tn.write(b"config t\n")
time.sleep(2)
tn.write(b"interface loopback 1\n")
time.sleep(2)
tn.write(b"ip address 8.8.8.8 255.255.255.0\n")
tn.write(b"end\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))

Here, If I am using some long output command as "Show Version" then this script is not working as wanted and it will show only few lines of output and also cut the next command as:

Enter your remote  telnet account: deepak
Password:

R1#show version
Cisco IOS Software, 7200 Software (C7200-ADVIPSERVICESK9-M), Version 15.2(4)S5, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2014 by Cisco Systems, Inc.
Compiled Thu 20-Feb-14 06:51 by prod_rel_team

ROM: ROMMON Emulation Microcode
BOOTLDR: 7200 Software (C7200-ADVIPSERVICESK9-M), Version 15.2(4)S5, RELEASE SOFTWARE (fc1)

R1 uptime is 2 hours, 16 minutes
System returned to ROM by unknown reload cause - suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19
System image file is "tftp://255.255.255.255/unknown"
Last reload reason: Unknown reason



This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, **distributors and users are responsible for**

**R1#onfig t**

% Invalid input detected at '^' marker.

R1#interface loopback 1

% Invalid input detected at '^' marker.

R1#ip address 8.8.8.8 255.255.255.0

% Invalid input detected at '^' marker.

R1#end

Looking your help for the same.

Upvotes: 0

Views: 898

Answers (3)

Mike Pennington
Mike Pennington

Reputation: 43077

You did not put term len 0 (which disables command paging) in before running show version. This will fix your problem:

import sys
import getpass
import telnetlib
import time

HOST = "192.168.182.129"

user = input("Enter your remote  telnet account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
 tn.read_until(b"Password: ")
 tn.write(password.encode('ascii') + b"\n")
tn.write(b"term len 0\n")
tn.write(b"show version\n")
time.sleep(5)
tn.write(b"config t\n")
time.sleep(2)
tn.write(b"interface loopback 1\n")
time.sleep(2)
tn.write(b"ip address 8.8.8.8 255.255.255.0\n")
tn.write(b"end\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))

Upvotes: 0

Shervin Hariri
Shervin Hariri

Reputation: 105

At the end of your code you change some thing like this, but first be sure about your Router :

Be sure that in your Router you configured :

  • aaa new-model ( with : aaa authentication login default local )
  • Also in line vty 0 15 : transport input all, login authentication default
# if you configured enable password you should use this section :
tn.write(b"enable\n")
tn.write(b"cisco\n")
# Code for test :
tn.write(b"des TEST\n")
tn.write(b"end\n")
tn.write(b"exit\n")
# **End section should be exactly like this line**
print(tn.read_all())

Upvotes: 0

Lalo Ramírez
Lalo Ramírez

Reputation: 36

Instead of:

tn.write(b"config t\n")

Try to use:

tn.write("config t\n")

A better way to compact you script will be to store the commands to config into a list like this way:

commands =['conf t',  'interface loopback 1', 'ip address 8.8.8.8 255.255.255.0']
for command in commands:
    tn.write(command+'\n')
    time.sleep(1)

Upvotes: 0

Related Questions