Exscript - 网络设备发现/配置收集脚本(Python)

时间:2015-08-20 19:36:10

标签: python networking discovery

我对python很新,我正在尝试用来从cisco网络设备收集配置信息的脚本有困难。

在进行ping测试以发现哪些设备启动后,会创建一个包含响应IP的文件,然后通过Exscript(用于SSH / telnet访问的python工具)函数进行解析。

问题是在ping测试完成后,文件是使用适当的IP创建的,脚本会在不启动quickstart或getdevinfo函数的情况下结束。

知道为什么会这样吗?

from Exscript.util.start import quickstart
from Exscript.util.interact import read_login
from Exscript.util.file import get_hosts_from_file
from Exscript import Account
import os

account = read_login()
hosts = open("hosts",'w')

for x in range(65,85):
    if os.system("ping -c 1 -W 2 172.16.200.%s" % x) == 0:
        print 'reachable'
        hosts.write("ssh://172.16.200.%s" % x + "\n")
    else:
        print 'unreachable'
hosts.close

def getdevinfo(job,host,conn):
    print 'connection started'
    conn.execute('show ver | i Ver')
    devtypeinfo = str(conn.response)
    forparse = devtypeinfo.split()
    for word in forparse:
        if word.lower() == "security":
            file = open("hostinfo - " + str(conn.host),'w')
            print "Device Type Detected: ASA"
            conn.send("enable\r")
            conn.app_authorize(account)
            conn.execute("show run hostname")
            file.write(conn.response)
            conn.execute("show int ip bri | excl unassigned")
            file.write(conn.response)
            conn.execute("show route")
            file.write(conn.response)
            conn.send("exit\r")
            conn.close()
            file.close()
        elif word.lower() == "ios":
            file = open("hostinfo - " + str(conn.host),'w')
            print "Device Type Detected: Router"
            conn.send("enable\r")
            conn.app_authorize(account)
            conn.execute("show run | i hostname")
            file.write(conn.response)
            conn.execute("show ip int bri | excl unassigned")
            file.write(conn.response)
            conn.execute("show ip route")
            file.write(conn.response)
            conn.execute("show cdp nei")
            file.write(conn.response)
            conn.send("exit\r")
            conn.close()
            file.close()

hosts2 = get_hosts_from_file('hosts')
quickstart(hosts2, getdevinfo, max_threads = 6)

1 个答案:

答案 0 :(得分:0)

如果有人遇到类似的东西(我有点怀疑,因为这个脚本是如此具体,也许这不是发布这个的正确位置) - 问题是快速入门函数被传递一个空列表主持人,所以没有建立联系。

为了解决这个问题,我跳过了get_hosts_from_file函数,只是将响应主机添加到列表中,并将该列表传递给quickstart函数。