我是python的新手,在使用脚本从思科网络设备收集配置信息时遇到了困难。
在ping测试以发现哪些设备处于运行状态之后,将创建一个包含响应IP的文件,然后由Exscript (用于SSH/telnet访问的python工具)函数进行解析。
问题是,在ping测试完成后,使用适当的IP创建文件后,脚本将结束,而不会启动快速入门或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)发布于 2015-08-22 01:33:36
以防任何人遇到类似的东西(我有点怀疑,因为这个脚本太具体了,可能这里不是发布它的正确位置)-问题是快速入门功能被传递了一个空的主机列表,所以没有建立任何连接。
为了解决这个问题,我跳过了get_hosts_from_file函数,只是将响应主机添加到一个列表中,并将该列表传递给快速入门函数。
https://stackoverflow.com/questions/32126880
复制相似问题