IP Conversion

IP Conversion with Python

Convert IPv4 from dotted decimal to decimal
import socket, struct
ipaddr = raw_input("Enter IP Address ")
print struct.unpack("!I", socket.inet_aton(ipaddr))[0]

Sample Run:

Enter IP Address
Convert IPv4 from dotted decimal to hex
import string
import binascii
import socket
ip = raw_input("Enter IP address in Dotted Decimal ")
iptodec = socket.inet_aton(ip)
iptohex = binascii.hexlify(socket.inet_aton(ip))
print iptohex

Sample Run:

Enter IP address in Dotted Decimal
Convert IPv4 from hex to dotted decimal
import socket
import struct
hexaddr = raw_input("Enter Hex Address for V4 ")
bytes = ["".join(x) for x in zip(*[iter(hexaddr)]*2)]
bytes = [int(x, 16) for x in bytes]
print ".".join(str(x) for x in (bytes))

Sample Run:

Enter Hex Address for V4 0a010101
Convert IPv6 to Decimal
import netaddr
ipv6addr = raw_input("Enter IPv6 Address ")
print int(netaddr.IPAddress(ipv6addr))

Sample Run:

Enter IPv6 Address 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Convert Decimal to IPv6
import netaddr
ipv6addrdec = long(raw_input("Enter Decimal Number for IPv6 addr "))
print str(netaddr.IPAddress(ipv6addrdec))

Sample Run:

Enter Decimal Number for IPv6 addr 42540766452641154071740215577757643572

451 thoughts on “IP Conversion”

Leave a Reply to RichardFak Cancel reply

Your email address will not be published. Required fields are marked *