-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_windows_ipconfig.py
executable file
·76 lines (61 loc) · 2.06 KB
/
list_windows_ipconfig.py
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
#!/usr/bin/env python3
import sys
import subprocess as sbpr
def get_configs():
data = sbpr.Popen(
'cmd.exe /c "chcp 437 & ipconfig & chcp 65001"',
stdout=sbpr.PIPE,
stderr=sbpr.DEVNULL,
shell=True
).communicate()[0]
data = data.decode('shift-jis').replace('\r', '').replace('\t', ' ').split('\n')
configs = {}
current = "ipconfig"
for s in data:
if len(s) == 0:
continue
elif not s.startswith(' '):
current = s.rstrip(':')
configs.setdefault(current, {})
elif ':' in s:
key, value = s.split(':', 1)
key = ' '.join(key.replace('.', '').split()).strip()
configs[current][key] = value.strip()
return configs
def get_wsl_ipaddress(configs):
for k in configs.keys():
if 'WSL' in k:
if 'IPv4 Address' in configs[k]:
return configs[k]['IPv4 Address']
def current_local_connection(configs):
connect_data = None
for k in configs.keys():
if 'Ethernet adapter' in k:
if configs[k].get('Media State', '') == 'Media disconnected':
continue
elif sum(w in k for w in ['VirtualBox', 'Bluetooth', 'vEthernet']):
continue
connect_data = configs[k]
break
if 'Wi-Fi' in k:
connect_data = configs[k]
break
if connect_data is None:
return {}
return connect_data
def get_windows_ipaddress(configs):
return current_local_connection(configs).get('IPv4 Address')
def get_windows_gateway(configs):
return current_local_connection(configs).get('Default Gateway')
if __name__ == "__main__":
configs = get_configs()
argv = sys.argv[1] if len(sys.argv) >= 2 else None
if argv == 'display':
print(get_wsl_ipaddress(configs))
elif argv == 'windowshost':
print(get_windows_ipaddress(configs))
elif argv == 'windowsgateway':
print(get_windows_gateway(configs))
else:
for k, v in configs.items():
print(k, v)