python自动化运维IP管理

一 点睛

IP地址规划是网络设计中非常重要的一个环节,规划的好坏会直接影响路由协议算法的效率,包括网络性能、可扩展性等方面,在这个过程当中,免不了要计算大量的IP地址,包括网段、网络掩码、广播地 址、子网数、IP类型等。Python提供了一个强大的第三方模块 IPy(https://github.com/haypo/python-ipy/),最新版本为V0.81。IPy模 块可以很好地辅助我们高效完成IP的规划工作。

二 安装

[root@localhost ~]# pip install ipy

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

1 点睛

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

2 实战

(venv) E:\Python\python_auto_maintain>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from IPy import IP
>>> IP('10.0.0.0/8').version()
4    #4代表IPv4类型
>>> IP('::1').version()
6    #6代表IPv6类型
>>> ip = IP('192.168.0.0/16')
>>> print(ip.len())           #输出192.168.0.0/16网段的IP个数
65536
>>> ip = IP('192.168.1.20')
>>> ip.reverseNames()                #反向解析地址格式
['20.1.168.192.in-addr.arpa.']
>>> ip.iptype()                 #192.168.1.20为私网类型'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与掩码生产网段格式
>>> 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参数值以定制不同 输出类型的网段。输出类型为字符串
"""
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。
"""
>>> 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'

四 多网络计算方法

1 点睛

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

2 实战

>>> 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代表不存在重叠

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

1 代码

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

2 结果1

E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/1_2_2.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
hexadecimal: 0xc0a80100
binary ip: 11000000101010000000000100000000
iptype: PRIVATE

3 结果2

E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/1_2_2.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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值