如何合并IP网段
1. 安装IPy
pip3 install IPy
2. 写脚本:
yuyue@workplace:~ $ cat combine_ip.py
from IPy import IPSet, IP
import sys
def handler(file_name):
ret = IPSet()
for ip in open(file_name):
ip = "%s/24"%(ip)
ret.add(IP(ip, make_net = True))
for item in ret:
print(item)
if __name__ == "__main__":
file_name = sys.argv[1]
handler(file_name)
3. 开始执行,明明已经安装成功了,怎么执行脚本报错呢?
yuyue@workplace:~ $ python combine_ip.py
Traceback (most recent call last):
File "combine_ip.py", line 1, in <module>
from IPy import IPSet, IP
ImportError: No module named IPy
4. 找一找IPy装到哪里去了?
yuyue@workplace:~ $ sudo find / -name "*IPy*"
Password:
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/__pycache__/IPy.cpython-34.pyc
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPy-0.83-py3.4.egg-info
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPy.py
4. 执行 python3.4 combine_ip.py ip.list ok !!
==============分割线==============
http://www.ab126.com/goju/1840.html
>>> from ipcalc import Network
>>> net = Network('10.153.192.0/21')
>>> net.netmask()
IP('255.255.248.0')
>>> net.host_first()
IP('10.153.192.1')
>>> net.host_last()
IP('10.153.199.254')
>>> net.broadcast()
IP('10.153.199.255')