Python: ipaddress模块使用

# -*- coding:utf-8 -*-
import ipaddress

n1 = ipaddress.ip_network('192.0.2.0/28')
n2 = ipaddress.ip_network('192.0.2.1/32')
a = list(n1.address_exclude(n2))

print(a)
for i in a:
    print(i) # 将n2从n1中剔除

b = list(ipaddress.ip_network('192.168.0.0/29').hosts())
print(b)
for i in b:
    print(i)  # 打印主机地址

c = list(ipaddress.ip_network('192.168.0.0/22').subnets(new_prefix=24))
print(c)
for i in c:
    print(i) # 以24位掩码拆分'192.168.0.0/22'

d = list(ipaddress.ip_network('192.0.2.0/24').supernet(new_prefix=20))
print(d)

print(ipaddress.ip_network('192.0.2.0/24').supernet(new_prefix=20))

for addr in ipaddress.IPv4Network('192.168.2.0/28'):
    print(addr) # 打印'192.168.2.0/28'中的ip地址,注意和上面主机地址的区别

interface = ipaddress.IPv4Interface('192.168.1.1/23')
print(interface.ip) # 打印ip地址
print(interface.network) # 打印所属网段
print(interface.with_netmask) # 与网络的接口的字符串表示作为网络掩码
print(interface.with_prefixlen) # 带有掩码的接口的字符串表示形式,以前缀符号表示。

# IP地址进制间相互转换(点分十进制,十进制,十六进制,二进制,字节)

ip = ipaddress.ip_address('192.168.1.1')

print(ip.version) # 打印IP版本号
print(str(ip)) # 转换为字符串
print(int(ip)) # 转换为十进制
print(hex(int(ip))) # 转换为十六进制
print(bin(int(ip))) # 转换为二进制
print(oct(int(ip))) # 转换为八进制
print(ip.packed) # 转换为字节

print(ipaddress.ip_address(3221225985))  # 转换为ip地址
print(ipaddress.IPv4Address(3221225985))  # 转换为ip地址
print(ipaddress.v4_int_to_packed(3221225985))  # 转换为字节
print(ipaddress.IPv4Address(b'\xc0\x00\x02\x01'))  # 字节转换为ip地址
print(ipaddress.IPv4Address(0xc0a80101)) # 十六进制转换为ip地址
print(ipaddress.IPv4Address(0b11000000101010000000000100000001)) # 二进制转换为ip地址

d = [ipaddr for ipaddr in ipaddress.summarize_address_range(
    ipaddress.IPv4Address('192.168.1.0'),
    ipaddress.IPv4Address('192.168.1.25')
)]
for i in d:
    print(i) # Network objects can be iterated to list all the addresses belonging to the network.

e = [ipaddr for ipaddr in ipaddress.collapse_addresses([
    ipaddress.IPv4Network('192.168.1.0/31'),
    ipaddress.IPv4Network('192.168.1.2/31'),
    ipaddress.IPv4Network('192.168.1.4/30')]
)]
for i in e:
    print(i) # 将以上三个网段进行合并

print(ipaddress.ip_network('192.0.2.1/32').compare_networks(ipaddress.ip_network('192.0.2.2/32'))) # 结果为-1
print(ipaddress.ip_network('192.0.2.1/32').compare_networks(ipaddress.ip_network('192.0.2.0/32'))) # 结果为1
print(ipaddress.ip_network('192.0.2.1/32').compare_networks(ipaddress.ip_network('192.0.2.1/32'))) # 结果为0


print(ipaddress.IPv4Address('127.0.0.2') > ipaddress.IPv4Address('127.0.0.1'))
print(ipaddress.IPv4Address('127.0.0.2') == ipaddress.IPv4Address('127.0.0.1'))
print(ipaddress.IPv4Address('127.0.0.2') != ipaddress.IPv4Address('127.0.0.1'))  # 比较IP地址


# 实例一:合并IP地址
with open("IP.txt",'r',encoding='utf-8') as f:
    allip = f.readlines()
ip = [ipaddress.IPv4Network(net.strip('\n')) for net in allip]
ip2 = [addr for addr in ipaddress.collapse_addresses(ip)]
for i in ip2:
    print(i)

# 实例二:剔除IP地址(把n2中的ip地址从n1中剔除)
n1 = [ipaddress.ip_network('192.0.2.0/28'),ipaddress.ip_network('192.1.2.0/28')]
n2 = [ipaddress.ip_network('192.1.2.1/32'),ipaddress.ip_network('192.0.2.4/32')]
y = []
for i in n1:
    for x in n2:
        try:
            y = list(i.address_exclude(x))
        except Exception:
            pass
    for z in y:
        print(z)

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注