pythonmultithreadingpython-multithreadingtelnettelnetlib

Python threading with Telnet


I am trying to connect to multiple switches via telnet and get an output with CPU usage. One thread works and shows the proper CPU Usage. The second thread does not do anything. How do I get two threads working with the same command as the first.

import time
import telnetlib
import threading


Host1 = '192.168.1.42'
username1 = 'root'

Host2 = '192.168.86.247'
username2 = 'root'

tn = telnetlib.Telnet(Host1)

def switch1():
   tn.write(username1.encode("ascii") + b"\n")

   #confirms connection
   print("connected to %s" % Host1)

   #send command
   tn.write(b"sh cpu-usage\n")
   time.sleep(2)

   #reads clean i/o
   output = tn.read_very_eager()


   #print the command
   print (type("output"))
   print(output)
   print("done")



def switch2():
   #input username
   tn.write(username2.encode("ascii") + b"\n")
   tn.write(password.encode("ascii") + b"\n")

   #confirms connection
   print("connected to %s" % Host2)

   #send command
   tn.write(b"sh cpu-usage\n")
   time.sleep(2)

   #reads clean i/o
   output1 = tn.read_very_eager()


   #print the command
   print (type("output"))
   print(output1)
   print("done")



t1 = threading.Thread(target=switch1)



t2 = threading.Thread(target=switch2)





t1.start()
t2.start()

Here is the Output

[Command: python -u C:\Users\AKPY7Z\Documents\Threading\threadcpu.py]
connected to 192.168.1.42
connected to 192.168.86.247
<class 'str'><class 'str'>
b'ugoonatilaka\r\r\n         ^\r\n% Invalid input detecte'
done
b"\r\r\nswitch_a login: root\r\njanidugoonatilaka\r\nsh cpu-usage\r\n*password*\r\nifconfig\r\n\r\r\nSwitch version 2.01.2.7 03/29/18 10:36:11\r\nswitch_a>janidd at '^' marker.\r\n\r\nswitch_a>sh cpu-usage\r\r\nNow CPU Usage 17%\r\nMax CPU Usage 18%\r\nswitch_a>*password*\r\r\n         ^\r\n% Invalid input detected at '^' marker.\r\n\r\nswitch_a>ifconfig\r\r\n         ^\r\n% Invalid input detected at '^' marker.\r\n\r\nswitch_a>"
done
[Finished in 2.678s]<class 'str'>
b'\r\n'
done
[Finished in 293.505s]

Solution

  • You create connection to only one switch

    tn = telnetlib.Telnet(Host1)
    

    and later you use the same connection in both functions and every function tries to use different username and password - and probably only one of them uses correct values for this switch.

    You should run Telnet(Host1) in one function and Telnet(Host2) in other function and they would try to access different switches.

    def switch1():
       tn = telnetlib.Telnet(Host1)
       # ... rest ...
    
    def switch2():
       tn = telnetlib.Telnet(Host2)
       # ... rest ...
    

    BTW:

    You could create one function and run it with different parameters

    import time
    import telnetlib
    import threading
    
    
    host1 = '192.168.1.42'
    username1 = 'root'
    
    host2 = '192.168.86.247'
    username2 = 'root'
    
    def switch(host, username, password=None):
        tn = telnetlib.Telnet(host)
       
        tn.write(username.encode("ascii") + b"\n")
        if password:
           tn.write(password.encode("ascii") + b"\n")
            
        # confirms connection
        print("connected to %s" % host)
    
        # send command
        tn.write(b"sh cpu-usage\n")
        time.sleep(2)
    
        # reads clean i/o
        output = tn.read_very_eager()
    
        # print the command
        print (type("output"))
        print(output)
        print("done")
    
    t1 = threading.Thread(target=switch, args=(host1, username1, password))
    t2 = threading.Thread(target=switch, args=(host2, username2, None))
    
    t1.start()
    t2.start()