def runplaybookNotRegister(): global consolelist, head, tail, session del consolelist[:] hostname = request.form['hostname'] username = request.form['username'] password = request.form['password'] role = request.form.getlist('rolename') print(role) dir = runplaybooks().runplaybookhostnoregister(hostname, username, password, role) session['dir'] = dir owd = os.getcwd() g = proc.Group() p = g.run(["bash", "-c", "(cd " + dir + " && ansible-playbook main.yml)"]) def read_process(): yield head while g.is_pending(): lines = g.readlines() for proc, line in lines: app.logger.info(line) yield "<h5>" + line + "<h5><br>" yield tail return flask.Response(read_process(), mimetype='text/html')
def web_console(): g = proc.Group() action=request.args.get('action') allow_action=['UpdateFile','UploadDir','Upload'] if action not in allow_action: return make_response('error') if action in ['UploadDir','Upload']: local=urllib.unquote(request.args.get('local')) remote=urllib.unquote(request.args.get('remote')) cmd=["python","-u",os.path.join(config_dir,'function.py'),action,local,remote] elif action=='UpdateFile': dir_=request.args.get('dir') if dir_=='/': cmd=["python","-u",os.path.join(config_dir,'function.py'),action] else: cmd=["python","-u",os.path.join(config_dir,'function.py'),'Dir',dir_] else: cmd=["python","-u",os.path.join(config_dir,'function.py'),action] p = g.run(cmd) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield "data:" + line + "\n\n" yield "data:end\n\n" return Response(read_process(), mimetype= 'text/event-stream')
def stream(cmd): #depricated - use index.html instead! g = proc.Group() path = "/home/osmc/flask/%s" if cmd == 'on': script = path % "turn-tv-on.sh" elif cmd == 'off': script = path % "turn-tv-off.sh" elif cmd == 'source1': script = path % "switch-to-hdmiN.sh 1" elif cmd == 'source2': script = path % "switch-to-hdmiN.sh 2" elif cmd == 'source3': script = path % "switch-to-hdmiN.sh 3" elif cmd == 'lamp_on': script = path % "../azw/turn_on.sh" elif cmd == 'lamp_off': script = path % "../azw/turn_off.sh" else: script = "echo 'Unknown command - check syntax'" p = g.run(["bash", "-c", script]) def read_process(): yield "Current response:<p>" while g.is_pending(): lines = g.readlines() for proc, line in lines: yield line yield "</p><br/>" return flask.Response(read_process(), mimetype='text/html')
def runplaybookplugin(): global consolelist, session, head, tail del consolelist[:] groupname = request.form.getlist('groupname') role = request.form.getlist('rolename') conn, cur = connect() cur.execute("SELECT * FROM hostlist where groupname='%s'" % groupname[0]) data = cur.fetchall() print len(data) if len(data) == 0: messages = "Please added atleast one host from addhost menu" return render_template('message.html', messages=messages) if len(role) == 0: messages = "Please add roles at external/plugins folder" return render_template('message.html', messages=messages) dir = runplaybooks().runplaybookgroup(groupname, role) session['dir'] = dir owd = os.getcwd() g = proc.Group() p = g.run(["bash", "-c", "(cd " + dir + " && ansible-playbook main.yml)"]) def read_process(): yield head while g.is_pending(): lines = g.readlines() for proc, line in lines: app.logger.info(line) yield "<h5>" + line + "<h5><br>" yield tail return flask.Response(read_process(), mimetype='text/html')
def stream(self): """ Runs user request and outputs console stream to client""" dag_name = request.args.get("dag_name") start_date = request.args.get("start_date") end_date = request.args.get("end_date") clear = request.args.get("clear") if clear == 'true': cmd = ['airflow', 'clear', '-c', str(dag_name), '-s', str(start_date), '-e', str(end_date)] else: cmd = ['airflow', 'backfill', str(dag_name), '-s', str(start_date), '-e', str(end_date)] print('BACKFILL CMD:', cmd) # Update command used in history file_ops('w', ' '.join(cmd)) g = proc.Group() g.run(cmd) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: if not isinstance(line, str): line = line.decode() line = ansi_escape.sub('', line) print('LINE===> {}'.format(line)) yield "data:" + line + "\n" return flask.Response(read_process(), mimetype='text/event-stream')
def logcat(request): logger.info('Starting Logcat streaming') try: pkg = request.GET.get('package') if pkg: if not strict_package_check(pkg): return print_n_send_error_response(request, 'Invalid package name') template = 'dynamic_analysis/android/logcat.html' return render(request, template, {'package': pkg}) app_pkg = request.GET.get('app_package') if app_pkg: if not strict_package_check(app_pkg): return print_n_send_error_response(request, 'Invalid package name') adb = os.environ['MOBSF_ADB'] g = proc.Group() g.run([adb, 'logcat', app_pkg + ':V', '*:*']) def read_process(): while g.is_pending(): lines = g.readlines() for _, line in lines: time.sleep(.01) yield 'data:{}\n\n'.format(line) return StreamingHttpResponse(read_process(), content_type='text/event-stream') return print_n_send_error_response(request, 'Invalid parameters') except Exception: logger.exception('Logcat Streaming') err = 'Error in Logcat streaming' return print_n_send_error_response(request, err)
def stream(): if session["honeyinstall"]: success = False g = proc.Group() #p = g.run( [ "ls", "-la"] ) p = g.run( [ "sh", "/opt/ansible/run_ansible.sh"] ) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: time.sleep(.1) yield "data:" + line + "\n\n" # TODO mettre un condition en matchant le contenu de line (si line == install ok alors add honey) honey = Honey(honeytype=session['honeytype'],ip=session['honeyip'],desc=session['honeydesc']) db.session.add(honey) db.session.commit() session['honeyinstall'] = False session.pop('honeytype') session.pop('honeyip') session.pop('honeydesc') return Response( read_process(), mimetype= 'text/event-stream' ) else: return Response("Nope", mimetype= 'text/event-stream')
def run_cmd(): # example call: http://pi:5000/_run_cmd?fun=ncplus¶m=info # example call: http://pi:5000/_run_cmd?fun=ZWAVE¶m=speakers&val=on # example call: http://pi:5000/_run_cmd?fun=BASH¶m=RESTART&val=KODI cmd = flask.request.args.get('fun', "", type=str) param = unquote(flask.request.args.get('param', "").replace('_', ' ')) #print "param", flask.request.args.get('param', "") #print "unquote", param #print "args", flask.request.args.get('param', "").replace('_', ' ') val = unquote(flask.request.args.get('val', "")) g = proc.Group() path = "/home/osmc/flask/%s" if cmd == 'TV': script = path % ("cmd-processor-TV.sh " + param) elif cmd == 'SMARTTV': script = "" sTV = send_key2tv.smartTV() results = sTV.sendKey(val) sTV.disconnect() elif cmd == 'ZWAVE': script = "" # path % ("cmd-processor-Z-Wave.sh " + param + " " + val) results = zwave.agocontrol_send_cmd(param, val) elif cmd == 'NCPLUS': script = "" # path % ("cmd-processor-NCPLUS.sh " + param) results = send_key2ncplus.send_key(param) elif cmd == 'BASH': script = path % ("cmd-processor-BASH.sh " + param + " " + val) elif cmd == 'X10': script = "" #path % ("cmd-processor-X10.sh " + param) results = send_x10_to_htpc.send_x10_cmd(param) elif cmd == 'WOL': script = "" # path % ("cmd-processor-WOL.sh " + param) results = wol.main(param) elif cmd == 'JASPER': script = 'su -c "/home/osmc/flask/cmd-processor-JASPER.sh \'%s\'" - osmc' % param #print script else: script = "echo 'Unknown command - check syntax: %s %s'" % (cmd, param) if script != "": p = g.run(["bash", "-c", script]) response = "" def read_process(): response = "" while g.is_pending(): lines = g.readlines() for proc, line in lines: response = response + line return response results = read_process() return flask.jsonify(result=results.replace("\n", ""))
def deploy(): client_address = flask.request.remote_addr auth = flask.request.headers.get('Authorization') if client_address in WHITELISTED_CLIENTS and auth == DEPLOY_SECRET: g = proc.Group() g.run(["bash", "-c", "scripts/deploy.sh"]) return flask.Response(stream_output(g), mimetype='text/plain') else: return "nope\n", 403
def stream(): g = proc.Group() p = g.run(["pytest -s"]) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield line return flask.Response(read_process(), mimetype='text/plain')
def stream(): from shelljob import proc first_file(UPLOAD_FOLDER + EXTENSION) g = proc.Group() bashCommand = "python3 /home/bartrthomson/igor2/bin/cumed-predict /tmp/test.json /home/bartrthomson/results/antwoord/fold-01/checkpoints/cumed-56000 /tmp/" sys.path.append('/home/bartrthomson/igor2/bin') p = g.run(["bash", "-c", bashCommand]) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield line term = b'Done. Terminating...' if term in line: #see if one of the words in the sentence is the word we want output = '/tmp/SCAN-segmentation.npy' print('SEGMENTED FILE LOCATION::: ' + output) data = np.load(output) data = data[:, ..., 1] data = np.rot90(data, 3, axes=(0, 2)) data = np.flipud(data) data = data > 0.2 # Generate binary structure for biggest 3D region selection s = ndimage.generate_binary_structure( 3, 3) # iterate structure labeled_array, numpatches = ndimage.label(data, s) # labeling sizes = ndimage.sum(data, labeled_array, range(1, numpatches + 1)) # To get the indices of all the min/max patches. Is this the correct label id? map = np.where(sizes == sizes.max())[0] + 1 # Inside the largest, respecitively the smallest labeled patches with values max_index = np.zeros(numpatches + 1, np.uint8) max_index[map] = 1 data = max_index[labeled_array] # Biggest region np.save('/tmp/temp.npy', data) data = data.astype(float) data = nibabel.Nifti1Image(data, affine=np.eye(4)) nibabel.save(data, "/tmp/SCAN-segmentation.nii") segmentation_out = sitk.ReadImage( r'/tmp/SCAN-segmentation.nii') sitk.WriteImage( segmentation_out, r'/home/bartrthomson/flask_app/static/SCAN-segmentation.nii.gz' ) yield 'RETURN TO MAIN PAGE AND PRESS ANALYZE' return flask.Response(read_process(), mimetype='html')
def get_zone(zone_id): group = proc.Group() group.run(['/home/pi/mp6z/mp6z.py', 'get', str(zone_id)]) def generator(): while group.is_pending(): data = group.readlines() for handle, lines in data: yield lines return Response(generator(), mimetype='text/plain')
def stream(): g = proc.Group() p = g.run( [ "bash", "-c", "for ((i=0;i<100;i=i+1)); do echo $i; sleep 1; done" ] ) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield line return flask.Response( read_process(), mimetype= 'text/plain' )
def stream(): g = proc.Group() p = g.run(["cat", ".git/refs/heads/master"]) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield line return flask.Response(read_process(), mimetype='text/plain')
def test_group(): sp = proc.Group() for i in range(0, 5): sp.run(['ls', '-al', '/usr/local'], encoding='utf-8') while sp.is_pending(): lines = sp.readlines() for pc, line in lines: if not isinstance(line, str): raise Exception("Expecting string") # since encoding specified sys.stdout.write("{}:{}".format(pc.pid, line))
def stream(): g = proc.Group() p = g.run(["bash", "-c", "sudo python analyzer.py"]) print "Script launched" def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield "data:" + line + "\n\n" return Response(read_process(), mimetype='text/event-stream')
def test_group(): sp = proc.Group() sp.run( [ "bash", "-c", "for ((i=0;i<10;i=i+1)); do echo $i; sleep 0.25; done" ] ) while sp.is_pending(): lines = sp.readlines() if len(lines) > 1: raise Exception("Expecting 1 line at a time: "+ str(len(lines))) for pc, line in lines: if not isinstance(line, bytes): # no encoding specified, expectding bytes raise Exception("Expecting bytes object") sys.stdout.write( "{}:{}\n".format( pc.pid, line ) ) sys.stdout.flush()
def stream(): ipAddr = None username = None password = None codelines = None g = proc.Group() try: filepath = os.getcwd() + "/fabfile.py" with open(filepath) as f: codelines = f.read() clines = re.findall(r'env.hosts\s*\=\s*\[\'\d+\.\d+\.\d+\.\d+\'\]', codelines) if clines: ipAddr = re.search(r'\d+\.\d+\.\d+\.\d+', clines[0]) ipAddr = ipAddr.group() clines = re.findall(r'env.user\s*\=\s*\'\w+\'', codelines) if clines: username = re.search(r'\'\w+\'', clines[0]) username = username.group().strip("'") clines = re.findall(r'env.password\s*\=\s*\'.+\'', codelines) if clines: password = re.search(r'\'.+\'', clines[0]) password = password.group().strip("'") if checkSSHConnection(ipAddr, username, password): p = g.run(["bash", "-c", "cd", "/home/qqdewa/sideproject/"]) #p = g.run(["bash", "-c", "python fabfile.py"]) p = g.run(["bash", "-c", "fab -t 5 setup_api"]) else: return Response( "<span class='line' style='color:#aae8f8;font-family:Helvetica;font-size:11px'>Configuration Error</span>", mimetype="text/html") except: return Response( "<span class='line' style='color:#aae8f8;font-family:Helvetica;font-size:11px'>Configuration Error</span>", mimetype="text/html") def read_process(): trigger_time = time.time() while g.is_pending(): lines = g.readlines() for proc, line in lines: yield "<span class='line' style='color:#aae8f8;font-family:Helvetica;font-size:11px'>" + line + "</span><br>" trigger_time += 10 now = time.time() if now > trigger_time: yield "<span class='line' style='color:#aae8f8;font-family:Helvetica;font-size:11px'>*** Timeout</span>" break trigger_time = now + 10 return Response(read_process(), mimetype="text/html")
def stream(cmd): g = proc.Group() #p = g.run( [ "bash", "-c", "for ((i=0;i<100;i=i+1)); do echo $i; sleep 1; done" ] ) p = g.run( [ "zsh", "-c", "{args}".format(args=cmd)]) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield "data:" + line + "\n\n" return flask.Response( read_process(), mimetype= 'text/event-stream' )
def stream(): global spec_files g = proc.Group() p = g.run("sh ./me.sh %s %s %s" % (spec_files[0], omega_dir, omega_server_dir)) def read_process(): while g.is_pending(): lines = g.readlines() time.sleep(1) for proc, line in lines: yield line return flask.Response(read_process(), mimetype='text/plain')
def __init__(self, max_simul=multiprocessing.cpu_count(), feedback_timeout=5.0): """ @param max_simul: the maximum number of processes which can be running at the same time @param feedback_timeout: generate progress feedback at this interval """ self.group = proc.Group() self.max_simul = max_simul self.jobs = {} self.feedback_timeout = feedback_timeout self.last_feedback = time.time() self.job_count = 0
def index(company_name): g = proc.Group() p = g.run([ "python3", "/var/www/attendancekeeper/detector/trainer.py", company_name ]) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield line return Response(read_process(), mimetype='text/plain')
def run(): if request.method == 'GET': return "LOL LOL" if request.method == 'POST': command = request.get_json() g = proc.Group() p = g.run(command['command']) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield line return Response(read_process(), mimetype='text/plain')
def test_unicode_group(): sp = proc.Group() failed = False def on_error(e): nonlocal failed failed = True assert isinstance(e, UnicodeDecodeError) sp.run(['cat', 'UTF-8-test.txt'], encoding='utf-8', on_error=on_error) while sp.is_pending(): lines = sp.readlines() for pc, line in lines: print(line) assert failed
def scan(): result = None if request.method == 'POST': url = request.form['text'] g = proc.Group() #p = g.run( [ "nmap", "-PN", "-n", "-F", "-T4", "-sV", "-A", "--version-intensity", "5", url ] ) p = g.run([ "nmap", url ]) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield line + "\n\n" result = read_process() env = Environment(loader=FileSystemLoader('templates')) tmpl = env.get_template('output.html') return flask.Response(tmpl.generate(result=result),mimetype= 'text/html')
def analyze_data(): form = DataForm() if not form.enable_qualified.data: del form.qualified if not form.validate(): return jsonify(errors=form.errors) x_csv = save_file(form.xcsv.data) clf_pickle = save_file(form.clf.data) outfr = "fairtear/output/result.fr" sensitive_attrs = [prepare_attribute(form.sensitive.data)] if form.enable_qualified.data: qualified_attrs = [prepare_attribute(form.qualified.data)] else: qualified_attrs = [] fairness_targets = [prepare_attribute(form.target.data)] args = { "clf_pickle": clf_pickle, "x_csv": x_csv, "y_label": form.target.data['attribute'], "outfr": outfr, "sensitive_attrs": sensitive_attrs, "qualified_attrs": qualified_attrs, "fairness_targets": fairness_targets, } # stream console output # https://mortoray.com/2014/03/04/http-streaming-of-command-output-in-python-flask/ g = proc.Group() p = g.run([sys.executable, "-u", "fairtear/compile.py", json.dumps(args)]) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: sys.stdout.buffer.write(line) yield line return Response(read_process(), mimetype="text/plain")
def stream(self): ''' runs user request and outputs console stream to client''' dag_name = request.args.get("dag_name") start_date = request.args.get("start_date") end_date = request.args.get("end_date") clear = request.args.get("clear") if clear == 'true': cmd = [ "airflow", "clear", "-c", "-s", str(start_date), "-e", str(end_date), str(dag_name) ] else: cmd = [ "airflow", "backfill", "-s", str(start_date), "-e", str(end_date), str(dag_name) ] print 'BACKFILL CMD:', cmd # Update command used in history self.file_ops('w', ' '.join(cmd)) g = proc.Group() p = g.run(cmd) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: print 'LINE===> {}'.format(line) yield "data:" + line + "\n\n" return flask.Response(read_process(), mimetype='text/event-stream')
def shell(cmd): try: print cmd # cmd = ["env"] g = proc.Group() p = g.run(cmd) status_code = 200 def read_process(): result = '' while g.is_pending(): lines = g.readlines() for proc, line in lines: result = result + line print line return result resp = read_process() return resp except Exception as e: print(e) raise e
def firmware_update_stream(): filename = os.path.join(app.config['UPLOAD_FOLDER'], request.args.get("filename")) #give a short period to allow the file to be updloaded escape_count = 0 while not Path(filename).is_file(): if escape_count > 5: break escape_count += 1 sleep(0.5) g = proc.Group() p = g.run(["python3", os.path.join(app.config['UPLOAD_FOLDER'],"update_firmware.py"), "-H " + filename]) def read_process(): while g.is_pending(): lines = g.readlines() for proc, line in lines: yield "data:" + str(line, 'utf-8') + "\n\n" yield "data: finished\n\n" read_proc_op = read_process() return Response(read_proc_op,mimetype='text/event-stream')
def set_zone(zone_id): cliargs = ['/home/pi/mp6z/mp6z.py', 'set', str(zone_id)] if 'volume' in request.json: cliargs.append('-v') cliargs.append(str(request.json.get('volume'))) if 'balance' in request.json: cliargs.append('--bl') cliargs.append(str(request.json.get('balance'))) if 'bass' in request.json: cliargs.append('-b') cliargs.append(str(request.json.get('bass'))) if 'dnd' in request.json: cliargs.append('-d') cliargs.append(str(request.json.get('dnd'))) if 'mute' in request.json: cliargs.append('-m') cliargs.append(str(request.json.get('mute'))) if 'power' in request.json: cliargs.append('-p') cliargs.append(str(request.json.get('power'))) if 'source' in request.json: cliargs.append('-s') cliargs.append(str(request.json.get('source'))) if 'treble' in request.json: cliargs.append('-t') cliargs.append(str(request.json.get('treble'))) group = proc.Group() group.run(cliargs) def generator(): while group.is_pending(): data = group.readlines() for handle, lines in data: yield lines return Response(generator(), mimetype='text/plain')