欢迎光临
我们一直在努力

netaddr网络地址工具python

print("==========1==========")
from netaddr import IPNetwork
 
# IPNetwork('192.168.7.80/30'), IPNetwork('192.168.7.84/30'), IPNetwork('192.168.7.88/30'), IPNetwork('192.168.7.92/30')
ip_str = "192.168.7.83/28"
# ip_str="192.168.7.92/30"
# ip_str = "192.168.1.130/32"
# ip_str = "192.168.34.92/255.255.255.248"
print(ip_str)
ip = IPNetwork(ip_str)
print(ip)
 
# 子网掩码
print(ip.netmask)
# 广播地址
print(ip.broadcast)
# 网络地址
print(ip.network)
# 取反掩码
print(ip.hostmask)
# 同一网段ip总数
print(ip.size)
# 修改地址的掩码长度为28
ip.prefixlen = 28
print(ip)
 
# 将同一网段的ip,再化为掩码长度为30的几个子网段
new_ips_list = [x for x in ip.subnet(30)]
print(new_ips_list)
 
# # 获取某一网段的所有主机地址
for ip in IPNetwork(ip_str).iter_hosts():
    print(ip)
 
print("==========2==========")
# IPAddress的用法
from netaddr import IPAddress
 
# 判断地址的公网、私网属性
print(IPAddress("100.64.0.1").is_private())
print(IPAddress("114.114.114.114").is_private())
 
print("==========3==========")
# 定义任意一段IP地址的范围
from netaddr import IPRange
 
ip_range = IPRange("192.168.1.10", "192.168.1.130")
# 对这一段ip地址进行地址聚合
print(ip_range.cidrs())
 
print("==========4==========")
# 多个分散ip再聚合
from netaddr import cidr_merge
 
ip_list = []
ip_list.append(IPNetwork("192.168.1.0/25"))
ip_list.append(IPNetwork("192.168.1.128/25"))
ip_list.append(IPNetwork("192.168.0.0/24"))
ip_list.append(IPNetwork("192.168.3.0/26"))
ip_list.append(IPNetwork("192.168.3.64/26"))
print(cidr_merge(ip_list))
 
print("==========5==========")
# 处理MAC地址
from netaddr import EUI, mac_unix, mac_cisco, mac_bare
 
# mac = EUI("98:5a:eb:9f:35:f8")
mac = EUI("98-5A-EB-9F-35-F8")
print(mac)
 
# mac显示风格1
mac.dialect = mac_unix
print(mac)
# mac显示风格2
mac.dialect = mac_cisco
print(mac)
# mac显示风格3
mac.dialect = mac_bare
print(mac)
 
# mac地址厂家信息
print(mac.info)

 

赞(0) 打赏

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏