Skip to content

Commit 87932ad

Browse files
committed
feat: added network_adapter_information.py file witch fetches network information including computer name, IP and MAC addresses
1 parent 7e5ef6b commit 87932ad

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

network_adapter_information.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import socket
2+
import platform
3+
import psutil
4+
5+
6+
def get_network_info():
7+
"""Fetches network information including computer name, IP and MAC addresses."""
8+
# Initialize network information dictionary
9+
network_info = {
10+
"Computer Name": platform.node(), # Get the computer's hostname
11+
"Adapters": [] # List to store network adapters' info
12+
}
13+
14+
# Loop through network interfaces and their addresses
15+
for interface_name, interface_addresses in psutil.net_if_addrs().items():
16+
adapter_info = {
17+
"Adapter": interface_name,
18+
"IP Address": None,
19+
"MAC Address": None
20+
}
21+
22+
# Get MAC and IP addresses for each adapter
23+
for address in interface_addresses:
24+
if address.family == socket.AF_LINK:
25+
adapter_info["MAC Address"] = address.address
26+
elif address.family == socket.AF_INET:
27+
adapter_info["IP Address"] = address.address
28+
29+
# Add adapter info if both IP and MAC addresses are found
30+
if adapter_info["IP Address"] and adapter_info["MAC Address"]:
31+
network_info["Adapters"].append(adapter_info)
32+
33+
return network_info

0 commit comments

Comments
 (0)