46 lines
1.3 KiB
Python
Executable file
46 lines
1.3 KiB
Python
Executable file
#!/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()
|