5
5
import shutil
6
6
import sys
7
7
import os
8
+ import time
8
9
import urllib2
9
10
import fcntl
10
11
import struct
@@ -49,17 +50,17 @@ def start_hostapd(cls):
49
50
hostapd_type = cls .hostapds .get (adapter )
50
51
try :
51
52
print "starting hostapd..."
52
- #os.system("start-stop-daemon --start --oknodo --quiet --exec /usr/sbin/" + hostapd_type + " -- /etc/hostapd/" + hostapd_type + " &")
53
- os .system ("/usr/sbin/" + hostapd_type + " /etc/hostapd/" + hostapd_type + " -B" )
54
-
53
+ out = os .system ("/usr/sbin/" + hostapd_type + " /etc/hostapd/" + hostapd_type + " -B" )
54
+ print "hostapd out: " + str (out )
55
55
except subprocess .CalledProcessError as e :
56
56
print e .output
57
57
58
58
@classmethod
59
59
def stop_hostapd (cls ):
60
60
try :
61
- out = subprocess .check_output (["pkill" , "-9" , "hostapd" ])
62
- print out
61
+ print "stopping hostapd..."
62
+ out = subprocess .check_output (["sudo" , "pkill" , "-9" , "hostapd" ])
63
+ print "hostapd out: " + str (out )
63
64
except subprocess .CalledProcessError as e :
64
65
print e .output
65
66
@@ -71,20 +72,16 @@ def get_ipaddr(cls, ifname):
71
72
0x8915 , # SIOCGIFADDR
72
73
struct .pack ('256s' , ifname [:15 ])
73
74
)[20 :24 ])
74
- ipaddr = socket .gethostbyname (socket .gethostname ())
75
- return ipaddr
76
75
77
76
@classmethod
78
- def register_ipaddr (cls , ipaddr , botname ):
77
+ def register_ipaddr (cls , botname , ipaddr ):
79
78
try :
80
79
ret = urllib2 .urlopen (cls .web_url + "?name=" + botname + "&ipaddr=" + ipaddr )
81
- print str (ret .getcode ())
82
80
if ret .getcode () != 200 :
83
81
raise Exception ()
84
82
except URLError as e :
85
- print e
83
+ print "except: " + str ( e )
86
84
raise
87
- print botname , ": " , ipaddr
88
85
89
86
@classmethod
90
87
def get_wlans (cls ):
@@ -110,10 +107,11 @@ def set_start_as_client(cls):
110
107
def start_as_client (cls ):
111
108
cls .stop_hostapd ()
112
109
try :
113
- out = subprocess .check_output (["ifdown" , "wlan0" ])
110
+ time .sleep (1.0 )
111
+ out = subprocess .check_output (["ifdown" , "--force" , "wlan0" ])
114
112
out = subprocess .check_output (["ifup" , "wlan0" ])
115
- print "registering ip..."
116
- cls . register_ipaddr (cls .get_ipaddr ("wlan0" ), self .get_config ().get ('bot_name' , 'CoderBot' ))
113
+ cls . register_ipaddr ( cls . get_config (). get ( 'bot_name' , 'CoderBot' ), cls . get_ipaddr ( "wlan0" ))
114
+ print "registered bot, ip: " + str (cls .get_ipaddr ("wlan0" ) + " name: " + cls .get_config ().get ('bot_name' , 'CoderBot' ))
117
115
except subprocess .CalledProcessError as e :
118
116
print e .output
119
117
raise
@@ -126,7 +124,8 @@ def set_start_as_ap(cls):
126
124
127
125
@classmethod
128
126
def start_as_ap (cls ):
129
- out = subprocess .check_output (["ifdown" , "wlan0" ])
127
+ time .sleep (1.0 )
128
+ out = subprocess .check_output (["ifdown" , "--force" , "wlan0" ])
130
129
out = subprocess .check_output (["ifup" , "wlan0" ])
131
130
cls .start_hostapd ()
132
131
@@ -149,20 +148,21 @@ def main():
149
148
if len (sys .argv ) > 2 and sys .argv [1 ] == "updatecfg" :
150
149
if len (sys .argv ) > 2 and sys .argv [2 ] == "ap" :
151
150
w .set_start_as_ap ()
152
- w .start_as_ap ()
151
+ # w.start_as_ap()
153
152
elif len (sys .argv ) > 2 and sys .argv [2 ] == "client" :
154
153
if len (sys .argv ) > 3 :
155
154
w .set_client_params (sys .argv [3 ], sys .argv [4 ])
156
155
w .set_start_as_client ()
157
- w . stop_hostapd ()
156
+ """
158
157
try:
159
158
w.start_as_client()
160
159
except:
161
160
print "Unable to register ip, revert to ap mode"
162
161
w.start_as_ap()
162
+ """
163
163
elif len (sys .argv ) > 3 and sys .argv [2 ] == "bot_name" :
164
- self .get_config ()['bot_name' ] = sys .argv [3 ]
165
- self .save_config ()
164
+ WiFi .get_config ()['bot_name' ] = sys .argv [3 ]
165
+ WiFi .save_config ()
166
166
else :
167
167
w .start_service ()
168
168
0 commit comments