replace int-to-ip4 and ip4-to-int with base256 single script that detects what we want from it automatically
This commit is contained in:
parent
f04fa8ba9e
commit
fa6c4120b7
3 changed files with 30 additions and 25 deletions
30
base256.py
Executable file
30
base256.py
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)
|
||||||
|
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 str(num)
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = from_base256(argv[1])
|
||||||
|
except ValueError:
|
||||||
|
result = to_base256(argv[1])
|
||||||
|
|
||||||
|
print(result)
|
|
@ -1,14 +0,0 @@
|
||||||
#!/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
|
|
||||||
|
|
||||||
addr = list()
|
|
||||||
num = int(argv[1])
|
|
||||||
for e in range(3, -1, -1):
|
|
||||||
multiplier = 256 ** e
|
|
||||||
octet = num // multiplier
|
|
||||||
addr.append(str(octet))
|
|
||||||
num = num % multiplier
|
|
||||||
|
|
||||||
print('.'.join(addr))
|
|
|
@ -1,11 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
"""A very simple script to convert an IPv4 address into an integer.
|
|
||||||
Expects a single IPv4 address as the only argument."""
|
|
||||||
from sys import argv
|
|
||||||
|
|
||||||
addr = argv[1].split('.')
|
|
||||||
num = 0
|
|
||||||
for e in range(3, -1, -1):
|
|
||||||
num += int(addr[3-e]) * 256 ** e
|
|
||||||
|
|
||||||
print(num)
|
|
Loading…
Reference in a new issue