import ipaddress def ipadd_calc(ip_net): try: net = ipaddress.ip_network(ip_net, strict=False) result = '是否是私有地址:%s ' % str(net.is_private) result += ' 所属IP子网:%s' % net.with_netmask result += ' 所属IP子网:%s/%s (前缀表达法)' % (net.network_address, net.prefixlen) result += ' 网络号:%s ' % str(net.network_address) result += ' 广播地址:%s ' % str(net.broadcast_address) sum_C = int(net.num_addresses/256) if sum_C >= 1: result += ' 包含:%s个C' % sum_C result += ' IP地址总数:%s ' % str(net.num_addresses) result += ' 可用IP地址总数:%s' % str(len([x for x in net.hosts()])) result += ' 起始可用IP地址:%s ' % str([x for x in net.hosts()][0]) result += ' 最后可用IP地址:%s ' % str([x for x in net.hosts()][-1]) result += ' 可用IP地址范围:%s ' % str([x for x in net.hosts()][0]) + ' ~ ' + str([x for x in net.hosts()][-1]) result += ' 子网前缀长度:%d' % net.prefixlen result += ' 掩码地址:%s ' % str(net.netmask) result += ' 反掩码地址:%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地址子网计算器