• 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)
    

     输出:

    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)
    
  • 相关阅读:
    luogu4182 [USACO18JAN] Lifeguards P (单调队列优化dp)
    bzoj3277 串 (后缀数组+二分答案+ST表)
    [模板]后缀数组
    bzoj4361 isn (dp+树状数组+容斥)
    luogu4187 [USACO18JAN]Stamp Painting (dp)
    [USACO15DEC]高低卡(白金)High Card Low Card (Platinum)
    USACO环绕岛屿Surround the Islands 并查集 枚举暴力
    5.7 ~ 5.12 刷题列表
    5.4 ~ 5.6 刷题记录
    HNOI2012 永无乡 无旋Treap
  • 原文地址:https://www.cnblogs.com/andy9468/p/11311088.html
Copyright © 2020-2023  润新知