Vladimir Fomin
Vladimir Fomin

Reputation: 343

Python, access to cisco switch via telnet

I install python 2.7.10 and try this code:

import getpass
import sys
import telnetlib

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

tn = telnetlib.Telnet(HOST,23,5)

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

tn.write("en\n")
tn.write("sh ver\n")
tn.write("exit\n")

print tn.read_all()

and I have this error:

D:>test.py
Enter your remote account: vfomin
Password:
Traceback (most recent call last):
File "D:\test.py", line 21, in
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

How can I connect to 172.17.0.42 with python?

Upvotes: 0

Views: 1448

Answers (2)

Neo
Neo

Reputation: 1

Try this library (works on python 2.x):

pip install git+https://github.com/sergeyzelyukin/cisco-telnet.git

import ciscotelnet
with ciscotelnet.CiscoTelnet(host, verbose = False) as cisco:
  #if cisco.login(final_mode=CiscoTelnet.MODE_ENABLE, user="john", user_pass="12345678", enable_pass="cisco"):
  if cisco.login(final_mode=CiscoTelnet.MODE_ENABLE, line_pass="abcdef", enable_pass="cisco"):
    print cisco.cmd("sh int status | inc Fa0/1")
    print cisco.conf(["interface fast0/1", "descr blank", "load-interval 300"])
    print cisco.wr()

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174692

It seems you are using Python3, so you need input and not raw_input.

Upvotes: 1

Related Questions