-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset_network
executable file
·133 lines (112 loc) · 3.22 KB
/
set_network
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
#!/usr/bin/python
import os
import subprocess
from subprocess import CalledProcessError
import time
import sys
class wpa_templates:
START = '''''
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
update_config=1
ap_scan=1
'''
OPEN = '''
network={
ssid="%s"
%s
key_mgmt=NONE
}'''
WEP = '''
network={
ssid="%s"
%s
key_mgmt=NONE
group=WEP104 WEP40
wep_key0="%s"
}
'''
WPAPSK = '''
network={
ssid="%s"
%s
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP WEP104 WEP40
eap=TTLS PEAP TLS
psk="%s"
}
'''
WPAEAP = '''
network={
ssid="%s"
%s
key_mgmt=WPA-EAP
pairwise=CCMP TKIP
group=CCMP TKIP WEP104 WEP40
eap=TTLS PEAP TLS
identity="%s"
password="%s"
phase1="peaplabel=0"
}
'''
#worst case, break this out.
MAX = '''
network={
ssid="%s"
%s
key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
pairwise=CCMP TKIP
group=CCMP TKIP WEP104 WEP40
psk="%s"
eap=TTLS PEAP TLS
identity="%s"
password="%s"
phase1="peaplabel=0"
}
'''
def setNetwork(network_conf, ssid):
if not os.path.isfile('/etc/wpa_supplicant/wpa_supplicant.conf.original'):
subprocess.call("cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.original", shell=True)
wpa_supplicant = open('/etc/wpa_supplicant/wpa_supplicant.conf','a')
#Will not take care of duplicates at the moment.
wpa_supplicant.write(network_conf)
wpa_supplicant.close();
print "Initiating connection to " + ssid + ". Please wait..."
try:
if int(subprocess.check_output("systemctl status wpa_supplicant | grep 'active (running)' | wc -l", shell=True)) == 0:
subprocess.call("systemctl stop hostapd &> /dev/null", shell=True)
subprocess.call("systemctl start wpa_supplicant &> /dev/null", shell=True)
time.sleep(10)
else:
subprocess.call("wpa_cli reconfigure &> /dev/null && sleep 2", shell=True)
network_count = int(subprocess.check_output('wpa_cli list_networks | wc -l', shell=True))
subprocess.call("wpa_cli select_network " + str(network_count - 2 - 1) + " &> /dev/null", shell=True)
time.sleep(5)
ifarray = subprocess.check_output("wpa_cli ifname", shell=True).split()
subprocess.call("udhcpc -i " + ifarray[len(ifarray)-1] + " -n &> /dev/null", shell=True)
except Exception as e:
print e
print "Sorry. Could not get an IP address."
else:
print "Attempting to enable network access, please check 'wpa_cli status' after a minute to confirm."
def main():
ssid = sys.argv[1]
for x in xrange(2, len(sys.argv)):
ssid = ssid + " " + sys.argv[x]
try:
current_ssid = subprocess.check_output(
'iwgetid -r',
shell=True
).rstrip("\r\n")
if current_ssid == ssid:
print "Already connected to " + current_ssid
else:
connectNetwork(ssid)
except CalledProcessError as e:
connectNetwork(ssid)
def connectNetwork(ssid):
setNetwork(wpa_templates.OPEN % (ssid, ""), ssid);
print "Connected to " + ssid
if __name__ == "__main__":
main()