Issue when running telnet program using python3 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Issue when running telnet program using python3 (/thread-14871.html) Pages:
1
2
|
Issue when running telnet program using python3 - searching1 - Dec-21-2018 I have used this program in automating our network device backup config and import getpass import telnetlib import time user = input("Enter your telnet username: ") password = getpass.getpass() f = open("ipadd.txt") for line in f: print ("Getting running config from devices " + line) HOST = line.strip() tn = telnetlib.Telnet(HOST, 23, 5) 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.read_until(b"#") tn.write(b"conf t"+b"\n") time.sleep(1) tn.write(b"hostname test"+b"\n") time.sleep(1) tn.write(b"exit"+b"\n") time.sleep(1) tn.write(b"terminal length 0"+b"\n") time.sleep(3) tn.write(b"show run"+b"\n") time.sleep(3) tn.write(b"exit"+b"\n") readoutput = tn.read_all().decode('ascii') saveoutput = open("device.txt" + HOST, "w") saveoutput.write(readoutput) saveoutput.close Thank you
RE: Issue when running telnet program using python3 - buran - Dec-21-2018 by any chance, does your file ipadd.txt has a header? Add print(HOST) after line 10 or show the output from print on line 9.(Dec-21-2018, 03:55 AM)searching1 Wrote: **Issue/Error Message:** Im using python3.7This is not related, but according to traceback you run python 3.5, not 3.7 RE: Issue when running telnet program using python3 - searching1 - Dec-21-2018 Hi @buran, Thanks for your inputs. First, yes.. im using 3.5 and 3.7, Tried both but same result tho. "by any chance, does your file ipadd.txt has a header? Add print(HOST) after line 10 or show the output from print on line 9." - For the ipadd.txt (im not aware of any header that has been added) - adding print(host) on line before or after 10 doesn't work tho, I have added it under line 20. Which display the host. Line 20: print("host:",HOST) Output: host: 10.200.200.2 Also just want to share that script seems working in a way that it can connect to the device and change the hostname. - Another issue occur is with the password? Just want to ask if you encountered this? Quote:Warning (from warnings module): Again Thanks RE: Issue when running telnet program using python3 - buran - Dec-21-2018 (Dec-21-2018, 03:09 PM)searching1 Wrote: First, yes.. im using 3.5 and 3.7, Tried both but same result tho.I didn't expect it's version problem, just mentioned it because there was some discrepancy... Everything you describe is really odd. Here is why: Based on the traceback from original post, the error starts on line 13: tn.read_until(b"Username:") . And it does not execute anything after that line. So I was expecting HOST is not actual ip address (maybe just some string from the 1 line - the header). I don't know why you were not able to print after line 10. Before that - you actually have print in your script print ("Getting running config from devices " + line) .Now you claim you were able to print at/after line 20. So the original error is not there any more? Finally - the indentation of your code is not correct, e.g. look at lines 9 and 10. I guess it's just problem when you post it? RE: Issue when running telnet program using python3 - searching1 - Dec-21-2018 Thanks for your input, Noting change, still no being resolve. I still have this issue and this warning. Warning (from warnings module): File "/usr/lib/python3.5/getpass.py", line 63 passwd = fallback_getpass(prompt, stream) GetPassWarning: Can not control echo on the terminal. Warning: Password input may be echoed. Traceback (most recent call last): File "/home/lab-station/pyp/Backup Script/telnetbu.py", line 20, in <module> tn = telnetlib.Telnet(HOST, 23, 5) File "/usr/lib/python3.5/telnetlib.py", line 218, in __init__ self.open(host, port, timeout) File "/usr/lib/python3.5/telnetlib.py", line 234, in open self.sock = socket.create_connection((host, port), timeout) File "/usr/lib/python3.5/socket.py", line 693, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "/usr/lib/python3.5/socket.py", line 732, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno -2] Name or service not known RE: Issue when running telnet program using python3 - buran - Dec-21-2018 I give up, this is yet another traceback/error... Also, please, use proper tags when post code, traceback, output, etc. See BBcode help for more info. Also, always post the actual code that produce the error - this error is produced from different code - i.e. line 20 is different, based on the traceback. RE: Issue when running telnet program using python3 - searching1 - Dec-21-2018 OK thanks anyway. note that this is the actual code. RE: Issue when running telnet program using python3 - buran - Dec-21-2018 (Dec-21-2018, 09:47 PM)searching1 Wrote: note that this is the actual code.No, according to latest traceback (Dec-21-2018, 09:34 PM)searching1 Wrote: File "/home/lab-station/pyp/Backup Script/telnetbu.py", line 20, in <module> so line 20 is tn = telnetlib.Telnet(HOST, 23, 5) in your code this is line 11. And line 20 is tn.write(b"conf t"+b"\n") It's still not clear how you get error on different code line if you don't change anything RE: Issue when running telnet program using python3 - buran - Dec-21-2018 Also, let us know when you cross-post (on SO or elsewhere) RE: Issue when running telnet program using python3 - searching1 - Dec-21-2018 Copy that Buran, Will do. Thanks |