1、IPy介绍
IP地址规划是网络设计中非常重要的一个环节,规划的好坏会直接影响路由协议算法的效率,包括网络性能、可扩展性等方面,在这个过程当中,免不了要计算大量的IP地址,包括网段、网络掩码、广播地址、子网数、IP类型等。
Python提供了一个强大的第三方模块IPy,最新版本(2017-11-16)为V0.83。
Github地址
https://github.com/autocracy/python-ipy/
pypi地址
https://pypi.python.org/pypi/IPy/
IPy模块可以很好地辅助我们高效完成IP的规划工作。
2、IPy安装
2.1、源码安装
Shell>cd /root/soft/Python
Shell>wget https://pypi.python.org/packages/88/28/79162bfc351a3f1ab44d663ab3f03fb495806fdb592170990a1568ffbf63/IPy-0.83.tar.gz
Shell>tar zxvf IPy-0.83.tar.gz
IPy-0.83/
IPy-0.83/.gitignore
IPy-0.83/AUTHORS
IPy-0.83/COPYING
IPy-0.83/ChangeLog
IPy-0.83/IPy.py
IPy-0.83/MANIFEST.in
IPy-0.83/Makefile
IPy-0.83/README
IPy-0.83/example/
IPy-0.83/example/confbuilder
IPy-0.83/example/confbuilder.py
IPy-0.83/setup.py
IPy-0.83/test/
IPy-0.83/test/test.rst
IPy-0.83/test/test_IPy.py
IPy-0.83/test/test_fuzz.py
IPy-0.83/test_doc.py
Shell>cd IPy-0.83
Shell>python2.7 setup.py install
running install
running build
running build_py
creating build
creating build/lib
copying IPy.py -> build/lib
running install_lib
copying build/lib/IPy.py -> /usr/local/python2.7/lib/python2.7/site-packages
byte-compiling /usr/local/python2.7/lib/python2.7/site-packages/IPy.py to IPy.pyc
running install_egg_info
Writing /usr/local/python2.7/lib/python2.7/site-packages/IPy-0.83-py2.7.egg-info
2.2、pip安装
Shell>pip2.7 install IPy
Collecting IPy
Downloading IPy-0.83.tar.gz
Installing collected packages: IPy
Running setup.py install for IPy ... done
Successfully installed IPy-0.83
3、IPy实战
3.1、获取IPy版本
>>> import IPy
>>> IPy.__version__
'0.83'
>>>
3.2、获取ip地址的版本信息
>>> IPy.IP("10.0.0.0/24").version()
4
>>>
>>> IPy.IP("192.168.1.1").version()
4
>>>
3.3、通过指定网段输出该网段的ip个数和所有ip地址清单
ip=IPy.IP("10.8.10.0/24")
print ip,type(ip)
'''
10.8.10.0/24 <class 'IPy.IP'>
'''
print ip.len()
'''
256
'''
for i in ip:
print i
'''
10.8.10.0
10.8.10.1
10.8.10.2
10.8.10.3
10.8.10.4
.....
10.8.10.253
10.8.10.254
10.8.10.255
'''
3.4、查看指定的ip是私有ip还是公网ip
ip=IPy.IP("10.8.10.0/24")
print ip.iptype()
'''
PRIVATE
'''
print IPy.IP("8.8.8.8").iptype()
'''
PUBLIC
'''
3.5、将IP地址转换成整数
#转换成整形
print IPy.IP("10.8.10.232").int()
'''
168299240
'''
3.6、IP地址排序
###################ip地址排序############
L=['10.8.1.1','10.7.1.2','10.8.10.254','10.1.1.1']
L1=sorted(L,key=lambda x:IPy.IP(x).int())
for i in L1:
print i
'''
10.1.1.1
10.7.1.2
10.8.1.1
10.8.10.254
'''
###################ip地址排序############
3.7、网络地址转换
#网络地址转换
print IPy.IP("192.168.0.0").make_net("255.255.254.0")
'''
192.168.0.0/23
'''
print IPy.IP("192.168.0.0/255.255.254.0",make_net=True)
'''
192.168.0.0/23
'''
print IPy.IP("192.168.0.0-192.168.1.255",make_net=True)
'''
192.168.0.0/23
'''
#Convert address to string
print "--"
print IPy.IP("192.168.0.0/24").strNormal(0),type(IPy.IP("192.168.0.0/24").strNormal(0))
print IPy.IP("192.168.0.0/24").strNormal(1)
print IPy.IP("192.168.0.0/24").strNormal(2)
print IPy.IP("192.168.0.0/24").strNormal(3)
print "---"
'''
--
192.168.0.0 <type 'str'>
192.168.0.0/24
192.168.0.0/255.255.255.0
192.168.0.0-192.168.0.255
---
'''
3.8、判断IP地址和网段是否包含于另一个网段中
#判断IP地址和网段是否包含于另一个网段中
print "判断IP地址和网段是否包含于另一个网段中"
print "192.168.1.22" in IPy.IP("192.168.1.0/24")
'''
True
'''
print "192.168.2.22" in IPy.IP("192.168.1.0/24")
'''
False
'''
3.9、网段比较
#网段比较
print "网段比较"
print IPy.IP("192.168.3.0/24")>IPy.IP("192.168.1.0/24")
'''
True
'''
3.10、判断两个网段是够存在重叠
#判断两个网段是够存在重叠
#返回0 代表不存在重叠
#返回1 代表存在重叠
print IPy.IP("192.168.3.0/24").overlaps("192.168.4.0/24")
'''
0
'''
print IPy.IP("192.168.2.0/23").overlaps("192.168.3.0/24")
'''
1
'''