Skip to content

Commit cc97507

Browse files
committed
Added command to set pi number
e.g sudo python wifi.py updatecfg 06 will change the client ip to 192.168.0.106 and set the ssid to Lboro Coderbot 06
1 parent d1df454 commit cc97507

File tree

2 files changed

+81
-57
lines changed

2 files changed

+81
-57
lines changed

scripts/etc/network/interfaces_cli

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
auto lo
22
iface lo inet loopback
33
iface eth0 inet dhcp
4-
54
allow-hotplug wlan0
6-
iface wlan0 inet dhcp
7-
#iface default inet dhcp
8-
9-
wpa-ssid "Lboro_Coderbot_Hub"
10-
wpa-psk "welovepis"
11-
12-
#wireless-power off
5+
iface wlan0 inet static
6+
address 192.168.0.105
7+
netmask 255.255.255.0
8+
gateway 192.168.0.1
9+
wpa-ssid Lboro_Coderbot_Hub
10+
wpa-psk welovepis
1311

wifi.py

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class WiFi():
1717

1818
CONFIG_FILE = "/etc/coderbot_wifi.conf"
1919
HOSTAPD_CONF_FILE = "/etc/hostapd/hostapd.conf"
20+
INTERFACES_FILE = "/etc/network/interfaces_cli"
2021
adapters = ["RT5370", "RTL8188CUS", "RT3572"]
2122
hostapds = {"RT5370": "hostapd.RT5370", "RTL8188CUS": "hostapd.RTL8188"}
2223
web_url = "http://coderbotsrv.appspot.com/register_ip"
@@ -91,6 +92,42 @@ def set_hostapd_params(cls, wssid, wpsk):
9192
except IOError as e:
9293
print e
9394

95+
def set_client_params(cls, wssid, wpsk, number):
96+
config = ConfigParser.ConfigParser()
97+
# configparser requires sections like '[section]'
98+
# open hostapd.conf with dummy section '[interfaces]'
99+
100+
try:
101+
with open(cls.INTERFACES_FILE) as f:
102+
inter_split = f.read().split("static")
103+
inter_start = inter_split[0] + "static\n"
104+
conf_str = '[interfaces]\n' + inter_split[1]
105+
#parser needs = to work
106+
conf_str = conf_str.replace(" ","=")
107+
conf_fp = StringIO.StringIO(conf_str)
108+
config.readfp(conf_fp)
109+
110+
except IOError as e:
111+
print e
112+
return
113+
114+
config.set('interfaces','address',"192.168.0.1" + number)
115+
config.set('interfaces','wpa-ssid',str(wssid))
116+
config.set('interfaces','wpa-psk',str(wpsk))
117+
118+
try:
119+
with open(cls.INTERFACES_FILE, 'wb') as f:
120+
conf_items = config.items('interfaces')
121+
f.write(inter_start)
122+
for (key,value) in conf_items:
123+
#quick workaround for values with spaces in
124+
value = value.replace("wlan0=inet=static","wlan0 inet static")
125+
f.write("{0} {1}\n".format(key, value))
126+
f.write("\n")
127+
except IOError as e:
128+
print e
129+
shutil.copy(cls.INTERFACES_FILE, "/etc/network/interfaces")
130+
94131
@classmethod
95132
def get_ipaddr(cls, ifname):
96133
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -137,57 +174,46 @@ def start_service(cls):
137174

138175
def main():
139176
w = WiFi()
140-
141-
print 'Testing Client Connection...'
142-
print 'Wait 3 seconds before checking connection to router...'
143-
sleep(3)
144-
print 'pinging router...'
145-
#ping hub router
146-
response = os.system('ping -c 1 192.168.0.1')
147-
#healthy response is 0
148-
149-
if response == 0:
150-
print 'Router has been found, staying on client mode'
151-
else:
152-
print 'Router not found, switching to AP mode'
153-
#setup hotspot
154-
shutil.copy("/etc/network/interfaces_ap", "/etc/network/interfaces")
155-
print 'restart networking...'
156-
os.system('sudo service networking restart')
157-
w.start_hostapd()
158-
print 'Waiting for hostapd to startup'
177+
if len(sys.argv) < 2:
178+
print 'Testing Client Connection...'
179+
print 'Wait 3 seconds before checking connection to router...'
159180
sleep(3)
160-
print 'copying client interfaces back for next time'
161-
shutil.copy("/etc/network/interfaces_cli", "/etc/network/interfaces")
181+
print 'pinging router...'
182+
#ping hub router
183+
response = os.system('ping -c 1 192.168.0.1')
184+
#healthy response is 0
185+
186+
if response == 0:
187+
print 'Router has been found, staying on client mode'
188+
else:
189+
print 'Router not found, switching to AP mode'
190+
#setup hotspot
191+
shutil.copy("/etc/network/interfaces_ap", "/etc/network/interfaces")
192+
print 'restart networking...'
193+
os.system('sudo service networking restart')
194+
w.start_hostapd()
195+
print 'Waiting for hostapd to startup'
196+
sleep(3)
197+
print 'copying client interfaces back for next time'
198+
shutil.copy("/etc/network/interfaces_cli", "/etc/network/interfaces")
162199

163-
if len(sys.argv) > 2 and sys.argv[1] == "updatecfg":
164-
if len(sys.argv) > 2 and sys.argv[2] == "ap":
165-
if len(sys.argv) > 3:
166-
w.set_hostapd_params(sys.argv[3], sys.argv[4])
167-
#w.set_start_as_ap()
168-
#w.start_as_ap()
169-
elif len(sys.argv) > 2 and sys.argv[2] == "hub":
170-
if len(sys.argv) > 3:
171-
w.set_client_params(sys.argv[3], sys.argv[4])
172-
#w.set_start_as_client()
173-
#w.stop_hostapd()
174-
#try:
175-
# w.start_as_client()
176-
#except:
177-
# print "Unable to register ip, revert to ap mode"
178-
# w.start_as_ap()
179-
elif len(sys.argv) > 2 and sys.argv[2] == "local_client":
180-
if len(sys.argv) > 3:
181-
w.set_client_params(sys.argv[3], sys.argv[4])
182-
w.set_start_as_client()
183-
try:
184-
w.start_as_local_client()
185-
except:
186-
print "Unable to connect to WLAN, revert to ap mode"
187-
w.start_as_ap()
188-
189-
#else:
190-
# w.start_service()
200+
elif sys.argv[1] == "updatecfg":
201+
if len(sys.argv) == 3:
202+
print "updating configs..."
203+
#Update config to use this number, eg. for 7
204+
#client ip goes to 192.168.0.7
205+
w.set_client_params("Lboro_Coderbot_Hub","welovepis", sys.argv[2])
206+
#hostspot ssid goes to Lboro Coderbot 7
207+
w.set_hostapd_params("Lboro Coderbot " + sys.argv[2], "coderbot")
208+
print "done!"
209+
else:
210+
if len(sys.argv) > 2 and sys.argv[2] == "ap":
211+
if len(sys.argv) > 3:
212+
w.set_hostapd_params(sys.argv[3], sys.argv[4])
213+
elif len(sys.argv) > 2 and sys.argv[2] == "client":
214+
if len(sys.argv) > 4:
215+
w.set_client_params(sys.argv[3], sys.argv[4], sys.argv[5])
216+
191217

192218
if __name__ == "__main__":
193219
main()

0 commit comments

Comments
 (0)