Loading

IP地址处理模块IPy

一、IPy介绍

IPy-用于处理IPv4和IPv6地址和网络的类和工具。

网址: https://github.com/autocracy/python-ipy/

API介绍:

IP类允许一个舒适的解析并处理大多数
用于IPv4和IPv6地址和网络的符号。
受到RIPE的Perl模块NET :: IP界面的极大启发,但
不共享实现。它不共享非CIDR网络掩码,
所以像这样的时髦网络掩码0xffffff0f无法在这里完成。

The IP class allows a comfortable parsing and handling for most
notations in use for IPv4 and IPv6 addresses and networks. It was
greatly inspired by RIPE's Perl module NET::IP's interface but
doesn't share the implementation. It doesn't share non-CIDR netmasks,
so funky stuff like a netmask of 0xffffff0f can't be done here.

二、IPy模块安装

Centos 

[root@localhost ~]# pip3 install IPy
Collecting IPy
  Downloading https://files.pythonhosted.org/packages/88/28/79162bfc351a3f1ab44d663ab3f03fb495806fdb592170990a1568ffbf63/IPy-0.83.tar.gz
Installing collected packages: IPy
  Running setup.py install for IPy ... done
Successfully installed IPy-0.83

Mac OS

MacBook-Pro:~ h$ pip3 install IPy
Collecting IPy
  Downloading https://files.pythonhosted.org/packages/88/28/79162bfc351a3f1ab44d663ab3f03fb495806fdb592170990a1568ffbf63/IPy-0.83.tar.gz
Installing collected packages: IPy
  Running setup.py install for IPy ... done
Successfully installed IPy-0.83
MacBook-Pro:~ h$ python3
Python 3.6.3 (v3.6.3:2c5fed86e0, Oct  3 2017, 00:32:08) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import IPy
>>> exit()

三、IP地址、网段的基本处理

IPy模块包含IP类,使用它可以方便处理绝大部分格式为IPv6及IPv4的网络和地址。比如通过version方法可以区分IPv4与IPv6,如:

>>> IP('127.0.0.0/30').version()
4
>>> IP('::1').version()
6

通过指定的网段输出该网段的IP个数及所有IP地址清单,代码如下:

>>> from IPy import IP
>>> ip=IP('192.168.1.0/24')
>>> ip.len()        #输出192.168.1.0/24网段的IP个数
256
>>> for x in ip:    #输出192.168.1.0/24网段的所有IP清单
...     print(x)
...
192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
.........

下面介绍IP类的几个常见的方法,包括反向解析名称、IP类型、IP转换等。

>>> from IPy import IP
>>> ip = IP('192.168.121.1')
>>> ip.reverseNames()                  #反向解析地址格式
['1.121.168.192.in-addr.arpa.']
>>> ip.iptype()        #192.168.121.1为私网类型'PRIVATE'
'PRIVATE'
>>> IP('8.8.8.8').iptype()        #8.8.8.8为公网类型
'PUBLIC'
>>> IP('8.8.8.8').int()              #转换成整型格式
134744072
>>> IP('8.8.8.8').strHex()           #转换成十六进制格式
'0x8080808'
>>> IP('8.8.8.8').strBin()            #转换成二进制格式
'00001000000010000000100000001000'
>>> print(IP(0x8080808))            #十六进制转成IP格式
8.8.8.8

 IP方法也支持网络地址的转换,例如根据IP与掩码生成网段格式,示例:

>>> from IPy import IP
>>> IP('192.168.1.0').make_net('255.255.255.0')
IP('192.168.1.0/24')
>>> IP('192.168.1.0/255.255.255.0',make_net=True)
IP('192.168.1.0/24')
>>> IP('192.168.1.0-192.168.1.255',make_net=True)
IP('192.168.1.0/24')

也可以通过strNormal方法指定不同wantprefixlen参数值以定制不同输出类型的网段。输出类型为字符串,如下:

wantprefixlen的取值及含义:

wantprefixlen=0,五返回,如192.168.1.0;

wantprefixlen=1,prefix格式,如192.168.1.0/24;

wantprefixlen=2,decimalnetmask格式,如192.168.1.0/255.255.255.0;

wantprefixlen=3,lastIP格式,如192.168.1.0-192.168.1.255。

>>> from IPy import IP
>>> 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'

 

posted @ 2018-05-24 20:47  KubeSec  阅读(2802)  评论(0编辑  收藏  举报