At work, we sometimes encounter the need to calculate whether an IP or network segment is in another network segment. Sometimes it is judged whether the two segments overlap. This is a lot easier to use ipy.
The following shell is the installation of ipy:
Cd/usr/localwget https://pypi.python.org/packages/source/I/IPy/IPy-0.81.tar.gz--NO-CHECK-CERTIFICATETAR-ZXVF IPY-0.81.TAR.GZCD Ipy-0.81python setup.py Install
The convenience of Ipy:
1:ip (' 10.0.0.0/8 '). Version () #获取IP的类型
2:
ip = IP (' 192.168.0.0/16 ') print Ip.len () #有多少个ipfor x in Ip:print (x) #分别打印出每个ip
3:
IP (' 8.8.8.8 '). Iptype () #ip的类型IP (' 8.8.8.8 '). Int () #转换成整形IP (' 8.8.8.8 '). Strhex () #转换成十六进制IP (' 8.8.8.8 '). Strbin () # Convert to binary print (IP (0x8080808)) #十六进制转换成ip
4:
IP (' 192.168.1.0/24 '). Strnormal (1) #输出/24 format IP (' 192.168.1.0/24 '). Strnormal (2) #输出掩码格式IP (' 192.168.1.0/24 '). Strnormal (3) #输出广播地址格式
5:
IP (' 192.168.1.0/24 ') in IP (' 192.168.0.0/16 ') #192.168.1.0/24 is inside the 192.168.0.0/16 bit
6:
IP (' 192.168.1.0/24 '). overlaps (' 192.168.2.0/24 ') #网段不存在重叠 returns 0 (returns 0 if overlapping)
Through the above record, we can solve the problem in the work is: batch to determine the type of IP, the bulk of the IP into their desired format, batch to determine whether an IP is included in a network segment, in batches to determine whether there is overlap between two network segments.
This article is from the "Zhangweihong" blog, make sure to keep this source http://zhuangweihong.blog.51cto.com/8808431/1650093
Python's computational-ipy on IP