Python Forum
Issue when running telnet program using python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue when running telnet program using python3
#1
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


Error:
**Issue/Error Message:** Im using python3.7 Traceback (most recent call last): File "telnetbu.py", line 27, in <module> tn.read_until(b"Username:") File "/usr/lib/python3.5/telnetlib.py", line 311, in read_until selector.register(self, selectors.EVENT_READ) File "/usr/lib/python3.5/selectors.py", line 351, in register key = super().register(fileobj, events, data) File "/usr/lib/python3.5/selectors.py", line 237, in register key = SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data) File "/usr/lib/python3.5/selectors.py", line 224, in _fileobj_lookup return _fileobj_to_fd(fileobj) File "/usr/lib/python3.5/selectors.py", line 39, in _fileobj_to_fd "{!r}".format(fileobj)) from None ValueError: Invalid file object: <telnetlib.Telnet object at 0x7fe669487080>
Thank you
Reply
#2
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.7
This is not related, but according to traceback you run python 3.5, not 3.7
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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):
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.

Again Thanks
Reply
#4
(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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
OK thanks anyway. note that this is the actual code.
Reply
#8
(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>
tn = telnetlib.Telnet(HOST, 23, 5)

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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Also, let us know when you cross-post (on SO or elsewhere)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Copy that Buran, Will do. Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  telnet from ssh tunnel oldfart 3 7,064 Jul-17-2020, 02:28 PM
Last Post: noobami99
  telnet to a device under tacacs management kang18 0 1,540 Jun-05-2020, 06:11 AM
Last Post: kang18
  Selector file descriptor issue in simple multiclient server program dohpam1ne 0 2,888 May-28-2020, 02:39 PM
Last Post: dohpam1ne
  3.6 telnet eyler 3 11,211 Jun-28-2019, 05:22 AM
Last Post: Khanhamid90
  Any suggestion on python library to use for both ssh and telnet? lord_mani 4 3,669 Jun-25-2019, 04:07 PM
Last Post: gb74razor
  telnet question jacklee26 2 2,462 Mar-30-2019, 06:45 AM
Last Post: jacklee26
  Retrieve output from telnet command Networker 1 4,041 Mar-12-2019, 01:36 PM
Last Post: searching1
  Aggregate multiple telnet connections Jibeji 1 4,222 Mar-02-2018, 07:21 PM
Last Post: mpd
  mysql connector/telnet issue (re: text game) rebubula76 1 2,505 Feb-06-2018, 08:00 PM
Last Post: rebubula76
  Multithread telnet not working Parallel anna 7 7,358 Feb-05-2018, 01:17 PM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020