Skip to content

Instantly share code, notes, and snippets.

@staaldraad
Last active August 28, 2022 05:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save staaldraad/d8db19591c52a25b2945 to your computer and use it in GitHub Desktop.
Save staaldraad/d8db19591c52a25b2945 to your computer and use it in GitHub Desktop.
Quick script to generate different formats for a given IP address.
#!/usr/bin/env python
import sys
if len(sys.argv) < 2:
print "Enter IP address as first argument: python %s 127.0.0.1"%sys.argv[0]
sys.exit(1)
ip = sys.argv[1]
ips = ip.split('.')
iph = '0x{:02X}.0x{:02X}.0x{:02X}.0x{:02X}'.format(*map(int, ips))
ipi = reduce(lambda a, b: (a << 8) + b, map(int, ips), 0)
ipo = '{:04o}.{:04o}.{:04o}.{:04o}'.format(*map(int, ips))
ipbb = '{:08b}.{:08b}.{:08b}.{:08b}'.format(*map(int, ips))
print "Long: %s"%ipi
print "Hex: %s"%iph
print "Octal: %s"%ipo
print "Binary: %s"%ipbb
for j in range(1,3):
ipb = int(ips[j])
for i in range(j,len(ips)-1):
ipb = ipb*256+int(ips[i+1])
fstring = "{:d}."*j
print "Mixed long: "+(fstring.format(*map(int, ips[:j])))+str(ipb)
print "Mixed long (hex): "+(fstring.format(*map(int, ips[:j])))+"0x{:02X}".format(ipb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment