Jul-23-2020, 03:38 PM
(This post was last modified: Jul-23-2020, 03:53 PM by John_Williamson.)
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:
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:
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.
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.