def mod_network(ip_addr, unmod_nets, ip_map): ip_addr = IPAddress(ip_addr) # Looping through the list of unmod_nets and checking if IP is in network, if True then break for net in unmod_nets: in_network = ip_addr in net if in_network: break # in_network would be False here if IP address wasn't in any of the unmod_networks if not in_network: return ip_addr.format() # Mapping current IP to mod IP net = ip_map[net] # Converting IPs to binary, only keeping the host and network bits respectively ip_addr = ip_addr.bits().replace('.', '')[net.prefixlen:] net = net.network.bits().replace('.', '')[:net.prefixlen] # Modified IP address ip_addr = net + ip_addr octet = 8 ip_addr = '.'.join([str(int(ip_addr[i:i + octet], 2)) for i in range(0, len(ip_addr), octet)]) return ip_addr
def test_ipaddress_v4(): ip = IPAddress('192.0.2.1') assert ip.version == 4 assert repr(ip) == "IPAddress('192.0.2.1')" assert str(ip) == '192.0.2.1' assert ip.format() == '192.0.2.1' assert int(ip) == 3221225985 assert hex(ip) == '0xc0000201' assert ip.bin == '0b11000000000000000000001000000001' assert ip.bits() == '11000000.00000000.00000010.00000001' assert ip.words == (192, 0, 2, 1)
def test_ipaddress_v6(): ip = IPAddress('fe80::dead:beef') assert ip.version == 6 assert repr(ip) == "IPAddress('fe80::dead:beef')" assert str(ip) == 'fe80::dead:beef' assert ip.format() == 'fe80::dead:beef' assert int(ip) == 338288524927261089654018896845083623151 assert hex(ip) == '0xfe8000000000000000000000deadbeef' assert ip.bin == '0b11111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000011011110101011011011111011101111' assert ip.bits() == '1111111010000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1101111010101101:1011111011101111' assert ip.words == (65152, 0, 0, 0, 0, 0, 57005, 48879)
def test_ipaddress_v6(): ip = IPAddress('fe80::dead:beef') assert ip.version == 6 assert repr(ip) == "IPAddress('fe80::dead:beef')" assert str(ip) == 'fe80::dead:beef' assert ip.format() == 'fe80::dead:beef' assert int(ip) == 338288524927261089654018896845083623151 assert hex(ip) == '0xfe8000000000000000000000deadbeef' assert ip.bin == '0b11111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000011011110101011011011111011101111' assert ip.bits( ) == '1111111010000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1101111010101101:1011111011101111' assert ip.words == (65152, 0, 0, 0, 0, 0, 57005, 48879)
""" IPAddress from netaddr in Python Please Subscribe to Asim Code. YouTube Channel: Asim Code https://youtu.be/gMtBiNjgeKE """ from netaddr import IPAddress ip = IPAddress('192.168.56.1') print(ip.version) print(ip.bin) print(ip.bits()) print(ip.words) print(ip.is_unicast()) print(ip.is_link_local())
import netaddr import socket from netaddr import IPAddress # Allow user input for ip print("what is the ip?") addr = input() # reverse DNS dns = socket.gethostbyaddr(addr) # Define ip ip = IPAddress(addr) # Print info print("IP version -", ip.version) print("private -", ip.is_private()) print("unicast -", ip.is_unicast()) print("multicast -", ip.is_multicast()) print("IP in bits -", ip.bits()) print("reverse dns -", dns)
def getBits(self, netmask): mask = IPAddress(netmask) return mask.bits().count('1')
def getBits(self, netmask): mask = IPAddress(netmask) return mask.bits().count("1")