python運維開發經常使用模塊(二)IPy

1.安裝

IP地址規劃是網絡設計中很是重要的一個環節,規劃的好壞會直 接影響路由協議算法的效率,包括網絡性能、可擴展性等方面,在這個 過程中,免不了要計算大量的IP地址,包括網段、網絡掩碼、廣播地 址、子網數、IP類型等。Python提供了一個強大的第三方模塊 IPy(https://github.com/haypo/python-ipy/),最新版本爲V1.0。IPy模 塊能夠很好地輔助咱們高效完成IP的規劃工做,下面進行詳細介紹。ipy採用pip安裝方式:python

[root@VM_0_7_centos ~]# pip install ipy
Collecting ipy
Downloading http://mirrors.tencentyun.com/pypi/packages/e1/66/b6dd22472bb027556849876beae2dd4dca3a4eaf2dd3039277b4edb8c6af/IPy-1.00.tar.gz
Installing collected packages: ipy
Running setup.py install for ipy ... done
Successfully installed ipy-1.0

2.模塊經常使用方法

 IPy模塊包含IP類,使用它能夠方便處理絕大部分格式爲IPv6及 IPv4的網絡和地址。好比經過version方法就能夠區分出IPv4與IPv6, 如:git

>>> ip=IP('10.0.0.0/8').version()
>>> print ip   
4                 #4表明IPv4類型
>>> ip=IP('::1').version()
>>> print ip 
6                #4表明IPv6類型

經過指定的網段輸出該網段的IP個數及全部IP地址清單,代碼如 下:github

>>> ip=IP('192.168.0.0/16')
>>> print ip.len()
65536

for x in ip: #輸出192.168.0.0/16網段的全部IP清單
    print(x)

IP類幾個常見的方法,包括反向解析名稱、IP類型、IP轉 換等。算法

>>> ip=IP('192.168.1.20')
>>> ip.reverseNames()           #反向解析地址格式
['20.1.168.192.in-addr.arpa.'] 
>>> ip=IP('192.168.1.20')        #192.168.1.20爲私網類型'PRIVATE'
>>> ip.iptype()
'PRIVATE'
>>> ip=IP('8.8.8.8')        #8.8.8.8爲公網類型
>>> ip.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

IP方法也支持網絡地址的轉換,例如根據IP與掩碼生產網段格式, 以下:centos

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

也能夠經過strNormal方法指定不一樣wantprefixlen參數值以定製不一樣 輸出類型的網段。輸出類型爲字符串,以下:網絡

>>> 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'

wantprefixlen的取值及含義: ·wantprefissh

xlen=0,無返回,如192.168.1.0; ·oop

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

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

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

多網絡計算方法詳解

有時候咱們想比較兩個網段是否存在包含、重疊等關係,好比同 網絡但不一樣prefixlen會認爲是不相等的網段,如10.0.0.0/16不等於 10.0.0.0/24,另外即便具備相同的prefixlen但處於不一樣的網絡地址,一樣 也視爲不相等,如10.0.0.0/16不等於192.0.0.0/16。IPy支持相似於數值型 數據的比較,以幫助IP對象進行比較,如:

>>> IP('10.0.0.0/24') < IP('12.0.0.0/24')
True

判斷IP地址和網段是否包含於另外一個網段中,以下:

>>> '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

判斷兩個網段是否存在重疊,採用IPy提供的overlaps方法,如:

>>> 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')
0       #返回0表明不存在重疊

3.示例:

根據輸入的IP或子網返回網絡、掩碼、廣播、反向解析、 子網數、IP類型等信息。

#!/usr/bin/env python
#_*_coding:utf-8_*_
from IPy import IP
ip_s = raw_input('Please input an IP or net-range:') #接受用戶輸入,參數爲ip地址或網段地址
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)) #輸出網絡子網數
else:
    print ('reverse address: %s' % ips.reverseNames()[0]) #輸出反向解析網路地址
    print ('hexadecimal: %s' % ips.strHex()) #輸出十六進制地址
    print ('binary ip: %s' %ips.strBin()) #輸出二進制地址
    print ('iptype: %s' %ips.iptype()) #輸出地址類型 private、public、loopback等

 

[root@benjamincloud devpython]# python ip_sample.py 
Please input an IP or net-range:192.168.1.0/24   
net: 192.168.1.0
netmask: 255.255.255.0
broadcast: 192.168.1.255
reverse address: 1.168.192.in-addr.arpa.
subnet: 256
[root@benjamincloud devpython]# python ip_sample.py 
Please input an IP or net-range:192.168.1.20
reverse address: 20.1.168.192.in-addr.arpa.
hexadecimal: 0xc0a80114
binary ip: 11000000101010000000000100010100
iptype: PRIVATE

 

4.關於更多

>>> dir(IP)
['__add__', '__bool__', '__class__', '__cmp__', '__contains__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__weakref__', '_getIPv4Map', '_printPrefix', 'broadcast', 'get_mac', 'int', 'iptype', 'len', 'make_net', 'net', 'netmask', 'overlaps', 'prefixlen', 'reverseName', 'reverseNames', 'strBin', 'strCompressed', 'strDec', 'strFullsize', 'strHex', 'strNetmask', 'strNormal', 'v46map', 'version']

官網文檔與示例參考    https://github.com/haypo/python-ipy/

相關文章
相關標籤/搜索