11import time
22import os .path
33import subprocess
4+ import pkgutil
45import bakebit_128_64_oled as oled
56
67from modules .pages .simpletable import SimpleTable
@@ -18,6 +19,13 @@ def __init__(self, g_vars):
1819 # create simple table
1920 self .simple_table_obj = SimpleTable (g_vars )
2021
22+ """
23+ The kismet and bettercap code below can be removed in a future release
24+ if these apps are not added back to the WLAN Pi. (NB 18th June 2020)
25+
26+ The end of the proposed section to be removed is just above the profiler
27+ code
28+ """
2129 def kismet_ctl (self , g_vars , action = "status" ):
2230 '''
2331 Function to start/stop and get status of Kismet processes
@@ -133,39 +141,82 @@ def bettercap_stop(self, g_vars):
133141 def bettercap_start (self , g_vars ):
134142 self .bettercap_ctl (g_vars , action = "start" )
135143 return
144+
145+ """
146+ *** End of kismet and bettercap code to be removed ***
147+ """
148+
149+ def profiler_ctl_file_update (self , fields_dict , filename ):
150+
151+ # read in file to an array
152+ with open (filename , 'r' ) as f :
153+ lines = f .readlines ()
154+
155+ # loop through each field in values to set in file
156+ for key , value in fields_dict .items ():
157+
158+ # step through all lines and look for a match
159+ for count , line in enumerate (lines ):
160+ # replace match in file with key/value pair
161+ if line .startswith (key ):
162+ lines [count ] = "{}: {}\n " .format (key , value )
163+
164+ # write modified file back out
165+ with open (filename , 'w' ) as f :
166+ f .writelines (lines )
136167
168+
137169 def profiler_running (self ):
138170 try :
139171 # this cmd fails if process not active
140172 cmd = "systemctl is-active --quiet profiler.service"
141- cmd_output = subprocess .check_output (cmd , shell = True ). decode ( )
173+ subprocess .check_output (cmd , shell = True )
142174 return True
143175 except subprocess .CalledProcessError as exc :
144176 return False
145177
146178 def profiler_ctl (self , g_vars , action = "status" ):
147179 '''
148- Function to start/stop and get status of Profiler processe
180+ Function to start/stop and get status of Profiler processes
149181 '''
182+ # if we're been round this loop before,
183+ # results treated as cached to prevent re-evaluating
184+ # and re-painting
185+ if g_vars ['result_cache' ] == True :
186+ # re-enable keys
187+ g_vars ['disable_keys' ] = False
188+ return True
189+
190+ # disable keys while we react to the key press that got us here
191+ g_vars ['disable_keys' ] = True
192+
150193 # check resource is available
151194 try :
152- # this cmd fails if service no installed
195+ # this cmd fails if service not installed
153196 cmd = "systemctl is-enabled profiler.service"
154- cmd_output = subprocess .check_output (cmd , shell = True ). decode ( )
197+ subprocess .run (cmd , shell = True )
155198 except :
156199 # cmd failed, so profiler service not installed
157200 self .simple_table_obj . display_dialog_msg (g_vars , 'not available: {}' .format (
158201 profiler_ctl_file ), back_button_req = 1 )
159202 g_vars ['display_state' ] = 'page'
203+ g_vars ['result_cache' ] = True
160204 return
161205
206+ # get path to the config file
207+ package = pkgutil .get_loader ("profiler2" )
208+ entry_point = package .get_filename ()
209+ profiler_dir = os .path .split (entry_point )[0 ]
210+ config_file = "{}/config.ini" .format (profiler_dir )
211+
162212 dialog_msg = "Unset"
163213 item_list = []
164214
165215 # get profiler process status
166216 # (no check for cached result as need to re-evaluate
167217 # on each 1 sec main loop cycle)
168218 if action == "status" :
219+
169220 # check profiler status & return text
170221 if self .profiler_running ():
171222 item_list = ['Profiler active' ]
@@ -175,59 +226,72 @@ def profiler_ctl(self, g_vars, action="status"):
175226 self .simple_table_obj .display_simple_table (g_vars , item_list , back_button_req = 1 ,
176227 title = 'Profiler Status' )
177228 g_vars ['display_state' ] = 'page'
229+
230+ g_vars ['result_cache' ] = True
178231 return True
179232
180- # if we're been round this loop before,
181- # results treated as cached to prevent re-evaluating
182- # and re-painting
183- if g_vars ['result_cache' ] == True :
184- return True
233+ if action .startswith ("start" ):
234+ self .simple_table_obj . display_dialog_msg (g_vars , "Please wait..." , back_button_req = 0 )
235+
236+ if action == "start" :
237+ # set the config file to use params
238+ cfg_dict = { "ft_enabled" : "True" , "he_enabled" : "True" }
239+ self .profiler_ctl_file_update (cfg_dict , config_file )
185240
186- if action == "start" :
187- # disable keys while we do this
188- g_vars ['disable_keys' ] = True
241+ elif action == "start_no11r" :
242+ # set the config file to use params
243+ cfg_dict = { "ft_enabled" : "False" , "he_enabled" : "True" }
244+ self .profiler_ctl_file_update (cfg_dict , config_file )
245+
246+ elif action == "start_no11ax" :
247+ # set the config file to use params
248+ cfg_dict = { "ft_enabled" : "True" , "he_enabled" : "False" }
249+ self .profiler_ctl_file_update (cfg_dict , config_file )
250+
251+ else :
252+ print ("Unknown profiler action: {}" .format (action ))
189253
190254 if self .profiler_running ():
191255 dialog_msg = 'Already running!'
192256 else :
193257 try :
194- cmd = "systemctl start profiler.service"
195- cmd_output = subprocess .check_output (cmd , shell = True ). decode ( )
258+ cmd = "/bin/ systemctl start profiler.service"
259+ subprocess .run (cmd , shell = True , timeout = 2 )
196260 dialog_msg = "Started."
197- except subprocess .CalledProcessError as exc :
261+ except subprocess .CalledProcessError as proc_exc :
198262 dialog_msg = 'Start failed!'
199-
200- # signal that result is cached (stops re-painting screen)
201- g_vars ['result_cache' ] = True
202-
203- # re-enable keys
204- g_vars ['disable_keys' ] = False
205-
206- elif action == "start_no11r" :
207- pass
208-
263+ except subprocess .TimeoutExpired as timeout_exc :
264+ dialog_msg = 'Proc timed-out!'
265+
209266 elif action == "stop" :
210- # disable keys while we do this
211- g_vars [ 'disable_keys' ] = True
267+
268+ self . simple_table_obj . display_dialog_msg ( g_vars , "Please wait..." , back_button_req = 0 )
212269
213270 if not self .profiler_running ():
214271 dialog_msg = 'Already stopped!'
215272 else :
216273 try :
217- cmd = "systemctl stop profiler.service"
218- cmd_output = subprocess .check_output (cmd , shell = True ). decode ( )
274+ cmd = "/bin/ systemctl stop profiler.service"
275+ subprocess .run (cmd , shell = True )
219276 dialog_msg = "Stopped"
220277 except subprocess .CalledProcessError as exc :
221278 dialog_msg = 'Stop failed!'
222279
223- # signal that result is cached (stops re-painting screen)
224- g_vars [ 'result_cache' ] = True
280+ elif action == "purge" :
281+ # call profiler2 with the --clean option
225282
226- # re-enable keys
227- g_vars ['disable_keys' ] = False
283+ self .simple_table_obj . display_dialog_msg (g_vars , "Please wait..." , back_button_req = 0 )
228284
229- elif action == "purge" :
230- pass
285+ try :
286+ cmd = "/usr/bin/python3 -m profiler2 --clean"
287+ subprocess .run (cmd , shell = True )
288+ dialog_msg = "Reports purged."
289+ except subprocess .CalledProcessError as exc :
290+ dialog_msg = "Reports purge error: {}" .format (exc )
291+ print (dialog_msg )
292+
293+ # signal that result is cached (stops re-painting screen)
294+ g_vars ['result_cache' ] = True
231295
232296 self .simple_table_obj . display_dialog_msg (g_vars , dialog_msg , back_button_req = 1 )
233297 g_vars ['display_state' ] = 'page'
@@ -252,7 +316,10 @@ def profiler_start(self, g_vars):
252316 def profiler_start_no11r (self , g_vars ):
253317 self .profiler_ctl (g_vars , action = "start_no11r" )
254318 return
255-
319+
320+ def profiler_start_no11ax (self , g_vars ):
321+ self .profiler_ctl (g_vars , action = "start_no11ax" )
322+ return
256323
257324 def profiler_purge (self , g_vars ):
258325 self .profiler_ctl (g_vars , action = "purge" )
0 commit comments