-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWifi_file.py
More file actions
111 lines (90 loc) · 3 KB
/
Wifi_file.py
File metadata and controls
111 lines (90 loc) · 3 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
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
import os
import pickle
import time
class network():
def __init__(self, SSID, password):
self.SSID = str(SSID)
self.password = password
self.priority = 0
self.number = None
def __repr__(self):
return self.SSID
saved_networks = []
def wifi_find():
try:
a = os.popen("sudo iwlist wlan0 scan | perl -nle '/ESSID:(.*)$/ && print $1'").read()
except:
time.scleep(5)
a = os.popen("sudo iwlist wlan0 scan | perl -nle '/ESSID:(.*)$/ && print $1'").read()
network_list = []
capture = False
network = ''
for i in range(len(a)):
if a[i] == '"' and capture == True:
capture = not capture
if network[0:2] != '\\' and network[0:2] != '\\x' and network not in network_list:
network_list.append(network)
elif a[i] == '"' and capture == False:
capture = not capture
network = ''
elif capture == True:
network += a[i]
return network_list
def scan():
wifi_list = wifi_find()
display_list = []
for networks in wifi_list:
display_list.append(network(networks, 'unknown'))
return display_list
def dump(saved_networks):
network_file = open('/home/pi/touchscreen-main/Saved_wifi', 'wb')
pickle.dump(saved_networks, network_file)
network_file.close()
def save_get():
network_file = open('/home/pi/touchscreen-main/Saved_wifi', 'rb')
saved_networks = pickle.load(network_file)
network_file.close()
return saved_networks
def save_conf(*args):
candidate_network = args[0]
os.popen('wpa_cli list_networks').read()
#insert stuff to find all networks then delete them all from wpa_cli
a = 'wpa_cli set_network 0 ssid '+"'"+'"'+candidate_network.SSID+'"'+"'"
os.popen(a).read()
b = 'wpa_cli set_network 0 psk '+"'"+'"'+candidate_network.password+'"'+"'"
os.popen(b).read()
os.popen('wpa_cli save_conf').read()
os.popen('wpa_cli -i wlan0 reconfigure').read()
time.sleep(2)
c = connect()
if c == True:
saved_networks.append(candidate_network)
if args[1] == 1:
dump(saved_networks)
return True
else:
return False
#os.system('wpa_passphrase '+ str(candidate_network.SSID)+' ' + str(candidate_network.password) +' >> /etc/wpa_supplicant/wpa_supplicant.conf')
'''
def change_priority(network):
global saved_networks
for i in saved_networks:
if i == network:
i.priority = 1
else:
i.priority = 0
'''
def connect():
os.system('sudo rfkill block wifi')
os.system('sudo rfkill unblock wifi')
time.sleep(8)
a = os.popen('ifconfig wlan0').read()
if 'inet' in a:
return True
def disconnect():
os.system('sudo rfkill block wifi')
#home = network('BT-5HAC5X', 'TpUrbaEcGg9gFL')
#phone = network('AndroidAP0A38','pgzw6963')
#connect()
#wifi_find()
#dump([])