From ca90979e8fb8ff60a5f0f1fb107cef06a72a5766 Mon Sep 17 00:00:00 2001 From: ranomier Date: Sun, 1 Dec 2024 03:10:02 +0100 Subject: [PATCH] added list_ips tool --- list_ips | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 list_ips diff --git a/list_ips b/list_ips new file mode 100755 index 0000000..14b079d --- /dev/null +++ b/list_ips @@ -0,0 +1,46 @@ +#!/usr/bin/env python +#docstring=dirty tool to simplify the output of ip dramaticly +import subprocess as sp +import json + +CFG = { + "iface_allow_list": ["eth", "enp", "wlp", "wlan"], + "show_ip4": True, + "show_ip6": False, +} + +def filter_interfaces(): + output = json.loads( + sp.run(["ip", "-j", "addr", "show"], + text=True, + capture_output=True).stdout) + filtered_list=[] + for interface in output: + for allowed in CFG["iface_allow_list"]: + if allowed in interface["ifname"]: + filtered_list.append(interface) + return filtered_list + +def ip_string(interface): + conn_list = "" + for conn in interface["addr_info"]: + if conn["family"] == "inet" and CFG["show_ip4"] or conn["family"] == "inet6" and CFG["show_ip6"]: + conn_list += (conn["local"] + "/" + str(conn["prefixlen"])) + ", " + return conn_list.rstrip(", ") + +def pretty(interfaces): + longest_member = 0 + for interface in interfaces: + if len(interface["ifname"]) > longest_member: + longest_member = len(interface["ifname"]) + + for interface in interfaces: + len_diff = longest_member - len(interface["ifname"]) + print(interface["ifname"], ": ", " " * len_diff, ip_string(interface), sep="") + + + +def main(): + pretty(filter_interfaces()) +if __name__ == "__main__": + main()