安裝
先下載源碼,地址:ps://pypi.python.org/pypi/IPy/">https://pypi.python.org/pypi/IPy/ 朵栖,然后解壓后使用命令python setup.py install安裝责循。
使用
1、顯示IP類型
IP('192.168.1.1').version()
4
IP('::1').version()
6
類似如上所示歌粥,通過version方法可以的判斷輸入的IP是IPv4還是IPv6 。
2拍埠、網(wǎng)段計(jì)算輸出
代碼:
from IPy import IP
ip=IP('192.168.0.0/28')
print ip.len()
for x in ip:
print x
print ip.strNormal(0)
print ip.strNormal(1)
print ip.strNormal(2)
print ip.strNormal(3)
len()方法可以計(jì)算網(wǎng)段的IP個(gè)數(shù)失驶。
strNormal()方法指定不同wantprefixlen參數(shù)可以定制不同類型的輸出。上面輸出類似如下:
16
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
......
192.168.0.15
192.168.0.0
192.168.0.0/28
192.168.0.0/255.255.255.240
192.168.0.0-192.168.0.15
3枣购、格式轉(zhuǎn)換
實(shí)例介紹幾個(gè)常用方法嬉探,包括方向解析名稱、IP類型棉圈、IP進(jìn)制轉(zhuǎn)換涩堤、網(wǎng)絡(luò)地址網(wǎng)段地址轉(zhuǎn)換。
ip=IP('192.168.0.1')
print ip.reverseNames() #反向解析地址格式
print ip.iptype() #顯示IP地址類型分瘾,私有還是公有
ip=IP('8.8.8.8')
print ip.iptype()
print ip.int() #轉(zhuǎn)換成整型格式
print ip.strHex() #轉(zhuǎn)換成十六進(jìn)制格式
print ip.strBin() #轉(zhuǎn)換成二進(jìn)制格式
網(wǎng)絡(luò)地址胎围、網(wǎng)段地址格式轉(zhuǎn)換
print (IP('192.168.1.0').make_net('255.255.255.0'))
print (IP('192.168.1.0/255.255.255.0',make_net=True))
print (IP('192.168.1.0-192.168.1.255',make_net=True))
4、地址比較
判斷IP地址和網(wǎng)段是否包含于另一個(gè)網(wǎng)段中德召,如下:
'192.168.1.1' in IP('192.168.1.0/24')
True
IP('192.168.1.0/24') in IP('192.168.0.0/16')
True
判斷兩個(gè)網(wǎng)段是否存在重疊白魂,如下:
IP('192.168.0.0/23').overlaps('192.168.1.0/24')
1
IP('192.168.1.0/24').overlaps('192.168.2.0')
0
其中1表示存在重疊,0表示不存在重疊上岗。
舉例
代碼:
coding:utf-8
from IPy import IP
ip_s=raw_input("please input an IP or net-range: ")
ips=IP(ip_s)
if len(ips)>1: #網(wǎng)絡(luò)地址
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))
else: #單個(gè)地址
print('reverse address: %s' % ips.reverseNames()[0])
print('hexadecimal: %s' % ips.strHex())
print('binary: %s' % ips.strBin())
print('iptype: %s' % ips.iptype())
運(yùn)行結(jié)果:
C:\Users\admin\workspace\zhangnq>python IPy_test2.py
please input an IP or net-range: 192.168.1.1
reverse address: 1.1.168.192.in-addr.arpa.
hexadecimal: 0xc0a80101
binary: 11000000101010000000000100000001
iptype: PRIVATE
C:\Users\admin\workspace\zhangnq>python IPy_test2.py
please input an IP or net-range: 8.8.8.8
reverse address: 8.8.8.8.in-addr.arpa.
hexadecimal: 0x8080808
binary: 00001000000010000000100000001000
iptype: PUBLIC
C:\Users\admin\workspace\zhangnq>python IPy_test2.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
reverse address: 0.1.168.192.in-addr.arpa.
subnet: 16
hexadecimal: 0xc0a80100
binary: 11000000101010000000000100000000
iptype: PRIVATE
ipy模塊用法
一個(gè)自動(dòng)識(shí)別IP地址福荸、子網(wǎng)、方向解析肴掷、IP類型等信息的腳本
!/usr/bin/env python
-- coding: utf-8 --
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">def ip():
try:
from IPy import IP ###加載模塊
ip_s = raw_input('請(qǐng)輸入IP地址或者網(wǎng)段地址:' )###輸入一個(gè)IP地址或者網(wǎng)段
ips = IP(ip_s) #定義元素
if len(ips) > 1: #如果len出來的數(shù)字大于1敬锐,那么就是一個(gè)網(wǎng)段
print('網(wǎng)絡(luò)地址: %s' % ips.net())
print('子網(wǎng)掩碼: %s' % ips.netmask())
print('網(wǎng)絡(luò)廣播地址: %s' % ips.reverseNames() [0])
print('網(wǎng)絡(luò)子網(wǎng)數(shù): %s' % len(ips))
else: ###否則就是一個(gè)地址
print('IP反向解析: %s' % ips.reverseNames() [0])
print('十六進(jìn)制地址: %s' % ips.strHex())
print('二進(jìn)制地址: %s' % ips.strBin())
print('地址類型: %s' % ips.iptype())
print time.strftime("%Y-%m-%d %H:%M:%S")
#code
except Exception, e:
logging.info("error:" + str(e) + "\n" + traceback.format_exc())
print traceback.format_exc()
finally:
pass</pre>
運(yùn)行效果:
[root@mylinuxer python]# 192.168.1.0/24
-bash: 192.168.1.0/24: No such file or directory
[root@mylinuxer python]# python python.py
請(qǐng)輸入IP地址或者網(wǎng)段地址: 192.168.1.0/24
網(wǎng)絡(luò)地址: 192.168.1.0
子網(wǎng)掩碼: 255.255.255.0
網(wǎng)絡(luò)廣播地址: 1.168.192.in-addr.arpa.
網(wǎng)絡(luò)子網(wǎng)數(shù): 256
[root@mylinuxer python]# python python.py
請(qǐng)輸入IP地址或者網(wǎng)段地址: 192.168.1.1
IP反向解析: 1.1.168.192.in-addr.arpa.
十六進(jìn)制地址: 0xc0a80101
二進(jìn)制地址: 11000000101010000000000100000001
地址類型: PRIVATE
[root@mylinuxer python]# python python.py
請(qǐng)輸入IP地址或者網(wǎng)段地址: 116.213.249.211
IP反向解析: 211.249.213.116.in-addr.arpa.
十六進(jìn)制地址: 0x74d5f9d3
二進(jìn)制地址: 01110100110101011111100111010011
地址類型: PUBLIC