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]