File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments