【问题标题】:Searching throughout telnet output result while doing parallel telnet to multiple network devices在对多个网络设备进行并行 telnet 时搜索整个 telnet 输出结果
【发布时间】:2014-11-23 16:09:20
【问题描述】:

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

我想同时远程登录到多个主机,因为从一个主机到另一个主机等待完成需要很长时间。主机响应不够快,所以每次命令行后我都需要休眠。

下面是我的代码:

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"    

【问题讨论】:

  • 欢迎来到 StackOverflow。您没有提出问题或没有明确解释您的问题,因此读者无法快速判断他们是否可以帮助您。

标签: telnetlib


【解决方案1】:

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

在这里查看多线程示例代码:

http://stackoverflow.com/questions/6286235/multiple-threads-in-python

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-15
    • 2014-11-15
    • 2016-05-26
    • 2010-10-21
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多