0

我正在尝试ZNTS从 telnet 输出到主机中查找字符串(例如:)。

我想同时远程登录到多个主机,因为等待从主机到主机完成需要很长时间。主机响应不够快,所以我需要在每个命令行之后睡觉。

下面是我的代码:

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"    
4

1 回答 1

0

您可以尝试为每个 telnet 会话生成一个线程并以这种方式并行化搜索。

在此处查看多线程示例代码:

http://stackoverflow.com/questions/6286235/multiple-threads-in-python
于 2014-10-20T20:56:35.073 回答