Python: IP地址子网计算器

import ipaddress


def ipadd_calc(ip_net):
    try:
        net = ipaddress.ip_network(ip_net, strict=False)
        result = '是否是私有地址:%s '  % str(net.is_private)
        result += '\n所属IP子网:%s' % net.with_netmask
        result += '\n所属IP子网:%s/%s (前缀表达法)' % (net.network_address, net.prefixlen)
        result += '\n网络号:%s ' % str(net.network_address)
        result += '\n广播地址:%s ' % str(net.broadcast_address)
        sum_C = int(net.num_addresses/256)
        if sum_C >= 1:
            result += '\n包含:%s个C' % sum_C
        result += '\nIP地址总数:%s ' % str(net.num_addresses)
        result += '\n可用IP地址总数:%s' % str(len([x for x in net.hosts()]))
        result += '\n起始可用IP地址:%s ' % str([x for x in net.hosts()][0])
        result += '\n最后可用IP地址:%s ' % str([x for x in net.hosts()][-1])
        result += '\n可用IP地址范围:%s ' % str([x for x in net.hosts()][0]) + ' ~ ' + str([x for x in net.hosts()][-1])
        result += '\n子网前缀长度:%d' % net.prefixlen
        result += '\n掩码地址:%s ' % str(net.netmask)
        result += '\n反掩码地址:%s ' % str(net.hostmask)
        return result
    except ValueError:
        return('您输入格式有误,请检查!')

if __name__ == '__main__':
    ip_net = '10.10.200.255/255.255.255.0'
    print(ipadd_calc(ip_net))

  脚本出处:

https://www.yjsec.com/2021/07/28/python-ip地址子网计算器

  

posted @ 2021-09-02 16:42  Security  阅读(668)  评论(0编辑  收藏  举报