判断IP地址和网段是否包含于另一个网段中

>>> IP('10.10.10.0/24')<IP('10.10.11.0/24')

True

>>> '10.10.12.11' in IP('10.10.11.0/24')   

False

>>> '10.10.11.11' in IP('10.10.11.0/24') 

True

网段两个网段是否有重叠

>>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')    #1表示有重叠

1

>>> IP('192.168.2.0/23').overlaps('192.168.1.0/24')    #0表示没重叠


示例

根据输入的IP或子网返回网络、掩码、广播、反向解析、子网数、IP类型

[root@localhost python2.6]# cat netsimple1.py 

#!/usr/bin/env python

#by lineqi

#create time 2015-09-04

from IPy import IP

ip_s = raw_input('please input an IP or net-range:')

ips = IP(ip_s)


if len(ips) >1:

        print('net :%s ' % ips.net())

        print('netmask :%s ' % ips.netmask())

        print('broadcast :%s ' % ips.broadcast())

        print('revese address :%s ' % ips.reverseNames()[0])    #输出第1个子网地址反向解析

        print('subnet :%s ' % len(ips))                         #输出网络子网数

else:

        print('reverse address :%s' % ips.reverseNames())


print('iptype :%s' % ips.iptype())

[root@localhost python2.6]# python netsimple1.py 

please input an IP or net-range:192.168.1.0/28

net :192.168.1.0 

netmask :255.255.255.240 

broadcast :192.168.1.15 

revese address :0.1.168.192.in-addr.arpa. 

subnet :16 

iptype :PRIVATE

[root@localhost python2.6]# python netsimple1.py     

please input an IP or net-range:192.168.1.23

reverse address :['23.1.168.192.in-addr.arpa.']

iptype :PRIVATE