added scripts to convert ipv4 into an integer and back, only python is required, supports both 2.7 and 3, does not use anything fance, like ipaddr or ipaddress
This commit is contained in:
parent
03ecf62dd1
commit
eb3a895bcf
2 changed files with 25 additions and 0 deletions
14
int-to-ip4.py
Executable file
14
int-to-ip4.py
Executable file
|
@ -0,0 +1,14 @@
|
|||
#!/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 -= octet * multiplier
|
||||
|
||||
print('.'.join(addr))
|
11
ip4-to-int.py
Executable file
11
ip4-to-int.py
Executable file
|
@ -0,0 +1,11 @@
|
|||
#!/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