09-01-2020 12:59 PM
Hi All,
I'm trying to connect to our switch via telnet using script. It's just a basic script to change the hostname. But everytime I run the scrip I get an error.
import getpass import telnetlib HOST='192.168.0.2' 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"conf t\n") tn.write(b"hostname SW-Core1\n") tn.write(b"exit\n") tn.write(b"write\n") print(tn.read_all().decode('ascii'))
But everytime I run the scrip I get an error.
File "C:\Users\jcubio\AppData\Local\Programs\Python\Python38-32\lib\telnetlib.py", line 317, in read_until self.fill_rawq() File "C:\Users\jcubio\AppData\Local\Programs\Python\Python38-32\lib\telnetlib.py", line 526, in fill_rawq buf = self.sock.recv(50) ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
I tried telneting to the switch using putty and cmd and it was successful. I also tried the script in a gns3 environment and it was successful.
What would be the issue in this case? Please advise
Thank you
Solved! Go to Solution.
09-01-2020 01:25 PM
Add debug and see what is wrong on the script
tn = telnetlib.Telnet(HOST)
tn.set_debuglevel(1000)
09-01-2020 01:25 PM
Add debug and see what is wrong on the script
tn = telnetlib.Telnet(HOST)
tn.set_debuglevel(1000)
09-01-2020 01:37 PM
Hi Sir @balaji.bandi,
Debug shows it's passing the "User Name:" instead of "Username:" but in the script it is
Telnet Debug:
Telnet(192.168.0.2,23): recv b'\r\nUser Name:'
What should I change in the script?
Thank you very much
09-01-2020 01:44 PM
Can you post completely debug output as an attachment to understand better, please?
09-01-2020 01:47 PM
Hi Sir @balaji.bandi,
It works now. The issue is the spacing. Instead of
09-01-2020 01:50 PM
No worries, some time it happens blank space, depends on the editor you use - notepad ++ is good.
Glad all working as expected. and thank you marked as a solution.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide