1, Ipy Introduction
IP address planning is a very important link in network design, the quality of planning will directly affect the efficiency of the routing protocol algorithm, including network performance, scalability, etc., in this process, it is unavoidable to calculate a large number of IP addresses, including network segments, netmask, broadcast address, subnet number, IP type and so on.
Python offers a powerful third-party module IPY, the latest version (2017-11-16) for V0.83.
GitHub Address
https://github.com/autocracy/python-ipy/
PyPI Address
https://pypi.python.org/pypi/IPy/
The Ipy module is a great way to help us efficiently complete the planning of IP.
2, IPY installation 2.1, source installation
Shell>cd/root/soft/pythonshell>wget https://pypi.python.org/packages/88/28/ 79162bfc351a3f1ab44d663ab3f03fb495806fdb592170990a1568ffbf63/ipy-0.83.tar.gzshell>tar ZXVF IPy-0.83.tar.gz ipy-0.83/ipy-0.83/.gitignoreipy-0.83/authorsipy-0.83/copyingipy-0.83/changelogipy-0.83/ipy.pyipy-0.83/ manifest.inipy-0.83/makefileipy-0.83/readmeipy-0.83/example/ipy-0.83/example/confbuilderipy-0.83/example/ Confbuilder.pyipy-0.83/setup.pyipy-0.83/test/ipy-0.83/test/test.rstipy-0.83/test/test_ipy.pyipy-0.83/test/test _FUZZ.PYIPY-0.83/TEST_DOC.PYSHELL>CD ipy-0.83shell>python2.7 setup.py installrunning installrunning Buildrunning build_pycreating buildcreating build/libcopying ipy.py build/librunning install_libcopying build/ lib/ipy.py-/usr/local/python2.7/lib/python2.7/site-packagesbyte-compiling/usr/local/python2.7/lib/ python2.7/site-packages/ipy.py to Ipy.pycrunning install_egg_infowriting/usr/local/python2.7/lib/python2.7/ Site-packages/ipy-0.83-py2.7.egg-info
2.2. PIP Installation
Shell>pip2.7 install IPyCollecting IPy Downloading IPy-0.83.tar.gzInstalling collected packages: IPy Running setup.py install for IPy ... doneSuccessfully installed IPy-0.83
3, ipy actual combat 3.1, get Ipy version
>>> import IPy>>> IPy.__version__‘0.83‘>>>
3.2. Get the version information of the IP address
>>> IPy.IP("10.0.0.0/24").version()4>>>>>> IPy.IP("192.168.1.1").version()4>>>
3.3, through the specified network segment output the IP number of the network segment and all IP address list
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.010.8.10.110.8.10.210.8.10.310.8.10.4.....10.8.10.25310.8.10.25410.8.10.255‘‘‘
3.4. Check whether the specified IP is private IP or public network IP
ip=IPy.IP("10.8.10.0/24")print ip.iptype()‘‘‘PRIVATE‘‘‘print IPy.IP("8.8.8.8").iptype()‘‘‘PUBLIC‘‘‘
3.5. Convert IP Address to Integer
#转换成整形print IPy.IP("10.8.10.232").int()‘‘‘168299240‘‘‘
3.6. Sorting IP Addresses
###################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.110.7.1.210.8.1.110.8.10.254‘‘‘###################ip地址排序############
3.7. Network Address Translation
#网络地址转换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 stringprint "--"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/24192.168.0.0/255.255.255.0192.168.0.0-192.168.0.255---‘‘‘
3.8, determine whether the IP address and the network segment is included in another network segment
#判断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, network segment comparison
#网段比较print "网段比较"print IPy.IP("192.168.3.0/24")>IPy.IP("192.168.1.0/24")‘‘‘True‘‘‘
3.10, Judge two network segment is enough to exist overlap
#判断两个网段是够存在重叠#返回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‘‘‘
Python Module Learning ipy module