51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

IP地址处理模块IPy(Python)

在IP地址规划中,涉及到计算大量的IP地址,包括网段、网络掩码、广播地址、子网数、IP类型等,别担心,Ipy模块拯救你。Ipy模块可以很好的辅助我们高效的完成IP的规划工作。

{#7811-1542186234972}源码方式安装IPy模块

{#3710-1542186272476}[root@localhost ~]# wget -c https://pypi.python.org/packages/source/I/IPy/IPy-0.81.tar.gz --no-check-certificate

{#6856-1542186258524}[root@localhost ~]# tar zxf IPy-0.81.tar.gz

{#7084-1542186271039}[root@localhost ~]# cd IPy-0.81

{#1521-1542186271039}[root@localhost IPy-0.81]# python setup.py install

{#9234-1542186271039}running install

{#3344-1542186271039}running build

{#3644-1542186271039}running build_py

{#7259-1542186271039}creating build

{#7124-1542186271039}creating build/lib

{#5064-1542186271039}copying IPy.py -> build/lib

{#9495-1542186271039}running install_lib

{#5397-1542186271039}copying build/lib/IPy.py -> /usr/lib/python2.7/site-packages

{#5172-1542186271039}byte-compiling /usr/lib/python2.7/site-packages/IPy.py to IPy.pyc

{#4935-1542186271039}running install_egg_info

{#3446-1542186271039}Writing /usr/lib/python2.7/site-packages/IPy-0.81-py2.7.egg-info

{#4326-1542186313404}1)IP地址、网段的基本处理

{#2311-1542186371148}IPy模块包含IP类,使用它可以方便处理绝大部分格式为IPv6的网络和地址

{#7950-1542186371148}比如通过version方法来区分出IPv4和IPv6

{#2430-1542186371148}[root@localhost ~]# python

{#3052-1542186616080}Python 2.7.5 (default, Jul 13 2018, 13:06:57)

{#9024-1542186616080}[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2

{#1978-1542186616080}Type "help", "copyright", "credits" or "license" for more information.

{#8732-1542186616080}>>> import IPy

{#9065-1542186616080}>>> IPy.IP('10.0.0.0/8').version()

{#4386-1542186616080}4

{#9163-1542186616080}>>> IPy.IP('::1').version()

{#0047-1542186616080}6

{#4712-1542186616080}通过指定的网段输出该网段的IP个数和所有的IP地址清单

{#9418-1542186371148}内容如下:

{#9230-1542186371148}[root@aliyun_server_v2 ~]# cat ip.py

{#4083-1542186813968}#!/usr/bin/python

{#1470-1542186813968}import IPy

{#9329-1542186813968}ip = IPy.IP('192.168.31.0/24')

{#3634-1542186813968}print ip.len()

{#6589-1542186813968}for x in ip:

{#6163-1542186813968}print x

{#1499-1542186816156}[root@aliyun_server_v2 ~]# python ip.py

{#4058-1542186842209}256

{#1192-1542186842209}192.168.31.0

{#3025-1542186842209}192.168.31.1

{#9287-1542186842209}192.168.31.2

{#8729-1542186842209}192.168.31.3

{#7188-1542186842209}192.168.31.4

{#1188-1542186842209}192.168.31.5

{#4055-1542186842209}192.168.31.6

{#6648-1542186842209}192.168.31.7

{#6690-1542186842209}192.168.31.8

{#7390-1542186842209}192.168.31.9

{#1863-1542186842209}192.168.31.10

{#3664-1542186842209}192.168.31.11

{#9220-1542186842209}192.168.31.12

{#6420-1542186842209}192.168.31.13

{#1884-1542186842209}192.168.31.14

{#4446-1542186842209}192.168.31.15

{#1918-1542186842209}192.168.31.16

{#7684-1542186843405}.......

{#6480-1542186371148}反向解析名称、IP类型、IP转换等

{#9950-1542186990596}>>> from IPy import IP

{#4531-1542186991104}>>> ip = IP('192.168.31.253')

{#4791-1542186991104}>>> ip.reverseNames() # 反向解析地址格式

{#9790-1542186991104}['253.31.168.192.in-addr.arpa.']

>>> ip.iptype() # 私网类型

'PRIVATE'

>>> IP('8.8.8.8').iptype() # 公网类型

'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

>>> print IP(134744072) # 整型格式转换为IP格式

8.8.8.8

{#7395-1542186371148}IP方法也支持网络地址的转换,例如根据IP和掩码产生网段格式

{#1781-1542186371148}>>> print (IP('192.168.31.0').make_net('255.255.255.0'))

192.168.31.0/24

>>> print (IP('192.168.31.0/255.255.255.0',make_net=True))

192.168.31.0/24

>>> print (IP('192.168.31.0-192.168.31.255',make_net=True))

{#8785-1542187243646}192.168.31.0/24

{#9990-1542186371148}通过strNormal方法指定不同wantprefixlen参数值以定制不同输出类型的网段,输出类型为字符串

{#3820-1542186371148}>>> IP('192.168.31.0/24').strNormal(0)

{#1334-1542187302944}'192.168.31.0'

>>> IP('192.168.31.0/24').strNormal(1)

'192.168.31.0/24'

>>> IP('192.168.31.0/24').strNormal(2)

'192.168.31.0/255.255.255.0'

>>> IP('192.168.31.0/24').strNormal(3)

'192.168.31.0-192.168.31.255'

{#9237-1542188052218}wantprefixlen的取值及含义:

{#8231-1542188067882}wantprefixlen = 0,无返回,如192.168.31.0

{#8565-1542188080444}wantprefixlen = 1,prefix格式,如192.168.31.0/24

{#5477-1542188080859}wantprefixlen = 2,ecimalnetmask格式,如192.168.31.0/255.255.255.0

{#9686-1542188081451}wantprefixlen = 3,lastIP格式,如192.168.31.0-192.168.31.255

{#4580-1542186371148}2)多网络计算方法详解

{#5778-1542186371148}比较两个网段是否存在包含、重叠等关系,比如同网络但不同prefixlen会认为是不相等的网段,如10.0.0.0/16不等于10.0.0.0/24,另外即使具有相同的prefixlen但处于不同的网络地址,同样也视为不相等,如10.0.0.0/16不等于192.0.0.0/16。IPy支持类似于数值型数据的比较,以帮助IP对象进行比较。

{#9072-1542186371148}#比较IP大小

{#8042-1542186371148}>>> IP('10.0.0.0/24') < IP('12.0.0.0/24')

True

{#4058-1542186371148}#判断IP地址和网段是否包含于另一个网段中

{#7937-1542186371148}>>> '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

{#7841-1542186371148}#判断两个网段是否存在重叠(overlaps方法)

{#5656-1542186371148}>>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')

1 #返回1代表存在重叠

>> IP('192.168.1.0/24').overlaps('192.168.2.0/24')

0 #返回0代表不存在重叠

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

[root@localhost ~]# cat simple.py

#!/usr/bin/python

#coding=utf-8

from IPy import IP

#接受用户输入,参数为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('reverse address: %s' % ips.reverseNames()[0])

#输出网络子网数

print('subnet: %s' % len(ips))

#为单个IP地址

else:

#输出IP方向解析

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

#输出十六进制地址

print('hexadecimal: %s' % ips.strHex())

#输出二进制地址

print('binary: %s' % ips.strBin())

#输出地址类型,如PRIVATE、PUBLIC、LOOPBACK等

print('iptype: %s' % ips.iptype())
[root@localhost ~]# python simple.py

Please input an IP or net-range: 192.168.31.0/24

net: 192.168.31.0

netmask: 255.255.255.0

broadcast: 192.168.31.255

reverse address: 31.168.192.in-addr.arpa.

subnet: 256

hexadecimal: 0xc0a81f00

binary: 11000000101010000001111100000000

iptype: PRIVATE

[root@localhost ~]# python simple.py

Please input an IP or net-range: 192.168.31.253

reverse address: 253.31.168.192.in-addr.arpa.

hexadecimal: 0xc0a81ffd

binary: 11000000101010000001111111111101

iptype: PRIVATE

IP地址处理模块IPy(Python)
继续阅读

历史上的今天

11 月
15

赞(0)
未经允许不得转载:工具盒子 » IP地址处理模块IPy(Python)