Bottom Page

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
Quote
#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
searching1 likes this post
Quote
#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
Quote
#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?
Quote
#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
Quote
#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.
Quote
#7
OK thanks anyway. note that this is the actual code.
Quote
#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
Quote
#9
Also, let us know when you cross-post (on SO or elsewhere)
Quote
#10
Copy that Buran, Will do. Thanks
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  telnet from ssh tunnel oldfart 1 137 Aug-10-2019, 06:44 PM
Last Post: DeaD_EyE
  3.6 telnet eyler 3 4,374 Jun-28-2019, 05:22 AM
Last Post: Khanhamid90
  Any suggestion on python library to use for both ssh and telnet? lord_mani 4 491 Jun-25-2019, 04:07 PM
Last Post: gb74razor
  telnet question jacklee26 2 410 Mar-30-2019, 06:45 AM
Last Post: jacklee26
  Retrieve output from telnet command Networker 1 405 Mar-12-2019, 01:36 PM
Last Post: searching1
  GNS3 telnet from bash terminal to virtual cisco router marienbad 2 476 Feb-24-2019, 07:55 AM
Last Post: marienbad
  Telnet to switch from jump start server(linux) searching1 0 456 Jan-09-2019, 10:03 PM
Last Post: searching1
  Aggregate multiple telnet connections Jibeji 1 1,542 Mar-02-2018, 07:21 PM
Last Post: mpd
  mysql connector/telnet issue (re: text game) rebubula76 1 677 Feb-06-2018, 08:00 PM
Last Post: rebubula76
  Multithread telnet not working Parallel anna 7 2,068 Feb-05-2018, 01:17 PM
Last Post: anna

Forum Jump:


Users browsing this thread: 1 Guest(s)