From eb3a895bcfd60076854ca5886b0f327893b6dc78 Mon Sep 17 00:00:00 2001 From: Von Random Date: Sun, 13 Mar 2016 02:05:47 +0300 Subject: [PATCH] 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 --- int-to-ip4.py | 14 ++++++++++++++ ip4-to-int.py | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100755 int-to-ip4.py create mode 100755 ip4-to-int.py diff --git a/int-to-ip4.py b/int-to-ip4.py new file mode 100755 index 0000000..dd91b4c --- /dev/null +++ b/int-to-ip4.py @@ -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)) diff --git a/ip4-to-int.py b/ip4-to-int.py new file mode 100755 index 0000000..a23a9a6 --- /dev/null +++ b/ip4-to-int.py @@ -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)