@@ -42,7 +42,7 @@ def console(message: str, final: bool=False):
42
42
43
43
async def load_config (segment : str = 'meshcentral-account' ) -> dict :
44
44
'''
45
- Function that loads the segment from the meshbook .conf (by default) file and returns the it in a dict.
45
+ Function that loads the segment from the config .conf (by default) file and returns the it in a dict.
46
46
'''
47
47
48
48
conf_file = args .conf
@@ -186,13 +186,20 @@ def get_os_variants(category: str, os_map: dict) -> set:
186
186
allowed_os = get_os_variants (target_os , os_categories [key ])
187
187
break # Stop searching once a match is found
188
188
189
- # Filter out unreachable devices
189
+ # Filter out unwanted or unreachable devices.
190
190
for device in devices :
191
191
if not device ["reachable" ]:
192
192
continue # Skip unreachable devices.
193
193
194
- if not target_os or device ["device_os" ] in allowed_os :
195
- valid_devices .append (device ["device_id" ])
194
+ print (target_tag )
195
+ print (device ["device_tags" ])
196
+ if target_tag and target_tag not in device ["device_tags" ]:
197
+ continue
198
+
199
+ if device ["device_os" ] not in allowed_os :
200
+ continue
201
+
202
+ valid_devices .append (device ["device_id" ])
196
203
197
204
return valid_devices
198
205
@@ -203,6 +210,7 @@ async def gather_targets(meshbook: dict, group_list: dict[str, list[dict]], os_c
203
210
204
211
target_list = []
205
212
target_os = meshbook .get ("target_os" )
213
+ target_tag = meshbook .get ("target_tag" )
206
214
207
215
async def process_device_or_group (pseudo_target , group_list , os_categories , target_os ) -> list [str ]:
208
216
'''
@@ -216,7 +224,7 @@ async def process_device_or_group(pseudo_target, group_list, os_categories, targ
216
224
matched_devices .append (device )
217
225
218
226
if matched_devices :
219
- return await filter_targets (matched_devices , os_categories , target_os )
227
+ return await filter_targets (matched_devices , os_categories , target_os , target_tag )
220
228
return []
221
229
222
230
match meshbook :
@@ -238,7 +246,7 @@ async def process_device_or_group(pseudo_target, group_list, os_categories, targ
238
246
239
247
case {"group" : pseudo_target }: # Single group target
240
248
if isinstance (pseudo_target , str ) and pseudo_target in group_list :
241
- matched_devices = await filter_targets (group_list [pseudo_target ], os_categories , target_os )
249
+ matched_devices = await filter_targets (group_list [pseudo_target ], os_categories , target_os , target_tag )
242
250
target_list .extend (matched_devices )
243
251
elif pseudo_target not in group_list :
244
252
console (text_color .yellow + "Targeted group not found on the MeshCentral server." , True )
@@ -249,11 +257,11 @@ async def process_device_or_group(pseudo_target, group_list, os_categories, targ
249
257
if isinstance (pseudo_target , list ):
250
258
for sub_pseudo_target in pseudo_target :
251
259
if sub_pseudo_target in group_list :
252
- matched_devices = await filter_targets (group_list [sub_pseudo_target ], os_categories , target_os )
260
+ matched_devices = await filter_targets (group_list [sub_pseudo_target ], os_categories , target_os , target_tag )
253
261
target_list .extend (matched_devices )
254
262
if pseudo_target .lower () == "all" :
255
263
for group in group_list :
256
- matched_devices = await filter_targets (group_list [group ], os_categories , target_os )
264
+ matched_devices = await filter_targets (group_list [group ], os_categories , target_os , target_tag )
257
265
target_list .extend (matched_devices )
258
266
else :
259
267
console (text_color .yellow + "The 'groups' method is being used, but only one string is given. Did you mean 'group'?" , True )
@@ -289,7 +297,7 @@ async def execute_meshbook(session: meshctrl.Session, targets: dict, meshbook: d
289
297
}
290
298
round += 1
291
299
292
- console (("-" * 40 ))
300
+ console (text_color . reset + ("-" * 40 ))
293
301
if args .indent :
294
302
console ((json .dumps (responses_list ,indent = 4 )), True )
295
303
@@ -306,7 +314,7 @@ async def main():
306
314
parser .add_argument ("-mb" , "--meshbook" , type = str , help = "Path to the meshbook yaml file." , required = True )
307
315
308
316
parser .add_argument ("-oc" , "--oscategories" , type = str , help = "Path to the Operating System categories JSON file." , required = False , default = "./os_categories.json" )
309
- parser .add_argument ("--conf" , type = str , help = "Path for the API configuration file (default: ./meshbook .conf)." , required = False , default = "./meshbook .conf" )
317
+ parser .add_argument ("--conf" , type = str , help = "Path for the API configuration file (default: ./config .conf)." , required = False , default = "./config .conf" )
310
318
parser .add_argument ("--nograce" , action = "store_true" , help = "Disable the grace 3 seconds before running the meshbook." , required = False )
311
319
parser .add_argument ("-i" , "--indent" , action = "store_true" , help = "Use an JSON indentation of 4 when this flag is passed." , required = False )
312
320
parser .add_argument ("-s" , "--silent" , action = "store_true" , help = "Suppress terminal output" , required = False )
@@ -328,7 +336,7 @@ async def main():
328
336
The following section mainly displays used variables and first steps of the program to the console.
329
337
'''
330
338
331
- console (("-" * 40 ))
339
+ console (text_color . reset + ("-" * 40 ))
332
340
console ("meshbook: " + text_color .yellow + args .meshbook )
333
341
console ("Operating System Categorisation file: " + text_color .yellow + args .oscategories )
334
342
console ("Configuration file: " + text_color .yellow + args .conf )
@@ -347,7 +355,7 @@ async def main():
347
355
console ("Silent: " + text_color .yellow + "False" ) # Can be pre-defined because if silent flag was passed then none of this would be printed.
348
356
349
357
session = await init_connection (credentials )
350
- console (("-" * 40 ))
358
+ console (text_color . reset + ("-" * 40 ))
351
359
console (text_color .italic + "Trying to load the MeshCentral account credential file..." )
352
360
console (text_color .italic + "Trying to load the meshbook yaml file and compile it into something workable..." )
353
361
console (text_color .italic + "Trying to load the Operating System categorisation JSON file..." )
@@ -363,10 +371,10 @@ async def main():
363
371
364
372
if len (targets_list ) == 0 :
365
373
console (text_color .red + "No targets found or targets unreachable, quitting." , True )
366
- console (("-" * 40 ), True )
374
+ console (text_color . reset + ("-" * 40 ), True )
367
375
368
376
else :
369
- console (("-" * 40 ))
377
+ console (text_color . reset + ("-" * 40 ))
370
378
371
379
match meshbook :
372
380
case {"group" : candidate_target_name }:
@@ -390,8 +398,9 @@ async def main():
390
398
console (text_color .yellow + "{}..." .format (x + 1 )) # Countdown!
391
399
await asyncio .sleep (1 )
392
400
393
- console (("-" * 40 ))
394
- await execute_meshbook (session , targets_list , meshbook , group_list )
401
+ console (text_color .reset + ("-" * 40 ))
402
+ print (json .dumps (targets_list ,indent = 4 ))
403
+ #await execute_meshbook(session, targets_list, meshbook, group_list)
395
404
396
405
await session .close ()
397
406
0 commit comments