|
1 | 1 | import psutil
|
2 | 2 | from PyQt5 import QtGui
|
3 | 3 | import time
|
| 4 | +import platform |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def get_interfaces(): # Get the interfaces
|
| 8 | + actual_system = platform.system() |
| 9 | + if actual_system == 'Windows': |
| 10 | + ports = get_interfaces_windows() |
| 11 | + return ports |
| 12 | + elif actual_system == 'Darwin': |
| 13 | + ports = get_interfaces_mac() |
| 14 | + return ports |
| 15 | + else: |
| 16 | + return 'Unsupported OS' |
| 17 | + |
| 18 | +def get_interfaces_mac(): |
7 | 19 | ports = {}
|
8 | 20 | for intf, mac_addr in psutil.net_if_addrs().items(): # psutil for system information
|
9 | 21 | for i in mac_addr:
|
10 | 22 | if i.family == psutil.AF_LINK: # if the family is a link - Ethernet
|
11 | 23 | ports[intf] = i.address # add the interface and the mac address to the dictionary
|
12 | 24 |
|
13 |
| - #keys_to_remove = [x for x in ports if "Ethernet" not in x or "vEthernet" in x or "en0" in x or "en1" in x] |
14 |
| - # -------------------------------------------------- debug -------------------------------------------------- # |
15 |
| - # Explanation: If the interface is not Ethernet, I need to test with the Wi-Fi interface |
16 |
| - # if 'Wi-Fi' in keys_to_remove: |
17 |
| - # keys_to_remove.remove('Wi-Fi') |
18 |
| - # keys_to_remove.remove('vEthernet (WSL (Hyper-V firewall))') |
19 |
| - # -------------------------------------------------- debug -------------------------------------------------- # |
20 |
| - #for key in keys_to_remove: # remove the keys that are not Ethernet |
21 |
| - # del ports[key] |
22 |
| - |
23 |
| - if 'lo0' not in ports: |
| 25 | + if 'lo0' not in ports or len(ports) < 2: |
24 | 26 | ports['lo0'] = '00:00:00:00:00:00' # Loopback interfaces do not have a MAC address
|
25 | 27 |
|
26 | 28 | return ports
|
27 | 29 |
|
| 30 | +def get_interfaces_windows(): |
| 31 | + ports = {} |
| 32 | + for intf, mac_addr in psutil.net_if_addrs().items(): # psutil for system information |
| 33 | + for i in mac_addr: |
| 34 | + if i.family == psutil.AF_LINK: # if the family is a link - Ethernet |
| 35 | + ports[intf] = i.address # add the interface and the mac address to the dictionary |
| 36 | + |
| 37 | + keys_to_remove = [x for x in ports if "Ethernet" not in x or "vEthernet" in x or "en0" in x or "en1" in x] |
| 38 | + |
| 39 | + if 'Wi-Fi' in keys_to_remove: |
| 40 | + keys_to_remove.remove('Wi-Fi') |
| 41 | + keys_to_remove.remove('vEthernet (WSL (Hyper-V firewall))') |
| 42 | + |
| 43 | + for key in keys_to_remove: # remove the keys that are not Ethernet |
| 44 | + del ports[key] |
| 45 | + |
| 46 | + return ports |
| 47 | + |
28 | 48 |
|
29 | 49 | def update_table_view(table_view, stats): # Update the table view
|
30 | 50 | model = QtGui.QStandardItemModel()
|
|
0 commit comments