/
net_api.py
executable file
·302 lines (243 loc) · 9.31 KB
/
net_api.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
from subprocess import Popen, PIPE, run, check_output
import re
from time import sleep
def card_online(netcard):
lan = Popen('ifconfig ' + netcard, shell=True, stdout=PIPE,
universal_newlines=True)
return 'inet ' in lan.stdout.read()
def defaultcard():
cmd = "netstat -rn | grep default"
nics = Popen(cmd, shell=True, stdout=PIPE, universal_newlines=True)
device = nics.stdout.readlines()
if len(device) == 0:
return None
else:
return list(filter(None, device[0].rstrip().split()))[3]
def ifWlanDisable(wificard):
cmd = "ifconfig %s list scan" % wificard
nics = Popen(cmd, shell=True, stdout=PIPE, universal_newlines=True)
return not nics.stdout.read()
def ifStatue(wificard):
cmd = "ifconfig %s" % wificard
wl = Popen(cmd, shell=True, stdout=PIPE, universal_newlines=True)
wlout = wl.stdout.read()
return "associated" in wlout
def get_ssid(wificard):
wlan = Popen('ifconfig %s | grep ssid' % wificard,
shell=True, stdout=PIPE, universal_newlines=True)
# If there are quotation marks in the string, use that as a separator,
# otherwise use the default whitespace. This is to handle ssid strings
# with spaces in them. These ssid strings will be double quoted by ifconfig
temp = wlan.stdout.readlines()[0].rstrip()
if '"' in temp:
out = temp.split('"')[1]
else:
out = temp.split()[1]
return out
def nics_list():
notnics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|" \
r"faith|ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)"
nics = Popen(
'ifconfig -l',
shell=True,
stdout=PIPE,
universal_newlines=True
).stdout.read().strip()
return sorted(re.sub(notnics_regex, '', nics).strip().split())
def ifcardconnected(netcard):
wifi = Popen('ifconfig ' + netcard, shell=True, stdout=PIPE,
universal_newlines=True)
return 'status: active' in wifi.stdout.read()
def barpercent(sn):
sig = int(sn.partition(':')[0])
noise = int(sn.partition(':')[2])
return int((sig - noise) * 4)
def network_service_state():
return False
def networkdictionary():
nlist = nics_list()
maindictionary = {
'service': network_service_state(),
'default': defaultcard()
}
cards = {}
for card in nlist:
if 'wlan' in card:
scanv = f"ifconfig {card} list scan | grep -va BSSID"
wifi = Popen(scanv, shell=True, stdout=PIPE,
universal_newlines=True)
connectioninfo = {}
for line in wifi.stdout:
# don't sort empty ssid
# Window, MacOS and Linux does not show does
if line.startswith(" " * 5):
continue
ssid = line[:33].strip()
info = line[:83][33:].strip().split()
percentage = barpercent(info[3])
# if ssid exist and percentage is higher keep it
# else add the new one if percentage is higher
if ssid in connectioninfo:
if connectioninfo[ssid][4] > percentage:
continue
info[3] = percentage
info.insert(0, ssid)
# append left over
info.append(line[83:].strip())
connectioninfo[ssid] = info
if ifWlanDisable(card):
connectionstat = {
"connection": "Disabled",
"ssid": None,
}
elif not ifStatue(card):
connectionstat = {
"connection": "Disconnected",
"ssid": None,
}
else:
ssid = get_ssid(card)
connectionstat = {
"connection": "Connected",
"ssid": ssid,
}
seconddictionary = {
'state': connectionstat,
'info': connectioninfo
}
else:
if card_online(card):
connectionstat = {"connection": "Connected"}
elif ifcardconnected(card):
connectionstat = {"connection": "Disconnected"}
else:
connectionstat = {"connection": "Unplug"}
seconddictionary = {'state': connectionstat, 'info': None}
cards[card] = seconddictionary
maindictionary['cards'] = cards
return maindictionary
def connectionStatus(card):
if card is None:
netstate = "Network card is not enabled"
elif 'wlan' in card:
if not ifWlanDisable(card) and ifStatue(card):
cmd1 = "ifconfig %s | grep ssid" % card
cmd2 = "ifconfig %s | grep 'inet '" % card
out1 = Popen(cmd1, shell=True, stdout=PIPE,
universal_newlines=True)
out2 = Popen(cmd2, shell=True, stdout=PIPE,
universal_newlines=True)
line1 = out1.stdout.read().strip()
line2 = out2.stdout.read().strip()
netstate = line1 + '\n' + subnetHexToDec(line2)
else:
netstate = "WiFi %s not connected" % card
else:
cmd = "ifconfig %s | grep 'inet '" % card
out = Popen(cmd, shell=True, stdout=PIPE,
universal_newlines=True)
line = out.stdout.read().strip()
netstate = subnetHexToDec(line)
return netstate
def switch_default(nic):
nics = nics_list()
nics.remove(nic)
if not nics:
return
for card in nics:
nic_info = Popen(
['ifconfig', card],
stdout=PIPE,
close_fds=True,
universal_newlines=True
).stdout.read()
if 'status: active' in nic_info or 'status: associated' in nic_info:
if 'inet ' in nic_info or 'inet6' in nic_info:
run(f'service dhclient restart {card}', shell=True)
break
return
def stopallnetwork():
run('service netif stop', shell=True)
def startallnetwork():
run('service netif start', shell=True)
def stopnetworkcard(netcard):
run(f'service netif stop {netcard}', shell=True)
switch_default(netcard)
def restart_card_network(netcard):
run(f'service netif restart {netcard}', shell=True)
def restart_rounting_and_dhcp(netcard):
run('service routing restart', shell=True)
sleep(1)
run(f'service dhclient restart {netcard}', shell=True)
def start_static_network(netcard, inet, netmask):
run(f'ifconfig {netcard} inet {inet} netmask {netmask}', shell=True)
sleep(1)
run('service routing restart', shell=True)
def startnetworkcard(netcard):
run(f'service netif start {netcard}', shell=True)
sleep(1)
run('service routing restart', shell=True)
run(f'service dhclient start {netcard}', shell=True)
def wifiDisconnection(wificard):
run(f'ifconfig {wificard} down', shell=True)
run(f"ifconfig {wificard} ssid 'none'", shell=True)
run(f'ifconfig {wificard} up', shell=True)
def disableWifi(wificard):
run(f'ifconfig {wificard} down', shell=True)
def enableWifi(wificard):
run(f'ifconfig {wificard} up', shell=True)
run(f'ifconfig {wificard} up scan', shell=True)
def connectToSsid(name, wificard):
run('killall wpa_supplicant', shell=True)
# service
sleep(0.5)
run(f"ifconfig {wificard} ssid '{name}'", shell=True)
sleep(0.5)
wpa_supplicant = run(
f'wpa_supplicant -B -i {wificard} -c /etc/wpa_supplicant.conf',
shell=True
)
return wpa_supplicant.returncode == 0
def subnetHexToDec(ifconfigstring):
snethex = re.search('0x.{8}', ifconfigstring).group(0)[2:]
snethexlist = re.findall('..', snethex)
snetdec = ".".join(str(int(li, 16)) for li in snethexlist)
outputline = ifconfigstring.replace(re.search('0x.{8}', ifconfigstring).group(0), snetdec)
return outputline
def get_ssid_wpa_supplicant_config(ssid):
cmd = f"""grep -A 3 'ssid="{ssid}"' /etc/wpa_supplicant.conf"""
out = Popen(cmd, shell=True, stdout=PIPE, universal_newlines=True)
return out.stdout.read().splitlines()
def delete_ssid_wpa_supplicant_config(ssid):
cmd = f"""awk '/sid="{ssid}"/ """ \
"""{print NR-2 "," NR+4 "d"}' """ \
"""/etc/wpa_supplicant.conf | sed -f - /etc/wpa_supplicant.conf"""
out = Popen(cmd, shell=True, stdout=PIPE, universal_newlines=True)
left_over = out.stdout.read()
wpa_supplicant_conf = open('/etc/wpa_supplicant.conf', 'w')
wpa_supplicant_conf.writelines(left_over)
wpa_supplicant_conf.close()
def nic_status(card):
out = Popen(
f'ifconfig {card} | grep status:',
shell=True, stdout=PIPE,
universal_newlines=True
)
return out.stdout.read().split(':')[1].strip()
def start_dhcp(wificard):
run(f'dhclient {wificard}', shell=True)
def wait_inet(card):
IPREGEX = r'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
status = 'associated' if 'wlan' in card else 'active'
while nic_status(card) != status:
sleep(0.1)
print(nic_status(card))
while True:
ifcmd = f"ifconfig -f inet:dotted {card}"
ifoutput = check_output(ifcmd.split(" "), universal_newlines=True)
print(ifoutput)
re_ip = re.search(fr'inet {IPREGEX}', ifoutput)
if re_ip and '0.0.0.0' not in re_ip.group():
print(re_ip)
break