I am trying to find a string (e.g.: ZNTS
) from telnet output to a host.
I want to telnet to multiple hosts at the same time because waiting to finish from host to host takes a long time. The host can not response fast enough, so I need to sleep after every command line.
Below is my code:
import sys
import telnetlib
import time
MXK_LIST = ['172.16.32.15',
'172.16.33.30',
'192.168.55.3',
'192.168.52.3',
'192.168.54.3',
'192.168.42.25',
'192.168.43.3',
'192.168.44.3',
'192.168.45.3',
'192.168.46.3',
'192.168.47.3',
'192.168.48.3',
'192.168.49.9']
TOTAL_MXK = len(MXK_LIST)
ZNTS = str(sys.argv[1])
DEBUG = str(sys.argv[2])
username = "admin"
password = "4n0cmek0n9net"
I = 0
C = 0
print "\n Finding ZNTS = " + ZNTS
print "\n It will take around 5 minutes to complete the searching !!\n"
for MXK in MXK_LIST:
I = I + 1
OUTPUT = ""
try:
tn = telnetlib.Telnet(MXK)
except:
print "Bad Connection. Please verify IP again."
sys.exit(0)
tn.read_until(b"login: ",1)
tn.write(username + "\n")
time.sleep(5)
tn.read_until(b"password: ",1)
tn.write(password + "\n")
time.sleep(5)
OUTPUT = tn.read_until(b">",1)
if DEBUG == 1: print OUTPUT
time.sleep(5)
tn.write("onu showall 1" + "\n")
time.sleep(5)
tn.read_until(b"[no] ",1)
tn.write(b"yes" + "\n")
time.sleep(15)
OUTPUT = tn.read_until(b"quit",1)
time.sleep(5)
if DEBUG == 1: print OUTPUT
tn.write("A" + "\n")
time.sleep(5)
OUTPUT = tn.read_until(b">",1)
if DEBUG == 1: print OUTPUT
tn.write("exit\n")
if ZNTS in OUTPUT:
print str(I) + "/" + str(TOTAL_MXK) + " : FOUND " + ZNTS + " IN " + MXK
C = C + 1
else:
print str(I) + "/" + str(TOTAL_MXK) + " : NOT FOUND " + ZNTS + " IN " + MXK
print "\n"+ZNTS + " is found in " + str(C) + " MXK"