added list_ips tool

This commit is contained in:
Ranomier 2024-12-01 03:10:02 +01:00
parent 84b98480f8
commit a17ede229c

46
list_ips Executable file
View file

@ -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()