Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Learning Python with telnetlib. Script seems to stall
#1
I'm using pythpn 3.7.7 and working with an interface called Spyder on a Mac

My goal is simply to be able to telnet into a device, run some commands, and exit the device. I am using a Cisco switch for this , at this time.

I found the basic example of how to use the telnet library, which is as follows:
import getpass
import telnetlib

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

tn = telnetlib.Telnet(HOST)

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

tn.write(b"ls\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))
I then made some edits to match the output from the Cisco switch. My code looks like this:
import getpass
import telnetlib

HOST = "10.14.31.68"
user = input("Enter your remote 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"sh ip int br\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))
I found a video on the learning.oreilly.com web site, where the author uses this exact same example code on a Cisco switch, so I know it should work.

The issue is that after prompting for the remote account name, and I type in the name cisco, it never prompts for the password. It seems like it is stuck in a loop. If I control-C out of it, this is the Traceback:
Error:
Enter your remote account: cisco Warning: QtConsole does not support password mode, the text you type will be visible. Traceback (most recent call last): File "/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 884, in _input_request ident, reply = self.session.recv(self.stdin_socket, 0) File "/opt/anaconda3/lib/python3.7/site-packages/jupyter_client/session.py", line 803, in recv msg_list = socket.recv_multipart(mode, copy=copy) File "/opt/anaconda3/lib/python3.7/site-packages/zmq/sugar/socket.py", line 475, in recv_multipart parts = [self.recv(flags, copy=copy, track=track)] File "zmq/backend/cython/socket.pyx", line 791, in zmq.backend.cython.socket.Socket.recv File "zmq/backend/cython/socket.pyx", line 827, in zmq.backend.cython.socket.Socket.recv File "zmq/backend/cython/socket.pyx", line 186, in zmq.backend.cython.socket._recv_copy File "zmq/backend/cython/checkrc.pxd", line 12, in zmq.backend.cython.checkrc._check_rc KeyboardInterrupt During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/john_williamson/.spyder-py3/hello.py", line 6, in <module> password = getpass.getpass() File "/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 842, in getpass password=True, File "/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 889, in _input_request raise KeyboardInterrupt KeyboardInterrupt
One thing I tried was to change the tn.read_until(b"Username: ") line to have the full test that shows up when I manually telnet into the switch, which is:
Output:
Username: Kerberos: No default realm defined for Kerberos!
But that didn't change the way the script runs.

Interestingly, I just tried the script from my shell on the Mac and I do get prompted for the password. It still gets stuck there. Using just the Telnet command with the IP address in the shell and putting in my login name and password, work fine. No delays at all.
Quote
#2
Further update:

From the shell on my Mac, the script eventually runs. But it takes many minutes.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to kill a bash script running as root from a python script? jc_lafleur 4 418 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 338 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 696 May-11-2019, 08:12 PM
Last Post: keames
  How to I define a variable between strings in telnetlib write? Fez 2 809 May-02-2019, 06:53 PM
Last Post: Fez
  Help - telnetlib issue? juanpb12 1 885 Apr-19-2018, 05:26 PM
Last Post: Larz60+
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 6,904 Jul-07-2017, 08:59 AM
Last Post: snippsat
  Cant pass corect variables to python script in bash script neradp 3 3,165 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)