def upload_bam_to_IR16(sample_name, sample_path): mypassword = "******" config_file = "wtc_config.txt" irucli_cmd = "/home/ionadmin/IonReporterUploader-cli/bin/irucli.sh -c /home/ionadmin/IonReporterUploader-cli/%s -sn %s -sp %s -sg Unknown" % (config_file, sample_name, sample_path) #print irucli_cmd print pexpect.run(irucli_cmd, events={'lifetech.com :': mypassword}) ###################################################################################### # update list of finished uploads uploaded_bam_file = "/home/ionadmin/andreas/projects/ionoxford/TSB_Tools/DataManagement/IR16toIR40migration/finished_uploads.txt" uploaded_bams = [x.split("\t")[2] for x in open(uploaded_bam_file).readlines()] logfile = "/home/ionadmin/andreas/projects/ionoxford/TSB_Tools/DataManagement/IR16toIR40migration/log.txt" logrows = [row for row in open(logfile).readlines() if "uploaded file" in row and "bam" in row] with open(uploaded_bam_file, "a") as bamfile: for row in logrows: datestamp = str(datetime.datetime.now()) new_uploaded_bam = row.split(" uploaded file ")[1] if new_uploaded_bam not in uploaded_bams: new_row = [datestamp, sample_name, new_uploaded_bam] bamfile.write("\t".join(new_row))
def pt_newdir(**kwa): """newdir - Create a new python project """ if kwa['d']: pdb.set_trace() root = py.path.local(kwa['PATH']) if root.isdir(): msg = " ".join([root.strpath, "already exists, please remove it first"]) sys.exit(msg) pkgname = root.basename root.ensure(dir=True) root.join(pkgname).ensure(dir=True) root.join('test').ensure(dir=True) venv = root.join("venv") pexpect.run("virtualenv {}".format(venv.strpath)) setup = root.join("setup.py") setup.write(setup_content_s(pkgname)) env = root.join(".env") env.write(env_content_s(pkgname)) conftest = root.join("conftest.py") conftest.write(conftest_content()) pass
def main(): syslog.openlog('populate-hub-ldap', 0, syslog.LOG_LOCAL5) INSTALL_DIR=path.dirname(path.dirname(path.abspath(sys.argv[0]))) # restart slapd to get new config stdout.write("\nRestarting slapd to make sure gets new config...\n") sp.check_call(['/etc/init.d/slapd','restart']) # set up smbpasswd stdout.write('Establishing administrative passwd for Samba...\n') sp.check_call(['smbpasswd','-w','1nvene0']) # populate sldapd stdout.write('Populating default Samba values in LDAP server..\n') pexpect.run('smbldap-populate -u 10000 -g 10000',events={'ew password:'******'1nvene0\n','type new password:'******'1nvene0\n'}) # add inveneo_users group to ldap stdout.write("Adding 'inveneo_users' group") sp.call(['smbldap-groupdel','inveneo_users']) sp.check_call(['smbldap-groupadd','-a','-g','999','inveneo_users']) # adding permissions groups to ldap stdout.write("\nAdding 'permissions' groups to ldap...\n") sp.check_call(['smbldap-migrate-unix-groups','-G',path.join(INSTALL_DIR,'permission-groups')]) # Add default samba user try: stdout.write("Attempting to delete the 'default' user.\n") sp.Popen(ENV + ['inv-user-del.py','default'],stderr=sp.PIPE).communicate() stdout.write("Success") except Exception, ex: pass # don't care if user not there
def scp_transfer(transfer_job): """Plugin" for handling scp transfers using pexpect""" def print_ticks(d): pass host = transfer_job.params['host'] user_name = transfer_job.params['user_name'] password = transfer_job.params['password'] file_path = transfer_job.params['file_path'] if pexpect is None: return dict(state=transfer_job.states.ERROR, info=PEXPECT_IMPORT_MESSAGE) try: fh, fn = tempfile.mkstemp() except Exception as e: return dict(state=transfer_job.states.ERROR, info='Unable to create temporary file for transfer: %s' % str(e)) try: # TODO: add the ability to determine progress of the copy here like we do in the http_transfer above. cmd = "scp %s@%s:'%s' '%s'" % (user_name, host, file_path.replace(' ', '\ '), fn) pexpect.run(cmd, events={'.ssword:*': password + '\r\n', pexpect.TIMEOUT: print_ticks}, timeout=10) return dict(state=transfer_job.states.DONE, path=fn) except Exception as e: return dict(state=transfer_job.states.ERROR, info='Error during file transfer: %s' % str(e))
def test_cwd(self): " check keyword argument `cwd=' of pexpect.run() " tmp_dir = os.path.realpath(tempfile.gettempdir()) default = pexpect.run('pwd') pwd_tmp = pexpect.run('pwd', cwd=tmp_dir).rstrip() assert default != pwd_tmp assert tmp_dir == _u(pwd_tmp)
def test_diff(self): """ Test diff """ with util.Chdir("fl_tests"): util.writefile('mrpm1', ['this is a test file\n']) util.writefile('mrpm2', ['this is another test file\n']) util.writefile('mrpm1.2009-10-01', ['copy of test file\n']) util.writefile('old/mrpm2.2009-08-31', ['copy of another test file\n']) expected = ['diff ./mrpm1.2009-10-01 mrpm1\r', '1c1\r', '< copy of test file\r', '---\r', '> this is a test file\r', ''] cmd = util.script_location("fl") got = pexpect.run("%s diff mrpm1" % cmd).split("\n") self.assertEqual(expected, got) expected = ['diff ./old/mrpm2.2009-08-31 mrpm2\r', '1c1\r', '< copy of another test file\r', '---\r', '> this is another test file\r', ''] got = pexpect.run("%s diff mrpm2" % cmd).split("\n") self.assertEqual(expected, got)
def launch_ansible_playbook_command(ansible_command, job_id, password=None, become_password=None): logger.info("Enter") log_output_file_realpath = _create_log_output_file_path(job_id) output_file = open(log_output_file_realpath, 'a') try: if password and become_password: password_str = password + "\n" become_password_str = become_password + "\n" logger.info(pexpect.run(ansible_command, logfile=output_file, timeout=-1, events={"SSH password:"******"SUDO password.*:": become_password_str})) elif password: password_str = password + "\n" logger.info( pexpect.run(ansible_command, logfile=output_file, timeout=-1, events={"SSH password:"******"\n" logger.info(pexpect.run(ansible_command, logfile=output_file, timeout=-1, events={".*password:"******"Exit")
def remote_shell(shell_cmd, args, expect_receive="", expect_send=""): ''' Run ssh based shellcommandon a remote machine at args.ip :param shell_cmd: string based command :param expect_receive: :param expect_send: :param args: all command line arguments :return: dict = {'stdout': str:ouput, 'exitcode': return code} ''' cmd_args = vars(args) logfile_redirect = None shell_cmd.replace("'", "\\\'") if args.v is True: print("remote_shell: Shell Command: {}".format(shell_cmd)) logfile_redirect = sys.stdout if expect_receive == "" or expect_send == "": (command_output, exitstatus) = \ pexpect.run("ssh -q -o StrictHostKeyChecking=no -t " + args.port + " " + cmd_args['user'] + "@" + cmd_args['ip'] + " sudo sh -c \\\"" + shell_cmd + "\\\"", withexitstatus=1, events={"assword": cmd_args['pwd'] + "\n"}, timeout=600, logfile=logfile_redirect) else: (command_output, exitstatus) = \ pexpect.run("ssh -q -o StrictHostKeyChecking=no -t " + args.port + " " + cmd_args['user'] + "@" + cmd_args['ip'] + " sudo sh -c \\\"" + shell_cmd + "\\\"", withexitstatus=1, events={"assword": cmd_args['pwd'] + "\n", expect_receive: expect_send + "\n"}, timeout=600, logfile=logfile_redirect) if args.v is True: print("Shell cmd: {}".format(shell_cmd)) print("Exit Code: {}".format(exitstatus)) return {'stdout': command_output, 'exitcode': exitstatus}
def SubirProjeto(self,Aluno,Curso,Projeto): """ Método que sobe os arquivos para o repositorio do aluno :param Aluno: Usuario precisa ser um dicionário com 3 keys: name,username,email. Exemplo: {'name':'Alisson Machado','username':'******','email':'*****@*****.**'} :param Curso: Curso e uma variavel que precisa ter somente o numero do curso que o aluno ira fazer :param Projeto: Precisa ser um dicionário com pelo menos 2 chaves, ssh_url_to_repo e id, que são as informações necessárias para fazer o git push dos arquivos para o projeto do launo :returns: Método não possui valor de retorno, apenas informa na tela a porcentagem do upload dos arquivos """ mo = MongoOps() NomeCurso = mo.BuscarNomeDoCurso(Curso) log.info( "[+] Subindo projeto") try: os.chdir("/opt/4linux/%s"%NomeCurso) pexpect.run("git add --all") pexpect.run("git commit --amend") cmd = pexpect.spawn("git push %s master -f"%Projeto['ssh_url_to_repo']) cmd.expect(pexpect.EOF) except Exception as e: log.error("[-] Erro ao subir o projeto: %s",e) log.info("[+] Removendo o usuario 4linux do projeto") if not self.git.deleteprojectmember(int(Projeto['id']),str(self.adminuser)): log.error("[-] Erro ao remover membro do projeto %s",e)
def check_github_status(GITDIR,GITWORKTREE,GITHUBUSER,GITHUBPWD): # some git commands need to be executed from the git folder. # get current directory currentdir = os.getcwd(); # change the directory to go to github directory, push command doesnt work unless you inside github dir os.chdir(r'%s' % (GITWORKTREE)) output = pexpect.run ('git pull') if "Already up-to-date" in output: #go back to script directory os.chdir(r'%s' % (currentdir)) return "INFORMATION: GitHub pull is already up-to-date" else: pexpect.run ('git add .') pexpect.run ('git commit -m "adding new configuration file"') child = pexpect.spawn ('git push origin master') output = child.expect (['Username:'******'Traceback']) if output == 0: child.sendline (GITHUBUSER) child.expect ('Password:'******'Authentication failed']) if "fatal" in child.before: os.chdir(r'%s' % (currentdir)) return "ERROR: GitHub authentication failed. Please check username and password" elif output != 0: #print child.before os.chdir(r'%s' % (currentdir)) return "FAILED: was not able to upload new configuration file to github"
def test_run_event_typeerror(self): events = [('GO:', -1)] with self.assertRaises(TypeError): pexpect.run('bash --rcfile {0}'.format(self.rcfile), withexitstatus=True, events=events, timeout=10)
def execute(command): """Runs the "command", returns all output. Needs to be very robust.""" import pexpect #log = pexpect.run(command) pexpect.run('bash -c "%s &> /tmp/log"' % command) log = "".join(open("/tmp/log").readlines()) return log
def Connexion(self, m): tr = 0 renderText(m,{'x':5,'y':20,'text':'Waiting connexion in process...'}) pexpect.run('sudo ifdown wlan0') pexpect.run('sudo ifup wlan0') while not self.connected and tr < 5: child = pexpect.spawn('sudo wifi connect -a ' + self.ssid, timeout=100) ret = child.expect([pexpect.TIMEOUT, 'passkey>', pexpect.EOF]) if ret == 1: self.connected = True else: error = child.after if type(child.after) == str else child.before error = ' : ' + error if type(error) == str else '' print '[-] Error no' + str(tr + 1) + error[:-1] tr = tr + 1 time.sleep(1) if not self.connected: print '[-] Connexion failed' clearLine(m, 20) renderText(m, {'x':5,'y':20,'text':'Connexion failed'}) return False child.sendline(self.passkey) ret = child.expect([pexpect.TIMEOUT, r'\s*$', pexpect.EOF]) if ret == 1: print '[+] Connexion successed' return True else: print '[-] Error : ' + str(child.after) return False
def gtx_progress(args): """progress - show rebase progress usage: gtx progress refdir rebasedir """ p = optparse.OptionParser() p.add_option('-d', '--debug', action='store_true', default=False, dest='debug', help='start the debugger') (o, a) = p.parse_args(args) if o.debug: pdb.set_trace() ref = a[0] rebase = a[1] os.chdir(rebase) r = pexpect.run("git --no-pager log --no-color -1") commit = re.findall("commit\s+(\w+)", r) x = r.split("\r\n") needle = x[4].strip() pass os.chdir(os.path.join("..", ref)) r = pexpect.run("gtx depth %s" % commit[0]) if "No commit found" in r: r = pexpect.run("gtx depth -- \"%s\"" % needle) print r pass
def scpWithbackUp(localDir,remoteDir,backupDir,hosts): time=datetime.datetime.now().strftime('%Y%m%d%H%M%S') sp=remoteDir.split('/') name=sp[len(sp)-1] tgz = name+".tar.gz" res=pexpect.run('npm prune --production',cwd='dist') res=pexpect.run('tar -zcvf %s %s '%(tgz,localDir),withexitstatus=1,cwd='.') for host in hosts: scp=pexpect.spawn('scp %s %s:%s'%(tgz,host,'~'),timeout=3000) scp.logfile = sys.stdout scp.expect(pexpect.EOF) #ssh ssh = pexpect.spawn('ssh',[host]) ssh.logfile = sys.stdout ssh.expect(']#') #backupfile ssh.sendline("mv %s %s"%(remoteDir,backupDir+"/"+name+"."+time)) ssh.expect(']#') #unzip ssh.sendline('tar -xvf %s'%(tgz)) ssh.expect(']#') #cleanup ssh.sendline('rm -f %s'%(tgz)) ssh.expect(']#') #replece ssh.sendline('mv %s %s'%(localDir,remoteDir)) ssh.expect(']#')
def _create_wallet(self): print('did not find an existing wallet, creating a new one') #ensure the daemon is stopped, as this causes path errors (mostly usefull for development) pexpect.run('electrum daemon stop') #build a new wallet if no wallet yet exists walletpair=str(subprocess.check_output('python addrgen/addrgen.py',shell=True)) walletpair = re.split('\W+', walletpair) self.address = walletpair[1] self.privkey = walletpair[2] print('created a wallet with address \''+self.address+'\' and privatekey \''+self.privkey+'\'') child = pexpect.spawn('electrum', ['restore', self.privkey]) #respectively: use default password, use default fee (0.002), use default gap limit and give seed self._answer_prompt(child, '') #check if wallet was created succesfulyl command = """electrum listaddresses""" output = pexpect.run(command) walletFinder = re.compile(r'\[\W*"([A-z0-9]+)"\W*\]') result = walletFinder.search(output) #This horrible feedback loop is here due to a quirk of electrum. #Needs refactoring, but do not refactor without extensive testing (i.e. multiple vps all from clean install) #Because electrum behaviour right after startup tends to differ from server to server (i suspect something to do wtih specs) try: print result.group(1) return result.group(1) except: return self._create_wallet()
def view(eq, psviewer="evince"): """Launches a PostScript viewer (default: evince) with the equation. """ import os import pexpect x = tempfile.mktemp() tmp1 = "%s.tex" % x # create a LaTeX document and insert equations f = open(tmp1, "w") f.write(tex_str % eq) f.close() # compile LaTeX document. A DVI file is created cwd = os.getcwd() os.chdir(tempfile.gettempdir()) pexpect.run("latex %s" % tmp1) cmd = "dvips %s.dvi" % (x) pexpect.run(cmd) # remove temporary files os.remove("%s.tex" % x) os.remove("%s.dvi" % x) os.remove("%s.log" % x) os.chdir(cwd) os.system("%s %s.ps &" % (psviewer, x))
def run_the_code(username,commit_id,clone_repo,src_code): target = "/home/ubuntu/workspace/Push_exp/" + commit_id + "/" +clone_repo print target if os.path.isdir(target): print "enter successfully" os.chdir(target) if src_code[src_code.rfind('.'):] == ".java": cmd = "javac %s"%(src_code) os.system(cmd) cmd = "java %s"%(src_code[:src_code.rfind('.')]) c = pexpect.run(cmd) else: c = pexpect.run("python " + src_code) print "result :",c List = db_connect.get_poject_exp_list(username,clone_repo) for a in List["exp_records"]: if a["commit"]["id"] == commit_id: exp = a a["is_run"] = True a["return"] = c OK = db_connect.update_exp(username,clone_repo,List["exp_records"]) print "succefully end" shell = "python /home/ubuntu/workspace/data_set_public/autograder.py --commit %s --repo %s " % (commit_id,clone_repo) os.system(shell) return True raise Exception("can not run the exp code successfully") else: return False
def testjulius(): child= pexpect.spawn ('julius -input mic -C Sample.jconf') child.maxread=8000 i=0 pexpect.run("play pizza_order.wav", cwd=prompt_wav) # wav file containing prompt for pizza order while True: try: if i==0: child.expect("please speak") pass1=findsentence(child.before) if pass1 != None: pizza=pass1 print "Your pizza order is:", pass1 i=i+1 pexpect.run("play side_order.wav", cwd=prompt_wav) # wav file containing containing prompt for side order else: pass if i == 1: child.expect("please speak") pass2=findsentence(child.before) if pass2 != None: side=pass2 print "Your side order is:", side i=i+1 else: pass if i==2: print "Your order is: ",pizza," & ",side return 0 else: testjulius() except KeyboardInterrupt: child.close() pass
def commit(): subprocess.call("""cd /home/main/notebooks/records; git config --global user.email "*****@*****.**"; git config --global user.name "jttalks"; git pull""", shell=True) my_indices = [int(f) for f in os.listdir('/home/main/notebooks/records/') if '.' not in f] new_index = str(max(my_indices)+1) timestamp = datetime.now().ctime() my_data = pd.Series([me.real_name, me.character_name, me.email, me.race, me.house, timestamp]) my_data.to_csv("/home/main/notebooks/records/"+new_index, index=False) subprocess.call("""cd /home/main/notebooks/records; git add *; git commit -m "ADD: signup" """, shell=True) pexpect.run('git push -u origin master', cwd='/home/main/notebooks/records', events={'Username*':'jttalks\n', 'Password*':'jttalks1\n'})
def github_upload(GITDIR,GITWORKTREE,GITHUBUSER,GITHUBPWD): # some git commands need to be executed from the git folder. # get current directory currentdir = os.getcwd(); # change the directory to go to github directory, push command doesnt work unless you inside github dir # git --git-dir=configuration-control-scripts/.git --work-tree=configuration-control-scripts push origin master os.chdir(r'%s' % (GITWORKTREE)) output = pexpect.run ('git status') if "modified" in output or "Your branch is ahead" in output: print "INFORMATION: GitHub status is not uptodate. Push operation will start" pexpect.run ('git add .') pexpect.run ('git commit -m "adding new configuration file"') child = pexpect.spawn ('git push origin master'); output = child.expect (['Username:'******'Traceback']) if output == 0: child.sendline (GITHUBUSER) child.expect ('Password:'******'Authentication failed']) if "fatal" in child.before: os.chdir(r'%s' % (currentdir)) return "ERROR: GitHub authentication failed. Please check username and password" elif output != 0: #print child.before os.chdir(r'%s' % (currentdir)) return "FAILED: GitHub push failed, new configuration file was not uploaded" #go back to script directory os.chdir(r'%s' % (currentdir)) return "SUCCESSFUL: Push updated configuration file to GitHub"
def download(self): #if (self.loginDialog().exec_() != Qt.QDialog.Accepted): # return lines = os.popen("ls -1 /opt/disamiqo/frasi/*.txt").readlines() self.progressBar.setRange(0, len(lines)) self.progressBar.setVisible(True) for n, i in enumerate(lines): i = i.split("\n")[0] i = i.split("/")[-1] try: #foo = pexpect.spawn("scp "+str(self.userEdit.text())+"@lxplus.cern.ch:~sani/public/matteosoftware/"+i+" /opt/disamiqo/frasi/"+i+"_temp") foo = pexpect.spawn("scp [email protected]:matteosoftware/"+i+" /opt/disamiqo/frasi/"+i+"_temp") foo.expect('.assword:') foo.sendline("guest") foo.interact() except: pass self.progressBar.setValue(n) diff = pexpect.run("diff /opt/disamiqo/frasi/"+i+" /opt/disamiqo/frasi/"+i+"_temp") file_temp = open("/opt/disamiqo/frasi/"+i, "a") for line in diff.split("\n"): line = line.split("\r")[0] if (line.find("> ") != -1): line = line[2:] file_temp.write(line+"\n") file_temp.close() rm = pexpect.run("rm /opt/disamiqo/frasi/"+i+"_temp") self.progressBar.setValue(self.progressBar.maximum()) if (Qt.QMessageBox.information(self, "Disamiqo", "Download frasi completato !")): self.progressBar.hide() self.enableOK()
def process(self): self.artifact.generate_workfile() work_file = os.path.basename(self.artifact.work_filename()) artifact_file = os.path.basename(self.artifact.filename()) for e, ext in {"say" : "aiff", "espeak" : "wav"}.items(): sound_file = artifact_file.replace('mp3', ext) # TODO replace pexpect with subprocess tts_bin, s = pexpect.run("which %s" % e, withexitstatus = True) if s == 0: self.log.info("%s text-to-speech found at %s" % (e, tts_bin)) break else: self.log.info("%s text-to-speech not found" % e) e = None if e == "say": command = "say -f %s -o %s" % (work_file, sound_file) elif e == "espeak": command = "espeak -f %s -w %s" % (work_file, sound_file) else: raise Exception("unknown text-to-speech command %s" % e) self.log.info(command) self.artifact.stdout = pexpect.run(command, cwd=self.artifact.artifacts_dir) # Converting to mp3 command = "lame %s %s" % (sound_file, artifact_file) self.log.info(command) self.artifact.stdout = pexpect.run(command, cwd=self.artifact.artifacts_dir)
def test_run(): print("#### run('ls') have not log ####") pexpect.run("ls") # 没有日志输出 print("#### logfilr = run('ls'): log is in logfile ####") log = pexpect.run("ls") print(log), print("#### run('ls', logfile=sys.stdout): log is standard output ####") pexpect.run("ls", logfile=sys.stdout)
def main(args): """ Wrap the "idal" binary with pexpect, in order to fake a terminal for IDA so we could run IDA as a regular command-line command without GUI. :param args: :return: """ pexpect.run('idal ' + args, timeout=600)
def clone(self, timeout=60): """ execute git clone, clone to work dir """ if os.path.exists(self.work_dir): raise Exception("git repo's dir already exist") pexpect.run("mkdir -p %s" % self.work_dir) return self.wait_transfer_end(self.execute('git clone %s .' % self.url, wait=False), timeout)
def interrupt(self): # Need to start a second pexpect to interrupt as root if sudo: pexpect.run('sudo -p "%s" kill -2 %d"' % (self.password_prompt, self.pid), events={self.password_prompt: self.password+'\n'}) else: pexpect.run('su -c "kill -2 %d"' % self.pid, events={':': self.password+'\n'})
def process(self): self.artifact.generate_workfile() times = [] for i in xrange(self.N): start = time.time() pexpect.run("%s %s" % (self.EXECUTABLE, self.artifact.work_filename())) times.append("%s" % (time.time() - start)) self.artifact.data_dict['1'] = "\n".join(times)
def ssh_command(user,ip,password,command): try: #第一次登录ssh(即没有public key)时的提示字符串 ssh_newkey = 'Are you sure you want to continue connecting(yes/no)?' # 为ssh命令生成一个spawn类的子程序对象 child = pexpect.spawn('ssh -l %s %s %s'%(user,ip,command)) #列出期望出现的字符串,超时,ssh_newkey,或'password:'******'password: '******'ssh登录超时:' #打印出错信息 print child.before, child.after return None #如果出现的字符串为ssh_newkey,即第一次登录,没有public key if i == 1: #输入'yes' child.sendline('yes') #列出期望出现的字符串,超时或'password:'******'password: '******'ssh登录超时:' #打印出错信息 print child.before, child.after return None #如果出现的字符串为'password: '******'password',EOF,超时 i = child.expect(['password: '******'password: '******'密码输入错误!' #匹配到了EOF,打印ssh登录成功,并输入命令后成功退出 elif i == 1: print '恭喜,ssh登录输入命令成功!' #匹配到了超时,打印超时 else: print '输入命令后等待超时!' #将执行命令的时间和结果以追加的形式保存到ssh_log.txt文件中备份文件 f = open('ssh_log.txt','a') str1 = str(datetime.datetime.now())+' '+command f.writelines(str1+child.before) f.close() #返回命令的输出结果 result = child.before return result #异常打印原因并删除public key except pexpect.ExceptionPexpect, e: print "ssh连接失败,正在重启进程",str(e) pexpect.run("rm -rf ~/.ssh")
def _executeSync(self): if self._debug: print "ExecSync: %s PWD passed: %s" % (self._commandToExecute, self._password != None) if self._password: (self._output,self._status)=pexpect.run (self._commandToExecute, events={'.*:': self._password+'\n'}, withexitstatus=1) else: (self._output,self._status)=pexpect.run (self._commandToExecute, withexitstatus=1) return (self._output,self._status)
def is_experimental_network_up(self): cl = self.cn_node_ssh +self.debug_ps_file attempt = 1 while attempt < 5: time.sleep(10) out_str = pexpect.run(cl, timeout=20) v1_c = out_str.count('v1') v5_c = out_str.count('v5') self.log_message('-----is_experimental_network_up----') self.log_message('#Hosts='+str(v1_c)+',#switches='+str(v5_c)) self.log_message('-----------------------------------') if v1_c == 39 and v5_c == 39: return True attempt += 1 return False
def scp_package(server): command = "scp -i /Users/yxyang/.ssh/id_rsa -r /Users/yxyang/Downloads/youtube_crawler duke_quantify_openness@%s:~" % server try: output = pexpect.run(command) #i = scp.expect(['not known', 'continue connecting (yes/no)?', '~', 'timed out', 'unreachable', 'keyboard-interactive']) print server print output #output.sendline("yes") #print exit1 time.sleep(3) except Exception as e: print e
def test_html_report(self): """ Try running 'html report > filename' and verify that 1) no traceback occurs and 2) something is actually written to the output file. """ self.dbgfunc() cfpath = self.tmpdir("crawl.cfg") cfg = CrawlConfig.add_config() cfg.crawl_write(open(cfpath, 'w')) cmd = "html report --config %s" % cfpath CrawlConfig.log(cmd, close=True) result = pexpect.run(cmd) if "HPSS Unavailable" in result: pytest.skip("HPSS Unavailable") self.validate_report(result)
def mesh(tipr=0.5, mesha=0.01, meshQ=0.8, verbose=False): s = "".join(open("afm-templ.geo").readlines()) meshgeometry = "../tmp/t.geo" open(meshgeometry, "w").write(s % (tipr)) pexpect.run("gmsh -0 %s -o ../tmp/x.geo" % (meshgeometry)) g = geom.read_gmsh("../tmp/x.geo") if verbose: g.printinfo() geom.write_tetgen(g, "../tmp/t.poly") geom.runtetgen("/home/ondra/femgeom/tetgen/tetgen", "../tmp/t.poly", a=mesha, Q=meshQ, quadratic=True, verbose=verbose) m = geom.read_tetgen("../tmp/t.1", verbose) if verbose: m.printinfo() m.writemsh("../tmp/t12.msh", verbose) m.writexda("../tmp/in.xda", verbose) m.writeBC("../tmp/t12.boundaries", verbose) regions = m.regions nele = len(m.elements) return regions, nele
def run_noauth(self, cmd: str): """ Run the git command that does not require any user authentication """ output, rc = pexpect.run( command=f"{git_bin} {cmd}", withexitstatus=True, cwd=self.repo_dir, encoding="utf-8", ) if rc != 0: raise RuntimeError(f"git {cmd} failed: %s" % output) return output
def final_run(image_id, arch, qemu_dir): runsh_path = os.path.join(firmadyne_path, "scratch", image_id, "run.sh") if not os.path.isfile(runsh_path): print("[!] Cannot emulate firmware, run.sh not generated") return if qemu_dir: if arch == "armel": arch = "arm" elif arch == "mipseb": arch = "mips" print("[+] Using qemu-system-{0} from {1}".format(arch, qemu_dir)) cmd = 'sed -i "/QEMU=/c\QEMU={0}/qemu-system-{1}" "{2}"'.format( qemu_dir, arch, runsh_path) pexpect.run(cmd) print("[+] All set! Press ENTER to run the firmware...") print("[+] When running, press Ctrl + A X to terminate qemu") print("[+] Command line:", runsh_path) run_cmd = ["--", runsh_path] child = pexpect.spawn("sudo", run_cmd, cwd=firmadyne_path) child.sendline(sudo_pass) child.interact()
def exec_cmd(cmd): global debug out = pexpect.run(cmd) if debug: print cmd if debug: print out if "]:" in out: index = out.index("]:") out = out[index + 2:].strip() index = out.index(" ") out = out[:index].strip() return int(out, 16) else: print "ERROR: %s %s" % (cmd, out) return ""
def scp_file_to_host(src_file_name, vmnum=1): ''' scp the given file over to the RackHD host and place it in the home directory. :param src_file_name: name of file to copy over. May include path :type src_file_name: basestring :return: file path on target :rtype: basestring ''' logfile_redirect = file('/dev/null', 'w') just_fname = os.path.basename(src_file_name) port = fitports()['ssh'] # if localhost just copy to home dir if fitargs()['rackhd_host'] == 'localhost' and port == 22: remote_shell('cp ' + src_file_name + ' ~/' + just_fname) return '~/' + just_fname if (vmnum == 1): rackhd_hostname = fitargs()['rackhd_host'] else: rackhd_hostname = fitargs()['rackhd_host'].replace( "ora", "ora-" + str(vmnum - 1)) scp_target = fitcreds()['rackhd_host'][0]['username'] + '@{0}:'.format( rackhd_hostname) cmd = 'scp -P {0} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null {1} {2}'.format( port, src_file_name, scp_target) if VERBOSITY >= 4: print "scp_file_to_host: '{0}'".format(cmd) if VERBOSITY >= 9: logfile_redirect = sys.stdout (command_output, ecode) = pexpect.run(cmd, withexitstatus=1, events={ '(?i)assword: ': fitcreds()['rackhd_host'][0]['password'] + '\n' }, logfile=logfile_redirect) if VERBOSITY >= 4: print "scp_file_to_host: Exit Code = {0}".format(ecode) assert ecode == 0, \ 'failed "{0}" because {1}. Output={2}'.format(cmd, ecode, command_output) return '~/' + just_fname
def do_single_work(self, host): print 'begin host %s work:#######################################################' % host # 登录 ssh_client = pxssh.pxssh() ssh_client.login(host, self.username, self.password) # 删除目标目录 # ssh_client.sendline('rm -rf %s' % self.scptargetdir) # ssh_client.prompt() # print(ssh_client.before) # 建目标目录 # father_path = os.path.abspath(os.path.dirname(self.scptargetdir) + os.path.sep + ".") # ssh_client.sendline('mkdir -p %s'%father_path) # ssh_client.prompt() # print(ssh_client.before) # 复制包到目标机器 # (command_output, exitstatus) = pexpect.run('scp -r %s %s@%s:%s' # %(self.scpsourcedir, self.username, host, self.scptargetdir), withexitstatus=1) # 复制文件到目标位置 for sourcefile in self.scp_map.keys(): # 检查目标目录是否存在,不存在则新建 father_path = os.path.abspath( os.path.dirname(self.scp_map[sourcefile])) if father_path != os.path.abspath('/'): ssh_client.sendline('mkdir -p %s' % father_path) ssh_client.prompt() print(ssh_client.before) # 进行scp操作 (command_output, exit_status) = pexpect.run( 'scp %s %s@%s:%s' % (sourcefile, self.username, host, self.scp_map[sourcefile]), withexitstatus=1) print command_output if exit_status != 0: return # 执行命令 for work_cmd in self.work_list: ssh_client.sendline(work_cmd) ssh_client.prompt() print(ssh_client.before) ssh_client.logout() print 'end host %s work:#######################################################' % host
def get_architecture(firmware_id): """Gets the architecture of the given firmware image.""" print(bcolors.OKBLUE + "[-] Getting the firmware architecture..." + bcolors.ENDC) command = GETARCH_COMMAND.format(FIRMADYNE_PATH, OUTPUT_DIR, firmware_id) print(bcolors.ITALIC + command + bcolors.ENDC) output = pexpect.run(command, events={'Password for user {}:'.format(USER):PASSWORD + '\n'}) # extract the architecture info from the output arch = "" try: arch = output.split('\n')[0].split(':')[1] except: print(bcolors.FAIL + "[!] The firmware architecture couldn't be determined..." + bcolors.ENDC) print(bcolors.ITALIC + "[!] Please try manually with the file command and provide the correct architecture type with the --arch parameter..." + bcolors.ENDC) else: print(bcolors.OKGREEN + "[+] The architecture of your firmware image is:" + arch + bcolors.ENDC) return arch
def test_000_pep8(self): """ Apply pep8 to all .py files in the system and return the result """ full_result = "" for r, d, f in os.walk('hpssic'): pylist = [ os.path.abspath(os.path.join(r, fn)) for fn in f if fn.endswith('.py') and not fn.startswith(".#") ] inputs = " ".join(pylist) if any([r == "./test", ".git" in r, ".attic" in r, "" == inputs]): continue result = pexpect.run("pep8 %s" % inputs) full_result += result.replace(MSG.cov_no_data, "") self.expected("", full_result)
def wait_for_experimental_network_cleanup(self): cl = self.cn_node_ssh +self.debug_ps_file attempt = 1 time.sleep(4) while attempt < 6: time.sleep(2) out_str = pexpect.run(cl, timeout=40) v1_c = out_str.count('v1') v5_c = out_str.count('v5') self.log_message('-----wait_for_experimental_network_cleanup----') self.log_message('#Hosts='+str(v1_c)+',#switches='+str(v5_c)) self.log_message('----------------------------------------------') if v1_c == 0 and v5_c == 0: return True attempt += 1 return False
def parseIONLog(): res = pexpect.run('tail -40 ion.log') print 'Raw results:' print res lines = res.split('\n') count = len(lines) print 'Found %d lines' % (count) for l in lines[::-1]: # Process in reverse order print 'Examining line: %s' % (l) if l.find('end of statistics snapshot...') >= 0: lastLine = count print 'Found lastLine: %d' % (count) if l.find('Start of statistics snapshot...') >= 0: return (lines[count:]) count -= 1 return []
def __run_commands(commands): """Run commands with error detection. :param list commands: The commands to execute. :return: None :rtype: None :raise RuntimeError: If unable to run a command. """ for c in commands: (command_output, exitstatus) = pexpect.run( c, events={"(?i)password": __prompt_for_password()}, withexitstatus=True) if exitstatus != 0: raise RuntimeError("Unable to {0}: {1}".format( c, command_output.strip()))
def clear_all_entities(): """Clear all entities from the server. We must delete all entities on the server in the correct order, first scans, then sources, then credentials. """ error_finder = re.compile("internal server error") errors = [] output = [] for command in ("scan", "source", "cred"): clear_output = pexpect.run("{} {} clear --all".format( client_cmd, command), encoding="utf8") errors.extend(error_finder.findall(clear_output)) output.append(clear_output) assert errors == [], output
def run(self): import pexpect import sys print( "To see output, run tests via py.test directly. " "This will fail on a headless setup." ) output, errno = pexpect.run(' '.join([ sys.executable, 'runtests.py', # Since CircleCI puts the venv in the source tree: '--ignore', 'venv', '-s', ]), withexitstatus=True) print(output.decode('utf-8')) raise SystemExit(errno)
def prepare_folder(folder): """Preparing the folder for storing the backups. :param folder: :return: """ if not os.path.exists(folder): logger.info('~creating folder') os.makedirs(folder, exist_ok=True) (output, ret) = pexpect.run(f'/usr/bin/borg init -e none {folder}', withexitstatus=1) logger.info(f'~ret: {ret}, output: {output}') if ret != 0: raise RuntimeError('could not create and init borg folder') else: logger.info(f'~borg folder {folder} exists')
def getHostMachineDiskInfo(machineAddress): whitespacePattern = re.compile("\s+") # command "df /mnt/images/sfs" output: # Filesystem 1K-blocks Used Available Use% Mounted on # /dev/mapper/vg-sfs 825698728 101306472 682449216 13% /mnt/images/sfs dfCommand = "ssh {_machineAddress} df {shareFileSystemPath}".format( _machineAddress=machineAddress, shareFileSystemPath="/mnt/images/sfs") resultLines = pexpect.run(dfCommand).strip().split("\n") columns = whitespacePattern.split( resultLines[1]) # discard first title line result = dict() result['Remaining'] = int(columns[3]) result['Used'] = float(columns[4][:-1]) return result
def init_child_env(child_dir: Path) -> None: cwd = Path(".").absolute() if child_dir.exists(): shutil.rmtree(child_dir) child_dir.mkdir() os.chdir(str(child_dir)) result = run("envo test --init") assert result == b"\x1b[1mCreated test environment \xf0\x9f\x8d\xb0!\x1b[0m\r\n" comm_file = Path("env_comm.py") content = comm_file.read_text() content = content.replace("parent = None", 'parent = ".."') comm_file.write_text(content) os.chdir(str(cwd))
def test_run_event_as_string(self): events = [ # second match on 'abc', echo 'def' ('abc\r\n.*GO:', 'echo "def"\n'), # final match on 'def': exit ('def\r\n.*GO:', 'exit\n'), # first match on 'GO:' prompt, echo 'abc' ('GO:', 'echo "abc"\n') ] (data, exitstatus) = pexpect.run('bash --rcfile {0}'.format(self.rcfile), withexitstatus=True, events=events, timeout=10) assert exitstatus == 0
def link_urlorg(self, url, sha256fname): foo, fname = self.generate_hash_path(url) if os.path.exists(fname): print '*** in link_urlorg()' print '*** original already exists!', url print '*** destination', fname sys.exit(1) output, excode = pexpect.run('ln %s %s' % (sha256fname, fname), withexitstatus=True) if excode: print '*** in link_urlorg()' print output print print '*** Aborting link_urlorg() due to error!' sys.exit(1) self.log('linked origfile %s' % url, 9)
def getHostMachineUsedMemoryAndManyVmResourceInfo(self, machineAddress): xenVmNamePrefix = "vm" xenVmNamePattern = re.compile(xenVmNamePrefix + "\d+") # ex: vm123445 whitespacePattern = re.compile("\s+") usedMemory = 0 vms = list() # command "xentop -i 2 -d 0.1 -b " output: # NAME STATE CPU(sec) CPU(%) MEM(k) MEM(%) MAXMEM(k) MAXMEM(%) # Domain-0 -----r 232891 0.0 2091520 8.3 no limit n/a # vm2057 --b--- 54 0.0 262144 1.0 262144 1.0 # vm2113 --b--- 54 0.0 262144 1.0 262144 1.0 # vm2132 --b--- 56 0.0 262144 1.0 262144 1.0 # NAME STATE CPU(sec) CPU(%) MEM(k) MEM(%) MAXMEM(k) MAXMEM(%) # Domain-0 -----r 232891 127.3 2091520 8.3 no limit n/a # vm2057 --b--- 54 0.0 262144 1.0 262144 1.0 # vm2113 --b--- 54 0.0 262144 1.0 262144 1.0 # vm2132 --b--- 56 0.2 262144 1.0 262144 1.0 # counting avaliable memory xentopCommand = "ssh {_machineAddress} sudo xentop -i 2 -d 0.1 -b".format( _machineAddress=machineAddress) resultLines = pexpect.run(xentopCommand).strip().split("\n") resultLines = resultLines[(len(resultLines) / 2):] # need strings[middleline~end] for line in resultLines: columns = whitespacePattern.split(line.strip()) if not columns: continue if columns[0] == "Domain-0": # hyperviser resource consume usedMemory = usedMemory + int(columns[4]) elif xenVmNamePattern.match(columns[0]): usedMemory = usedMemory + int(columns[4]) vm = { "vmid": columns[0][len(xenVmNamePrefix):], # vm1234 -> 1234 "hostmachine": machineAddress, "cpuUsage": float(columns[3]), "memoryUsage": float(columns[4]) } vms.append(vm) result = dict() result['Used'] = usedMemory result['VmResourceInfo'] = vms return result
def update_network_info(request): wifi_name = pexpect.run('uci get wireless.default_radio1.ssid') wifi_name = wifi_name.decode("utf-8").strip() mesh_key = pexpect.run('uci get wireless.default_radio0.key') mesh_key = mesh_key.decode("utf-8").strip() net_inf = pexpect.run('nmap -sn --send-ip 192.168.1.0-31') net_inf = net_inf.decode("utf-8") print(net_inf) ip_list = re.findall(r'[0-9]+(?:\.[0-9]+){3}', net_inf) mac_list = re.findall(r'[a-fA-F0-9:]{17}|[a-fA-F0-9]{12}', net_inf) name_list = [] for mac in mac_list: name_list.append(net_inf[net_inf.find(mac) + len(mac) + 2:net_inf.find(')', net_inf.find(mac))]) hostname = pexpect.run('uci get system.@system[0].hostname').decode( "utf-8").strip() ip = pexpect.run('uci get network.lan.ipaddr').decode("utf-8").strip() mac_addr = pexpect.run('ifconfig br-lan').decode("utf-8") mac_addr = mac_addr[mac_addr.find('HWaddr ') + 7:mac_addr.find('HWaddr ') + 24] ip_list.insert(0, ip) mac_list.insert(0, mac_addr) name_list.insert(0, hostname) aps = [] for ip, mac, name in zip(ip_list, mac_list, name_list): temp = {} temp['apIP'] = ip temp['apMAC'] = mac temp['apType'] = name aps.append(temp) data = {} data['wifiName'] = wifi_name data['meshKey'] = mesh_key data['aps'] = aps global ACCESS_POINT_LIST if ACCESS_POINT_LIST != aps: print("ACCESS POINT LIST UPDATED") ACCESS_POINT_LIST = aps with open('network_info.dat', 'w+') as fp: fp.write(json.dumps(data)) return data
def run_wrapper(cli_cmd, timeout=5): try: child_result, child_exitstatus = pexpect.run( "/bin/bash -c '{0}'".format(cli_cmd), timeout=timeout, withexitstatus=True) print("Exit status: {0}".format(child_exitstatus)) if child_exitstatus is None or child_exitstatus != 0: print("Fail: The response to the command was: {0}".format( child_result.decode().strip())) else: print("Success! Response:\n{0}".format( child_result.decode().strip())) except pexpect.ExceptionPexpect: print(sys.exc_info()) except TypeError: print(sys.exc_info())
def _get_tester_ip(self): serverip = self._get_serverip() monip = fit_common.fitcfg()["rackhd-config"]["apiServerAddress"] cmd = "ping -R -c 1 " + monip + "" (command_output, exitstatus) = pexpect.run( "ssh -q -o StrictHostKeyChecking=no -t " + fit_common.fitcfg()["image_service"]['usr'] + "@" + serverip + " sudo bash -c \\\"" + cmd + "\\\"", withexitstatus=1, events={ "assword": fit_common.fitcfg()["image_service"]['pwd'] + "\n" }, timeout=300) uud = command_output.split("\t") myip = uud[1].split("\r\n")[0] logs.debug('My IP address is: ' + myip) return myip
def getHostMachineTotalMemory(machineAddress): whitespacePattern = re.compile("\s+") # see capacity memory lessMeminfoCommand = "ssh {_machineAddress} less {meminfoFile}".format( _machineAddress=machineAddress, meminfoFile="/proc/meminfo") print(lessMeminfoCommand) resultLines = pexpect.run(lessMeminfoCommand).strip().split("\n") targetLinePattern = re.compile("^MemTotal:") memoryTotal = 0 for line in resultLines: if targetLinePattern.match(line): columns = whitespacePattern.split( line) # line will like "MemTotal: 452454543 kB" memoryTotal = int(columns[1]) break return memoryTotal
def test_0_pep8(self): """ Check code for pep8 conformance """ self.dbgfunc() full_result = "" for r, d, f in os.walk('.'): pylist = [ os.path.abspath(os.path.join(r, fn)) for fn in f if fn.endswith('.py') and not fn.startswith(".#") ] inputs = " ".join(pylist) if any([r == "./test", ".git" in r, ".attic" in r, "" == inputs]): continue result = pexpect.run("pep8 %s" % inputs) full_result += result.replace(hx.msg.cov_no_data, "") self.expected("", full_result)
def _spawn_subshell(self, *, run: str, print_lines: List[str]): if not click.confirm("Spawn a shell to fix the problem?", default=True): return old_cwd = os.getcwd() try: os.chdir(str(self.path)) out = pexpect.run(run) sys.stdout.write(out.decode()) for line in print_lines: gprint(line) os.system(os.getenv("SHELL", "/bin/bash")) except Exception as exc: # pylint: disable=broad-except print(exc) finally: os.chdir(old_cwd) confirm("Confirm the problem has been fixed")
def encodeText(self, nameGpg, user, message): ''' encode the text ''' fileName = self.createRandom() + '.' + user + '.temp' file = open(fileName, 'w') file.write(message) file.close() cmd = 'gpg -a -e -r "' + nameGpg + '" ' + fileName result = pexpect.run(cmd) os.remove(fileName) match = re.search('(.*)encryption failed: public key not found(.*)', result) if not (match is None): return _('Can\'t encrypt it. (Public key not found)') file = open(fileName + '.asc', 'r') encodedText = file.read() os.remove(fileName + '.asc') return encodedText
def test_app_loadable_elf(env, extra_data): rel_project_path = os.path.join('tools', 'test_apps', 'system', 'gdb_loadable_elf') app_files = ['gdb_loadable_elf.elf'] app = ttfw_idf.LoadableElfTestApp(rel_project_path, app_files, target="esp32") idf_path = app.get_sdk_path() proj_path = os.path.join(idf_path, rel_project_path) elf_path = os.path.join(app.binary_path, 'gdb_loadable_elf.elf') esp_log_path = os.path.join(proj_path, 'esp.log') with SerialThread(esp_log_path): openocd_log = os.path.join(proj_path, 'openocd.log') gdb_log = os.path.join(proj_path, 'gdb.log') gdb_args = '-x {} --directory={}'.format( os.path.join(proj_path, '.gdbinit.ci'), os.path.join(proj_path, 'main')) with ttfw_idf.OCDProcess(openocd_log), ttfw_idf.GDBProcess( gdb_log, elf_path, app.target, gdb_args) as gdb: i = gdb.pexpect_proc.expect_exact([ 'Thread 1 hit Temporary breakpoint 2, app_main ()', 'Load failed' ]) if i == 0: Utility.console_log('gdb is at breakpoint') elif i == 1: raise RuntimeError('Load has failed. Please examine the logs.') else: Utility.console_log('i = {}'.format(i)) Utility.console_log(str(gdb.pexpect_proc)) # This really should not happen. TIMEOUT and EOF failures are exceptions. raise RuntimeError( 'An unknown error has occurred. Please examine the logs.') gdb.pexpect_proc.expect_exact('(gdb)') gdb.pexpect_proc.sendline('b esp_restart') gdb.pexpect_proc.sendline('c') gdb.pexpect_proc.expect_exact( 'Thread 1 hit Breakpoint 3, esp_restart ()') if pexpect.run('grep "Restarting now." {}'.format(esp_log_path), withexitstatus=True)[1]: raise RuntimeError('Expected output from ESP was not received')