【发布时间】:2014-04-29 07:40:37
【问题描述】:
最近我不得不为我的实习编写一个脚本来检查是否在一堆路由器/交换机配置中出现子网。
我制作了一个生成输出的脚本。现在我需要第二个脚本(我无法让它工作),它读取输出,如果出现子网将其写入aanwezig.txt,如果不写入nietAanwezig.txt。
许多其他答案帮助我制作了这个脚本并且它可以工作,但它只对前 48 个 IP 执行,并且有超过 2000 个...
checkOutput.py的代码:
def main():
file = open('../iprangesclean.txt', 'rb')
aanwezig = open('../aanwezig.txt', 'w')
nietAanwezig = open('../nietAanwezig.txt', 'w')
output = open('output.txt', 'rb')
for line in file:
originalLine = line
line.rstrip()
line = line.replace(' ', '')
line = line.replace('\n', '')
line = line.replace('\r', '')
one,two,three,four = line.split('.')
# 3Byte IP:
ipaddr = str(one) + "." + str(two) + "." + str(three)
counter = 1
found = 0
for lijn in output:
if re.search("\b{0}\b".format(ipaddr),lijn) and found == 0:
found = 1
else:
found = 2
print counter
counter= counter + 1
if found == 1:
aanwezig.write(originalLine)
print "Written to aanwezig"
elif found == 2:
nietAanwezig.write(originalLine)
print "Written to nietAanwezig"
found = 0
file.close()
aanwezig.close()
nietAanwezig.close()
main()
iprangesclean.txt 的格式如下:
10.35.6.0/24
10.132.42.0/24
10.143.26.0/24
10.143.30.0/24
10.143.31.0/24
10.143.32.0/24
10.35.7.0/24
10.143.35.0/24
10.143.44.0/24
10.143.96.0/24
10.142.224.0/24
10.142.185.0/24
10.142.32.0/24
10.142.208.0/24
10.142.70.0/24
and so on...
output.txt的一部分(我不能给你所有的东西,因为它有用户信息):
*name of device*.txt:logging 10.138.200.100
*name of device*.txt:access-list 37 permit 10.138.200.96 0.0.0.31
*name of device*.txt:access-list 38 permit 10.138.200.100
*name of device*.txt:snmp-server host 10.138.200.100 *someword*
*name of device*.txt:logging 10.138.200.100
【问题讨论】:
-
那不是 Python 程序。编辑问题并更正缩进。
-
我们不知道
output.txt -
附带说明,您应该首先考虑清理代码并删除多余的行。也许像:gist.github.com/anonymous/11394195
-
能否提供
output.txt的第46到50行?