Python Forum
telnetlib timeout expections handling issue - 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: telnetlib timeout expections handling issue (/thread-10826.html)



telnetlib timeout expections handling issue - anna - Jun-08-2018

Hi There,

import threading
import telnetlib
import datetime
import re
import time
start = time.time()
username = "test" # the username
password = "test@123"
timeout = 120
def open_telnet(host):
    try:
       tn = telnetlib.Telnet(host, 23, timeout)
    except Exception as excp:
       print('time out:- {}\n '.format(host))
       pass
    tn.read_until("Username:")
    tn.write(username+"\n")
    tn.read_until("Password:")
    tn.write(password+"\n")
    tn.read_until(">")
    time.sleep(5)
    tn.write("show mac address  | i Fa"+"\n") # MAC learned through FastEthernet Interfaces
    tn.write("exit"+"\n")
    time.sleep(5)
    output=tn.read_all()
    for line in output.split('\n')[1:]:
           if 'exit' in line:
              break
           else:
               data= re.sub('\s{2,}', ' ', line) # Remove multiple spaces and replace with single space
               print host,data
def create_threads():
    threads = []
    with open('ohfbs.txt','r') as ipfile:
        for sr_no, line in enumerate(ipfile, start=1):
            host = line.strip()
            th = threading.Thread(target = open_telnet ,args = (host,))
            th.start()
            time.sleep(2)
            threads.append(th)
        for thr in threads:
            thr.join()

if __name__ == "__main__":
        create_threads()
        print("Exiting the program")
Error:
sometime getting error and not printing timeout and host ip Exception in thread Thread-16: Traceback (most recent call last): File "/usr/lib64/python2.7/threading.py", line 812, in __bootstrap_inner self.run() File "/usr/lib64/python2.7/threading.py", line 765, in run self.__target(*self.__args, **self.__kwargs) File "ciscobs.py", line 16, in open_telnet tn.read_until("Username:") UnboundLocalError: local variable 'tn' referenced before assignment Exception in thread Thread-11: Traceback (most recent call last): File "/usr/lib64/python2.7/threading.py", line 812, in __bootstrap_inner self.run() File "/usr/lib64/python2.7/threading.py", line 765, in run self.__target(*self.__args, **self.__kwargs) File "ciscobs.py", line 25, in open_telnet output=tn.read_all() File "/usr/lib64/python2.7/telnetlib.py", line 382, in read_all self.fill_rawq() File "/usr/lib64/python2.7/telnetlib.py", line 573, in fill_rawq buf = self.sock.recv(50) timeout: timed out
Output:
without timeout, output working ok 172.21.160.131 2306 0012.4387.1e40 DYNAMIC Fa0/22 172.21.160.131 2306 0026.9863.0bc1 DYNAMIC Fa0/16 172.21.160.131 3013 3cdf.1e02.e3c1 DYNAMIC Fa0/8 172.21.160.131 1700 2cb0.5d49.6893 DYNAMIC Fa0/8 172.21.200.200 1700 b475.0ed3.59d8 STATIC Fa0/2 172.21.200.200 1700 e8de.277e.30e5 STATIC Fa0/10 172.21.200.211 1700 0017.7c5d.68d5 STATIC Fa0/15 172.21.241.67 246 0013.5e06.f68c DYNAMIC Fa0/23 172.21.241.67 246 0013.5e0a.5462 DYNAMIC Fa0/22 172.21.241.67 246 0013.5e15.bc0c DYNAMIC Fa0/14 172.21.241.67 1700 1062.eb6b.73e6 DYNAMIC Fa0/23 172.21.241.67 1700 48ee.0c98.2c0d DYNAMIC Fa0/14 172.21.241.67 1700 48ee.0ca8.b283 DYNAMIC Fa0/23 172.21.230.211 1700 0017.7c6d.1d73 DYNAMIC Fa0/24 172.21.230.211 1700 0017.7c6d.5b1c DYNAMIC Fa0/24 172.21.230.211 1700 a0ab.1be1.e794 STATIC Fa0/12 172.21.230.211 1700 e8cc.1861.16cc DYNAMIC Fa0/24 172.21.230.211 246 0013.5e0a.889a DYNAMIC Fa0/24 172.21.200.217 1701 0022.3f32.05af STATIC Fa0/4 172.21.200.228 246 0013.5e09.7ccb DYNAMIC Fa0/23