Browse Source

replace int-to-ip4 and ip4-to-int with base256 single script that detects what we want from it automatically

Von Random 2 years ago
parent
commit
fa6c4120b7
3 changed files with 30 additions and 25 deletions
  1. 30
    0
      base256.py
  2. 0
    14
      int-to-ip4.py
  3. 0
    11
      ip4-to-int.py

+ 30
- 0
base256.py View File

@@ -0,0 +1,30 @@
1
+#!/usr/bin/env python
2
+"""A very simple script to convert an integer into an IPv4 address.
3
+   Expects an integer as the only argument."""
4
+from sys import argv
5
+
6
+def from_base256(number):
7
+    num = int(number)
8
+    addr = list()
9
+    for e in range(3, -1, -1):
10
+        multiplier = 256 ** e
11
+        octet = num // multiplier
12
+        addr.append(str(octet))
13
+        num = num % multiplier
14
+
15
+    return '.'.join(addr)
16
+
17
+def to_base256(addr):
18
+    addr = addr.split('.')
19
+    num = 0
20
+    for e in range(3, -1, -1):
21
+        num += int(addr[3-e]) * 256 ** e
22
+
23
+    return str(num)
24
+
25
+try:
26
+    result = from_base256(argv[1])
27
+except ValueError:
28
+    result = to_base256(argv[1])
29
+
30
+print(result)

+ 0
- 14
int-to-ip4.py View File

@@ -1,14 +0,0 @@
1
-#!/usr/bin/env python
2
-"""A very simple script to convert an integer into an IPv4 address.
3
-   Expects an integer as the only argument."""
4
-from sys import argv
5
-
6
-addr = list()
7
-num = int(argv[1])
8
-for e in range(3, -1, -1):
9
-    multiplier = 256 ** e
10
-    octet = num // multiplier
11
-    addr.append(str(octet))
12
-    num = num % multiplier
13
-
14
-print('.'.join(addr))

+ 0
- 11
ip4-to-int.py View File

@@ -1,11 +0,0 @@
1
-#!/usr/bin/env python
2
-"""A very simple script to convert an IPv4 address into an integer.
3
-   Expects a single IPv4 address as the only argument."""
4
-from sys import argv
5
-
6
-addr = argv[1].split('.')
7
-num = 0
8
-for e in range(3, -1, -1):
9
-    num += int(addr[3-e]) * 256 ** e
10
-
11
-print(num)

Loading…
Cancel
Save