about summary refs log tree commit diff
path: root/src/cli/ipconverter.py
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2024-05-05 09:59:11 -0700
committerFranck Cuny <franck@fcuny.net>2024-05-05 09:59:11 -0700
commitc54422174f2af26d0b204cc155ef9e366baa8a33 (patch)
treea2d09ace5a8b7a72b60c5ff195f2f0b2656d858c /src/cli/ipconverter.py
parentsome cleanup for CI (diff)
downloadworld-c54422174f2af26d0b204cc155ef9e366baa8a33.tar.gz
move ipconverter under `src`
Diffstat (limited to 'src/cli/ipconverter.py')
-rwxr-xr-xsrc/cli/ipconverter.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/cli/ipconverter.py b/src/cli/ipconverter.py
new file mode 100755
index 0000000..4e6906b
--- /dev/null
+++ b/src/cli/ipconverter.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+"""
+Utility to convert an IPv4 address to an int, or an int to an IPv4 address.
+"""
+
+import ipaddress
+
+import click
+
+
+@click.command()
+@click.argument("ips", nargs=-1, type=int)
+def int2ip(ips):
+    for ip in ips:
+        convip = ipaddress.ip_address(ip)
+        print(f"{ip} → {convip}")
+
+
+@click.command()
+@click.argument("ips", nargs=-1, type=str)
+def ip2int(ips):
+    for ip in ips:
+        convip = int(ipaddress.ip_address(ip))
+        print(f"{ip} → {convip}")