added list_ips tool
This commit is contained in:
parent
84b98480f8
commit
a17ede229c
1 changed files with 46 additions and 0 deletions
46
list_ips
Executable file
46
list_ips
Executable 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()
|
Loading…
Reference in a new issue