【发布时间】:2015-07-09 00:07:53
【问题描述】:
我是 python 新手。我想在python中扫描ip地址,为此我想读取一个带有不同表示法的ip地址的文件,比如cidr或range,脚本应该将ip cidr/ranges提取到单独的ip地址中。
file_in = open("test.txt", "r")
file_out = open("ip-test2.txt","w")
i = 1
for line in file_in:
#take the line and look if that is a individual ip , if not make block of ip-
addresses and write them into a new file_out
file_out.write(str(i) + ": " + line)
i = i + 1
file_out.close()
file_in.close()
知道怎么做吗?或者我可以使用哪个工具?
【问题讨论】:
-
Regex 听起来像是您需要的工具。也许......输入文件的样本以及您想要的输出类型会有所帮助。
-
你能给我们举几个文件的例子吗? netaddr 包使用非常广泛。
-
@lanhance 一个例子 192.168.132.197/30 192.168.132.123-125 192.0.2.0 并在 output_file 我想要 192.168.132.197 192.168.132.198 192.168 .132.123 192.168.132.124 192.168.132.125 192.0.2.0 所以我得到了所有 ips 个人
-
@Ben 谢谢 :) netaddr 效果很好。我希望范围的功能并不多,但我认为我可以将范围更改为单独的 ips 手册。
标签: python python-2.7 ip-address cidr