如何合並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')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM