进去linux主机 安装IPy模块
cd /usr/local/src/python
git clone https://github.com/haypo/python-ipy.git
cd python-ipy
python setup.py install
from IPy import IP
ip=IP('192.168.1.0/24')
print ip.len() #查看指定IP地址的个数
for x in ip: #返回所有的IP地址
print (x)
eg;
256
192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
....
>>> from IPy import IP
>>> ip=IP('192.168.1.20')
>>> ip.reverseNames()
['20.1.168.192.in-addr.arpa.']
>>> ip.iptype()
'PRIVATE'
>>> IP("8.8.8.8").int()
134744072
>>> IP("8.8.8.8").strHex()
'0x8080808'
>>> IP("8.8.8.8").strBin()
'00001000000010000000100000001000'
>>> print (IP(0x8080808))
8.8.8.8
>>> print (IP('192.168.1.0').make_net('255.255.255.0'))
192.168.1.0/24
>>> IP('192.168.1.0/24').strNormal(0)
'192.168.1.0'
>>> IP('192.168.1.0/24').strNormal(1)
'192.168.1.0/24'
>>> IP('192.168.1.0/24').strNormal(2)
'192.168.1.0/255.255.255.0'
>>> IP('192.168.1.0/24').strNormal(3)
'192.168.1.0-192.168.1.255'
>>> IP('192.168.1.0/24').strNormal(4)
'192.168.1.0/24'
多网段包含关系计算方法:
>>> IP('10.0.0.0/24')<IP('12.0.0.0/24')
True
>>> '192.168.1.100' in IP('192.168.1.0/24')
True
>>> IP('10.0.0.0/24')<IP('12.0.0.0/24')
True
>>> '192.168.1.100' in IP('192.168.1.0/24')
True
>>> IP('192.168.1.0/24') in IP('192.168.0.0/16')
True
判断网个网段是否存在重叠:
>>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')
1 #存在重叠
>>> IP('192.168.0.0/23').overlaps('192.168.2.0/24')
0 #不存在重叠


