minor tweaks and new tools
This commit is contained in:
parent
5a634016cf
commit
e9243344a8
5 changed files with 60 additions and 4 deletions
30
iphex
Executable file
30
iphex
Executable file
|
@ -0,0 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
"""A very simple script to convert an integer into an IPv4 address.
|
||||
Expects an integer as the only argument."""
|
||||
from sys import argv
|
||||
|
||||
def from_base256(number):
|
||||
num = int(number, 16)
|
||||
addr = list()
|
||||
for e in range(3, -1, -1):
|
||||
multiplier = 256 ** e
|
||||
octet = num // multiplier
|
||||
addr.append(str(octet))
|
||||
num = num % multiplier
|
||||
|
||||
return '.'.join(addr)
|
||||
|
||||
def to_base256(addr):
|
||||
addr = addr.split('.')
|
||||
num = 0
|
||||
for e in range(3, -1, -1):
|
||||
num += int(addr[3-e]) * 256 ** e
|
||||
|
||||
return '{0:08x}'.format(num)
|
||||
|
||||
try:
|
||||
result = from_base256(argv[1])
|
||||
except ValueError:
|
||||
result = to_base256(argv[1])
|
||||
|
||||
print(result)
|
Loading…
Add table
Add a link
Reference in a new issue