-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetinterfaces.py
More file actions
57 lines (51 loc) · 1.72 KB
/
netinterfaces.py
File metadata and controls
57 lines (51 loc) · 1.72 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
import os
import sys
import subprocess
__version__ = "0.1.0"
def get_interfaces():
platform = sys.platform
try:
if platform.startswith('linux'):
return _linux_interfaces()
if platform.startswith('win'):
return _windows_interfaces()
if platform == 'darwin':
return _mac_interfaces()
raise NotImplementedError()
except Exception:
return []
def _linux_interfaces():
net_path = "/sys/class/net"
return os.listdir(net_path) if os.path.exists(net_path) else []
def _windows_interfaces():
try:
output = subprocess.check_output(['netsh', 'interface', 'show', 'interface'],stderr=subprocess.STDOUT,text=True,encoding='utf-8',errors='ignore')
except subprocess.CalledProcessError:
return []
interfaces = []
in_table = False
for line in output.split('\n'):
line = line.strip()
if line.startswith('---'):
in_table = True
continue
if in_table and line:
parts = line.split()
if len(parts) >= 4:
interface_name = ' '.join(parts[3:])
interfaces.append(interface_name)
return interfaces
def _mac_interfaces():
try:
output = subprocess.check_output(['ifconfig'],stderr=subprocess.STDOUT,text=True,encoding='utf-8',errors='ignore')
except subprocess.CalledProcessError:
return []
interfaces = []
seen = set()
for line in output.split('\n'):
if ':' in line and not line.startswith(' '):
interface = line.split(':', 1)[0].strip()
if interface and interface not in seen:
seen.add(interface)
interfaces.append(interface)
return interfaces