-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-network-info.py
More file actions
128 lines (110 loc) · 4 KB
/
simple-network-info.py
File metadata and controls
128 lines (110 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Show local network interfaces, IP addresses, and MAC addresses."""
import re
import sys
import socket
import platform
import argparse
import subprocess
def get_interfaces_windows() -> list[dict]:
import ctypes
oem_cp = f"cp{ctypes.windll.kernel32.GetOEMCP()}"
raw = subprocess.check_output(["ipconfig", "/all"], stderr=subprocess.DEVNULL)
out = raw.decode(oem_cp, errors="replace")
interfaces: list[dict] = []
current: dict | None = None
# Locale-agnostic patterns: detect by value format, not by label text.
# IPs with a parenthesised qualifier are the adapter's own addresses.
re_ipv4 = re.compile(r":\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*\(")
re_ipv6 = re.compile(r":\s*([0-9a-fA-F:]+(?:%\d+)?)\s*\(")
re_mac = re.compile(r":\s*([0-9A-Fa-f]{2}(?:-[0-9A-Fa-f]{2}){5})\s*$")
for line in out.splitlines():
if not line.strip():
continue
if not line[0].isspace():
if current is not None:
interfaces.append(current)
current = {
"name": line.strip().rstrip(":"),
"ipv4": [],
"ipv6": [],
"mac": "",
}
elif current is not None:
m = re_mac.search(line)
if m:
current["mac"] = m.group(1)
continue
m = re_ipv4.search(line)
if m:
current["ipv4"].append(m.group(1))
continue
m = re_ipv6.search(line)
if m and ":" in m.group(1):
current["ipv6"].append(m.group(1).split("%")[0])
if current is not None:
interfaces.append(current)
return interfaces
def get_hostname_ips() -> list[str]:
try:
host = socket.gethostname()
return socket.gethostbyname_ex(host)[2]
except Exception:
return []
def get_external_ip() -> str:
try:
with socket.create_connection(("8.8.8.8", 80), timeout=3) as s:
return s.getsockname()[0]
except Exception:
return "n/a"
def get_interfaces_unix() -> list[dict]:
out = subprocess.check_output(["ip", "addr"], text=True, stderr=subprocess.DEVNULL)
interfaces: list[dict] = []
current: dict = {}
for line in out.splitlines():
m_iface = line.split(":")
if line and line[0].isdigit() and len(m_iface) >= 2:
if current:
interfaces.append(current)
current = {"name": m_iface[1].strip(), "ipv4": [], "ipv6": [], "mac": ""}
elif "inet " in line:
parts = line.split()
current.setdefault("ipv4", []).append(parts[1])
elif "inet6 " in line:
parts = line.split()
current.setdefault("ipv6", []).append(parts[1])
elif "link/ether" in line:
current["mac"] = line.split()[1]
if current:
interfaces.append(current)
return interfaces
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Show local network interfaces and IPs."
)
parser.add_argument(
"--all", action="store_true", help="Include interfaces with no IP assigned"
)
args = parser.parse_args()
try:
if platform.system() == "Windows":
ifaces = get_interfaces_windows()
else:
ifaces = get_interfaces_unix()
except Exception as e:
print(f"Error reading interfaces: {e}", file=sys.stderr)
sys.exit(1)
hostname = socket.gethostname()
outbound = get_external_ip()
print(f" {'Hostname':<16} {hostname}")
print(f" {'Outbound IP':<16} {outbound}\n")
for iface in ifaces:
has_ip = iface.get("ipv4") or iface.get("ipv6")
if not has_ip and not args.all:
continue
print(f" {iface['name']}")
if iface.get("mac"):
print(f" {'MAC':<12} {iface['mac']}")
for ip in iface.get("ipv4", []):
print(f" {'IPv4':<12} {ip}")
for ip in iface.get("ipv6", []):
print(f" {'IPv6':<12} {ip}")