Question

I am telnetting to a cisco switch via python script. The code goes as follows:

#!/usr/bin/python
import getpass
import sys
import telnetlib

HOST = "10.203.4.1"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
  tn.read_until("Password: ")
  tn.write(password + "\n")

tn.write("vt100\n")
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()

It just hangs up after running the script. How can I resolve this?

Was it helpful?

Solution 2

Here is a simpler solution:

import pexpect
import getpass

HOST = "10.203.4.1"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

child = pexpect.spawn ('telnet '+HOST)
child.expect ('Username: ')
child.sendline (user)
child.expect ('Password: ')
child.sendline (password)
# If the hostname of the router is set to "deep"
# then the prompt now would be "deep>"
routerHostname = "deep" #example - can be different
child.expect (routerHostname+'>')
child.sendline ('enable')

Etc.

OTHER TIPS

You should look at Trigger: https://trigger.readthedocs.org/en/latest/

It's a automation toolkit to interact with network devices, like cisco routers/switches:

from trigger.cmds import Commando

class ShowClock(Commando):
    """Execute 'show clock' on a list of Cisco devices."""
    vendors = ['cisco']
    commands = ['show clock']

if __name__ == '__main__':
    device_list = ['foo1-abc.net.aol.com', 'foo2-xyz.net.aol.com']
    showclock = ShowClock(devices=device_list)
    showclock.run() # Commando exposes this to start the event loop

    print '\nResults:'
    print showclock.results

Check the docs for more information: https://trigger.readthedocs.org/en/latest/

First of all please consider using something besides telnet. SSH is a great drop in replacement. Secondly to make this pythonic use a library called pexpect to do this very thing. The last line would use the command .interact() to gain control again.

Cisco Python Telnet Script for cisco router and switches best and simple script for telneting and configuring layer 3 devices.

import getpass
import sys
import telnetlib

HOST = "YOUR ROUTER IP ADDRESS"
user = raw_input("Enter your telnet username: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

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


 tn.write("exit\n")

  print tn.read_all()

link for the code : Download the script here

Steps:

  1. An end device with installed python and connect the end device to router

  2. Configure telnet and username and password database

  3. Run python script

I wrote a similar code and got similar error. Then I made the code vocal to know where I am making mistake. What I concluded is: "Using read_all() function is not a good idea all the time. It reads infinitely and brings impression like hung mode. Try replacing it with device prompt followed by a timer during reading. And try printing it to see if code captured the desired output"

import telnetlib
import os
import sys

host = raw_input("Enter the VG IP : ")
user = "cisco"
password = "cisco"
#cmd = raw_input("Enter the command you want to feed : ")
cmd1 = "term len 0"
cmd = "show clock"
pingable = False

response = os.system("ping -c 2 " + host)
if response == 0:
    pingable = True
    print(host, "is Pingable", pingable)
else:
    print(host, "is un-Pingable", pingable)

if(pingable):
    tn = telnetlib.Telnet(host)
    banner = tn.read_until("Username:", 5)
    tn.write(user + "\n")
    print(banner)
    tn.read_until("Password:", 5)
    tn.write(password1 + "\n")
    prompt = tn.read_until("#")
    print("I am logged in\n\n")
    print(prompt)
    tn.write(cmd1 + b"\n")
    output1 = tn.read_until("#",5)
    print("my first cmd output is :", output1, "\n")
    tn.write(cmd + "\n")
    output1 = tn.read_until("#",5)
    print("My 2nd cmd is feeded here", output1)
    tn.write("show version\n")
    output1 = tn.read_until("more-- ",5)
    print("version info : ", output1)
    tn.write("exit\n")

else:
    print(host, "is unpingable")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top