def Fvx_vg_create(self, lvm, uniq_lvm): if len(uniq_lvm) == len(self.dev)/3: tmp = [] while len(self.dev)>0: pseudoName = self.dev.pop(0)[:-1] pseudoName = re.search('pseudo name=(emcpower\w)', pseudoName, re.I).group(1) lunName = self.dev.pop(0)[:-1] lunName = self.dev.pop(0)[:-1] lunName = re.search('Logical device ID=.*\[(.*?)\]', lunName, re.I).group(1) if lunName in tmp: break if lunName not in lvm.keys(): for k in lvm.keys(): if k.startswith(lunName): lunName=k tmp.append(lunName) if self.config['Share_FS'].upper() == 'YES': share = '-s' else: share = ' ' for k,v in lvm[lunName].iteritems(): self.logger.error('Running: vxdg %s init %s %s' % (share, v.split(',')[1], pseudoName)) if getstatus('/opt/VRTS/bin/vxdg %s init %s %s %s' % (share, v.split(',')[1], pseudoName, self.log))[0] != 0: self.logger.error('Faild to run: \'/opt/VRTS/bin/vxdg %s init %s %s\'' % (share, v.split(',')[1], pseudoName)) self.logger.error('Try to add the disk %s to %s group' %(pseudoName, v.split(',')[1])) if getstatus('/opt/VRTS/bin/vxdg -g %s adddisk %s %s' %( v.split(',')[1], pseudoName, self.log))[0] != 0: self.logger.error('Faild to run: \'/opt/VRTS/bin/vxdg -g %s adddisk %s %s\'' %( v.split(',')[1], pseudoName, self.log)) raise Exception, '%113%' else: self.logger.error('successfully: vxdg -g %s adddisk %s' % (v.split(',')[1], pseudoName)) else: self.logger.error('successfully: vxdg init share %s %s' % (v.split(',')[1], pseudoName))
def OtherGridscripts(self): #Counter=0 for other in self.OtherHosts: self.logger.error('Running: ssh %s %s/orainstRoot.sh' %(other, self.Inventory)) status=getstatus('ssh %s %s/orainstRoot.sh' %(other, self.Inventory)) CheckStatus(status=status, stage='orainstRoot.sh') self.logger.error('Successfully: ssh %s %s/orainstRoot.sh' %(other, self.Inventory)) self.logger.error('Running: ssh %s %s/root.sh' %(other, self.GridHome)) status=getstatus('ssh %s %s/root.sh' %(other, self.GridHome)) self.logger.error('Successfully: ssh %s %s/root.sh' %(other, self.GridHome)) Log=getstatus("ssh %s ls -tr %s/install/root_%s*.log " %(other, self.GridHome, other)) if Log[0] is 0: Log=getstatus("ssh %s ls -tr %s/install/root_%s*.log |tail -1 " %(other, self.GridHome, other)) Counter,Flag = CheckLog(Log=Log[1], Line='"succeeded"',\ rsh='ssh', other=other, Add='|grep Infrastructure|grep Configure') if Flag != 0: if Flag == 1: self.logger.error("Successfully: Finish Grid scripts on the local server") elif Flag == 2: self.logger.error("Failed: Grid Infrastracture Installation") raise Exception, 'Failed: Grid Infrastracture Installation' else: self.logger.error("Failed: RAC GRID scripts on %s server" % other) self.logger.error("Failed: The Status is Unknown the log file didnt exist" % other) raise Exception, 'Failed: RAC GRID scripts'
def test_download(vm, params): """ Test command download """ add_ref = params.get("gf_add_ref", "disk") readonly = "yes" == params.get("gf_add_readonly") gf = utils_test.libguestfs.GuestfishTools(params) if add_ref == "disk": image_path = params.get("image_path") gf.add_drive_opts(image_path, readonly=readonly) elif add_ref == "domain": vm_name = params.get("main_vm") gf.add_domain(vm_name, readonly=readonly) gf.run() gf.do_mount("/") gf.write("/src.txt", "Hello World") src_size = gf.filesize("/src.txt").stdout.strip() dest = "%s/dest.txt" % data_dir.get_tmp_dir() gf.download("/src.txt", "%s" % dest) gf.close_session() content = commands.getoutput("cat %s" % dest) commands.getstatus("rm %s" % dest) if content != "Hello World": raise error.TestFail("Content or filesize is not match")
def RFitOracleBZ2(self): i=2 for other in self.OtherHosts: self.logger.error('Runing: /bin/cp %s/log/ServerName %s/log/%s on %s'\ %(self.OracleHome, self.OracleHome, other, other)) status=getstatus('ssh %s \'su oracle -c "/bin/cp -rf %s/log/ServerName %s/log/%s" \'' \ %(other, self.OracleHome, self.OracleHome, other)) CheckStatus(status=status, stage='successfully: /bin/cp -rf %s/log/ServerName %s/log/%s on %s'\ %(self.OracleHome, self.OracleHome, other, other)) self.logger.error('Runing: /bin/cp -rf /u01/oracle/diag/rdbms/appdb/OraSid /u01/oracle/diag/rdbms/appdb/%s%s on %s'\ %(self.SchemaName, i, other)) status=getstatus('ssh %s \'su oracle -c "/bin/cp -rf /u01/oracle/diag/rdbms/appdb/OraSid \ /u01/oracle/diag/rdbms/appdb/%s%s" \'' %(other, self.SchemaName, i)) CheckStatus(status=status,stage='successfully:/bin/cp -rf /u01/oracle/diag/rdbms/appdb/OraSid /u01/oracle/diag/rdbms/appdb/%s%s on %s' %(self.SchemaName, i, other)) self.logger.error('Runing: /bin/rm -f %s/dbs/hc_APPDB1.dat on %s' %(self.OracleHome, other)) status=getstatus('ssh %s \'su oracle -c "/bin/rm -f %s/dbs/hc_APPDB1.dat" \'' %(other, self.OracleHome)) CheckStatus(status=status,stage='successfully:/bin/rm -f %s/dbs/hc_APPDB1.dat on %s' %(self.OracleHome, other)) self.logger.error('Runing: /bin/rm -f %s/dbs/orapwAPPDB1 on %s' %(self.OracleHome, other)) status=getstatus('ssh %s \'su oracle -c "/bin/rm -f %s/dbs/orapwAPPDB1" \'' %(other, self.OracleHome)) CheckStatus(status=status,stage='successfully:/bin/rm -f %s/dbs/orapwAPPDB1 on %s' %(self.OracleHome, other)) self.logger.error('Runing: ln -sf /data/oracle/APPDB/INIT/orapwAPPDB %s/dbs/orapw%s%s on %s' \ %(self.OracleHome, self.SchemaName, i, other)) status=getstatus('ssh %s \'su oracle -c "ln -sf /data/oracle/APPDB/INIT/orapwAPPDB %s/dbs/orapw%s%s" \'' \ %(other, self.OracleHome, self.SchemaName, i)) CheckStatus(status=status,stage='successfully:ln -sf /data/oracle/APPDB/INIT/orapwAPPDB %s/dbs/orapw%s%s on %s' \ %(self.OracleHome, self.SchemaName, i, other)) i = i + 1
def clean(self): VGs = [] for FS_Topology in self.config['FS_Topology']: for k,v in FS_Topology.iteritems(): if v['VG'] not in VGs: VGs.append(v['VG']) self.logger.error('Starting to clean old LVM from the system') self.logger.error('Trying to vgimport %s' % v['VG']) if getstatus('%s %s %s' % (self.config['vgimport'], v['VG'], self.log))[0] != 0: self.logger.error('vgimport was not needed - This step can be ignored') else: self.logger.error('successfully: vgimport %s' % v['VG']) self.logger.error('Trying to vgchange %s' % v['VG']) if getstatus('%s %s %s' % (self.config['Disable_VG'], v['VG'], self.log))[0] != 0: self.logger.error('vgchange was not needed - This step can be ignored') else: self.logger.error('successfully: vgchange %s' % v['VG']) self.logger.error('Trying to vgremove %s' % v['VG']) if getstatus('%s %s %s' % (v['vgremove'], v['VG'], self.log))[0] != 0: self.logger.error('vgremove was not needed - This step can be ignored') else: self.logger.error('successfully: vgremove %s' % v['VG']) self.logger.error('%s %s' % (v['pvremove'],v['pseudo'])) if getstatus('%s %s %s' % (v['pvremove'],v['pseudo'], self.log))[0] != 0: self.logger.error('pvremove was not needed - This step can be ignored') else: self.logger.error('successfully: %s %s' % (v['pvremove'],v['pseudo']))
def cereal(self): bz2.BZ2Compressor(compresslevel=[1-9]) commands.getstatusoutput(1,null) commands.getstatus(0,null) #bz2.BZ2File(Medussa[mode=self.terminating, compress = true, compresslevel=[1-9]]) def user(self): automota.self(classmethod) world("\t\t\z\y %z /x5z/").author(serialize(self(setattr))) while world < destroyed: #create argus Cluster Paradox.resolved == true #end def create(self): HTMLParser def solid(self): #crypt.crypt.__doc__ def state(self): ConfigParser def cluster(self): ColorPicker("0x1239879") commands #continue def paradox(self): #crypt.crypt.__doc__ def select(self): #crypt.crypt.__doc__ def anomaly(self): crypt.crypt(self) def space(self): #crypt.crypt.__doc__ def dimension(self):#buildtools,afxres,ConfigParser,Cookie print "World"
def Runtar(self): for other in self.OtherHosts: status=getstatus('ssh %s /bin/ls %s' %(other, self.bz2)) CheckStatus(status=status, stage='check if the file %s exist in %s ' % (self.bz2, other)) self.logger.error('ssh %s "cd /;tar xvf %s"' %(other, self.bz2)) status=getstatus('ssh %s "cd /;tar xvf %s"' %(other, self.bz2)) CheckStatus(status=status, stage='Succesfully: ssh %s "cd /;tar xvfj %s"' % (self.bz2, other))
def LFitOracleBZ2(self): self.logger.error('Runing: /bin/cp -rf %s/log/ServerName %s/log/%s' \ %(self.OracleHome, self.OracleHome, os.environ['HOSTNAME'])) status=getstatus('su -c "/bin/cp -rf %s/log/ServerName %s/log/%s" oracle' \ %(self.OracleHome, self.OracleHome, os.environ['HOSTNAME'])) CheckStatus(status=status, stage='successfully: /bin/cp %s/log/ServerName %s/log/%s'\ %(self.OracleHome, self.OracleHome, os.environ['HOSTNAME'])) self.logger.error('Runing: /bin/cp -rf /u01/oracle/diag/rdbms/appdb/OraSid /u01/oracle/diag/rdbms/appdb/%s1' \ %(self.SchemaName)) status=getstatus('su -c "/bin/cp -rf /u01/oracle/diag/rdbms/appdb/OraSid /u01/oracle/diag/rdbms/appdb/%s1" oracle'\ %(self.SchemaName)) CheckStatus(status=status,stage='successfully:/bin/cp /u01/oracle/diag/rdbms/appdb/OraSid /u01/oracle/diag/rdbms/appdb/%s1'\ %(self.SchemaName)) self.logger.error('Runing: /bin/rm -f %s/dbs/hc_APPDB1.dat' %(self.OracleHome)) status=getstatus('su -c "/bin/rm -f %s/dbs/hc_APPDB1.dat" oracle' %(self.OracleHome)) CheckStatus(status=status,stage='successfully:/bin/rm -f %s/dbs/hc_APPDB1.dat' %(self.OracleHome)) self.logger.error('Runing: /bin/rm -f %s/dbs/orapwAPPDB1' %(self.OracleHome)) status=getstatus('su -c "/bin/rm -f %s/dbs/orapwAPPDB1" oracle' %(self.OracleHome)) CheckStatus(status=status,stage='successfully:/bin/rm -f %s/dbs/orapwAPPDB1' %(self.OracleHome)) self.logger.error('Runing: ln -sf /data/oracle/APPDB/INIT/orapwAPPDB %s/dbs/orapw%s1'\ %(self.OracleHome, self.SchemaName)) status=getstatus('su -c "ln -sf /data/oracle/APPDB/INIT/orapwAPPDB %s/dbs/orapw%s1" oracle' \ %(self.OracleHome, self.SchemaName)) CheckStatus(status=status,stage='successfully:ln -sf /data/oracle/APPDB/INIT/orapwAPPDB %s/dbs/orapw%s1'\ %(self.SchemaName, self.OracleHome))
def InstanceToGrid(self): i=2 self.logger.error('Starting to config the instance') self.logger.error('Running: %s/bin/srvctl add database -d %s -o %s -p /data/oracle/APPDB/INIT/spfileAPPDB.ora \ -n %s -y manual' %(self.OracleHome, self.SchemaName, self.OracleHome, self.SchemaName)) status=getstatus('su -c "%s/bin/srvctl add database -d %s -o %s -p /data/oracle/APPDB/INIT/spfileAPPDB.ora -n %s\ -y manual" oracle' %(self.OracleHome, self.SchemaName, self.OracleHome, self.SchemaName)) CheckStatus(status=status, stage='Successfully: %s/bin/srvctl add database -d %s -o %s \ -p /data/oracle/APPDB/INIT/spfileAPPDB.ora \ -n %s -y manual' %(self.OracleHome, self.SchemaName, self.OracleHome, self.SchemaName)) self.logger.error('Running: %s/bin/srvctl add instance -d %s -i %s1 -n %s'\ %(self.OracleHome, self.SchemaName, self.SchemaName.strip(), os.environ['HOSTNAME'])) status=getstatus('su -c "%s/bin/srvctl add instance -d %s -i %s1 -n %s" oracle' \ %(self.OracleHome, self.SchemaName, self.SchemaName.strip(), os.environ['HOSTNAME'])) CheckStatus(status=status, stage='successfully: %s/bin/srvctl add instance -d %s -i %s1 -n %s'\ %(self.OracleHome, self.SchemaName, self.SchemaName.strip(), os.environ['HOSTNAME'])) for other in self.OtherHosts: self.logger.error('Runing: %s/bin/srvctl add instance -d %s -i %s%s -n %s'\ %(self.OracleHome, self.SchemaName, self.SchemaName.strip(), i, other)) status=getstatus('su -c "%s/bin/srvctl add instance -d %s -i %s%s -n %s" oracle' \ %(self.OracleHome, self.SchemaName, self.SchemaName.strip(), i, other)) CheckStatus(status=status, stage='successfully: %s/bin/srvctl add instance -d %s -i %s%s -n %s'\ %(self.OracleHome, self.SchemaName, self.SchemaName.strip(), i, other)) i = i + 1 self.logger.error('Runing: %s/bin/srvctl start database -d %s' %(self.OracleHome, self.SchemaName)) status=getstatus('su -c "%s/bin/srvctl start database -d %s" oracle' \ %(self.OracleHome, self.SchemaName)) CheckStatus(status=status, stage='successfully: %s/bin/srvctl start database -d %s'\ %(self.OracleHome, self.SchemaName))
def test_download_offset(vm, params): """ Test command download-offset """ add_ref = params.get("gf_add_ref", "disk") readonly = params.get("gf_add_readonly", "no") gf = utils_test.libguestfs.GuestfishTools(params) image_path = params.get("image_path") gf.add_drive_opts(image_path, readonly=readonly) gf.run() gf.do_mount("/") string = "Hello World" gf.write("/src.txt", string) src_size = gf.filesize("/src.txt").stdout.strip() dest = "%s/dest.txt" % data_dir.get_tmp_dir() gf.download_offset("/src.txt", "%s" % dest, 0, len(string)) gf.close_session() content = commands.getoutput("cat %s" % dest) commands.getstatus("rm %s" % dest) if content != "Hello World": raise error.TestFail("Content or filesize is not match")
def test_download_offset(vm, params): """ Test command download-offset """ add_ref = params.get("gf_add_ref", "disk") readonly = params.get("gf_add_readonly", "no") gf = utils_test.libguestfs.GuestfishTools(params) if add_ref == "disk": image_path = params.get("image_path") gf.add_drive_opts(image_path, readonly=readonly) elif add_ref == "domain": vm_name = params.get("main_vm") gf.add_domain(vm_name, readonly=readonly) gf.run() gf.do_mount("/") string = "Hello World" gf.write("/src.txt", string) src_size = gf.filesize("/src.txt").stdout.strip() dest = "%s/dest.txt" % data_dir.get_tmp_dir() gf.download_offset("/src.txt", "%s" % dest, 0, len(string)) gf.close_session() content = commands.getoutput("cat %s" % dest) commands.getstatus("rm %s" % dest) if content != "Hello World": raise error.TestFail("Content or filesize is not match")
def __call__(self, *args, **kwargs): self.logger.error('Preforming LMV scan by running: \'vgscan\', \'lvscan\'') if getstatus('%s %s' % (self.config['vgscan'],self.log))[0] != 0: raise Exception, 'Faild to run: \'vgscan\'' self.logger.error('successfully: vgscan') if getstatus('%s %s' % (self.config['lvscan'],self.log))[0] != 0: raise Exception, 'Faild to run: \'lvscan\'' self.logger.error('successfully: lvscan')
def Interconnect(self): for inter in self.HeartBit.split(','): status=getstatus('%s/bin/oifcfg getif|grep cluster_interconnect|grep -v %s'%(self.GridHome, inter)) if status[0] == 0: status=getstatus('%s/bin/oifcfg setif -global %s/%s:cluster_interconnect'%(self.GridHome, inter,\ self.InterConnectNetworkAddres)) CheckStatus(status=status, stage='Succesfully: %s/bin/oifcfg setif -global %s/%s:cluster_interconnect'\ %(self.GridHome, inter, self.InterConnectNetworkAddres))
def Fvx_volume_create2(self,lvm,NonStandartVol={}): for k,hash in lvm.iteritems(): if not NonStandartVol.has_key(k): if not re.search('NONE_[0-9]+' ,hash.keys()[0]) : Size = getstatus('/opt/VRTS/bin/vxassist -g %s maxsize layout=concat' %(hash.values()[0].split(',')[1]))[1].split(':')[1].split()[0] self.logger.error('Running: vxassist -g %s make %s %s' % (hash.values()[0].split(',')[1], hash.keys()[0], Size)) if getstatus('/opt/VRTS/bin/vxassist -g %s make %s %s %s' % (hash.values()[0].split(',')[1], hash.keys()[0], Size, self.log))[0] != 0: self.logger.error('Faild to run: \'/opt/VRTS/bin/vxassist -g %s make %s %s\'' % (hash.values()[0].split(',')[1], hash.keys()[0], Size)) raise Exception, '%113%'
def Fvxdg_deport(self, lvm): for k,hash in lvm.iteritems(): self.logger.error('Checking if %s need to deport' %( hash.values()[0].split(',')[1].strip())) self.logger.error('Running: /opt/VRTS/bin/vxdg list %s' %( hash.values()[0].split(',')[1].strip())) if getstatus('/opt/VRTS/bin/vxdg list %s' %( hash.values()[0].split(',')[1].strip()))[0] == 0: self.logger.error('Running: /opt/VRTS/bin/vxdg deport %s' %( hash.values()[0].split(',')[1].strip())) if getstatus(' /opt/VRTS/bin/vxdg deport %s %s' %( hash.values()[0].split(',')[1].strip(), self.log))[0] != 0: self.logger.error('Faild to run: \'/opt/VRTS/bin/vxdg deport %s' %( hash.values()[0].split(',')[1].strip())) raise Exception, '%113%'
def makeDirsAndPer(self): for k,v in self.config['DIR_Topology'].iteritems(): CHowner = getstatus('\chown %s:%s %s' %( v['USER'],v['GROUP'],k)) if CHowner[0] != 0: raise Exception, 'Failed Changing Owner to %s %s : %s\n%s' %(k,v['USER'],v['GROUP'], CHowner[1]) self.logger.error('Changing Owner to %s %s : %s' %(k,v['USER'],v['GROUP'])) CHmod = getstatus('\chmod %s %s' %( v['CHMOD'],k)) if CHmod[0] != 0: raise Exception, 'Failed Changing mode to %s %s\n%s' %(k,v['CHMOD'], CHowner[1])
def reloadHBA(hba, log=''): time.sleep(5) if getstatus('%s -v -r %s %s' % (modprobe, hba, log))[0] != 0: return False time.sleep(10) if getstatus('%s -v -i %s %s' % (modprobe, hba, log))[0] != 0: return False time.sleep(5) return True
def OfflineBackup(TIMESTAMP): DBNAME = os.getenv("DBNAME") #Get DBNAME what to do if more than one db? if DBNAME == None: print "Fail To Get DBNAME" return False DB2_DIR = os.getenv("DB2_DIR") #Get DB2 BACKUP Directory if DB2_DIR == None: print "Fail To Get DB2_DIR" return False else: if os.path.isdir(DB2_DIR): pass else: print "Backup Directiory Does Not Exist" return False Filenames = os.listdir(DB2_DIR) # shi fou he li? for filename in Filenames: if filename.find(DBNAME) and filename.find(TIMESTAMP): break else: print "No Backup for Specific TimeStamp" return False Result = commands.getstatusoutput( 'db2 force applications all') #Stop Applications if Result[0] == 1024: print "No start database manager command was issued" return False elif Result[0] == 0: Result = commands.getstatusoutput('db2 list applications') if Result[0] == 0: commands.getstatus('db2 force applications all') else: print 'Applications Stopped' cmd = 'db2 restore database ' + DBNAME + ' from ' + DB2_DIR + ' taken at ' + TIMESTAMP try: child = pexpect.spawn(cmd) #restore backup child.logfile = sys.stdout child.expect('continue') child.sendline('y') child.expect(pexpect.EOF) cmd = 'db2 rollforward database ' + DBNAME + ' to end of logs and stop' Result = commands.getstatusoutput(cmd) #rollback log if Result[0] == 0: return True else: print 'Restore Fail' except Exception as e: print e sys.exit()
def OfflineBackup(TIMESTAMP): DBNAME=os.getenv("DBNAME") #Get DBNAME what to do if more than one db? if DBNAME==None: print "Fail To Get DBNAME" return False DB2_DIR=os.getenv("DB2_DIR") #Get DB2 BACKUP Directory if DB2_DIR==None: print "Fail To Get DB2_DIR" return False else: if os.path.isdir(DB2_DIR): pass else: print "Backup Directiory Does Not Exist" return False Filenames=os.listdir(DB2_DIR) # shi fou he li? for filename in Filenames: if filename.find(DBNAME) and filename.find(TIMESTAMP): break else: print "No Backup for Specific TimeStamp" return False Result=commands.getstatusoutput('db2 force applications all') #Stop Applications if Result[0]==1024: print "No start database manager command was issued" return False elif Result[0]==0: Result=commands.getstatusoutput('db2 list applications') if Result[0]==0: commands.getstatus('db2 force applications all') else: print 'Applications Stopped' cmd='db2 restore database '+DBNAME+' from '+DB2_DIR+' taken at '+TIMESTAMP try: child=pexpect.spawn(cmd) #restore backup child.logfile=sys.stdout child.expect('continue') child.sendline('y') child.expect(pexpect.EOF) cmd='db2 rollforward database '+DBNAME+' to end of logs and stop' Result=commands.getstatusoutput(cmd) #rollback log if Result[0]==0: return True else: print 'Restore Fail' except Exception as e: print e sys.exit()
def main(): cmd='ls' d=commands.getoutput(cmd) # 此命令在python3 中不能使用 # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息,也就是说不能“t=system('pwd')”这样使用,只能下面这种使用 system(pwd) # pwd 可以不加引号 # 如果再命令行下执行,结果直接打印出来 os.system('pwd') #pwd 必须加引号 #下面这样使用,虽说可以,但是t捕获到的仅仅是执行后的返回码,0代表成功,否则是其他的返回值,是一个int类型数值 t=os.system('pwd') # 该方法不但执行命令还返回执行后的信息对象 #好处在于:将返回的结果赋于一变量,便于程序的处理。 tmp = os.popen('ls *.py').readlines() #注意: 当执行命令的参数或者返回中包含了中文文字,那么建议使用subprocess,如果使用os.popen则会出现编码错误 #1、当你对shell命令的输出不感兴趣,只希望程序被运行,你可以典型的使用subprocess.call #2、如果你需要捕获命令的输出结果,那么你就需要使用subprocess.Popen #注意,以下才是重点: #在subprocess.call与Popen之间,存在一个非常大的区别。 #subprocess.call会封锁对响应的等待,而subprocess.Popen则不会!! #如果程序\子进程没有响应,python不理它,继续执行python语句,而Popen会等待,知道子进程输出结果才执行下一步语句。 import subprocess subprocess.call (["echo", "arg1", "arg2"],shell=True) #获取返回和输出: #利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe) p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in p.stdout.readlines(): print (line.decode('utf8')) retval = p.wait() if PY3: subprocess.getoutput('date') #date 是shell是一个命令 #Out[4]: '2016年 05月 24日 星期二 09:12:06 CST' subprocess.getstatusoutput('date') #Out[5]: (0, '2016年 05月 24日 星期二 09:12:22 CST') else: #四、使用模块commands import commands #只返回执行的结果, 忽略返回值. commands.getoutput("date") #用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result). cmd执行的方式是{ cmd ; } 2>&1, 这样返回结果里面就会包含标准输出和标准错误. commands.getstatusoutput("date") #返回ls -ld file执行的结果.参数必须是一个文件 commands.getstatus(file)
def sr_iov_cleanup(self): """ Clean up the sriov setup Check if the PCI hardware device drive is loaded with the appropriate, parameters (none of VFs), and if it's not, perform cleanup. @return: True, if the setup was completed successfuly, False otherwise. """ # Check if the host support interrupt remapping kvm_re_probe = False if self.kvm_params is not None: if (self.auai_path and open(self.auai_path, "r").read().strip() == "Y"): if self.kvm_params and self.kvm_params[self.auai_path] == "N": kvm_re_probe = True else: kvm_re_probe = True # Try to re probe kvm module with interrupt remapping support if kvm_re_probe: kvm_arch = kvm_control.get_kvm_arch() utils.system("modprobe -r %s" % kvm_arch) utils.system("modprobe -r kvm") cmd = "modprobe kvm" if self.kvm_params: for i in self.kvm_params: if self.kvm_params[i] == "Y": params_name = os.path.split(i)[1] cmd += " %s=1" % params_name logging.info("Loading kvm with: %s" % cmd) try: utils.system(cmd) except Exception: logging.debug("Failed to reload kvm") utils.system("modprobe %s" % kvm_arch) re_probe = False s = commands.getstatus('lsmod | grep %s' % self.driver) if s: os.system("modprobe -r %s" % self.driver) re_probe = True else: return True # Re-probe driver with proper number of VFs if re_probe: cmd = "modprobe %s" % self.driver logging.info("Loading the driver '%s' without option", self.driver) s = commands.getstatus(cmd) utils.system("/etc/init.d/network restart", ignore_status=True) if s: return False return True
def StopHome(self): self.logger.error('Running: %s/bin/srvctl stop home -o %s -s %s/Grid_%s.stat -n %s' \ %(self.GridHome, self.GridHome, self.SWLocation, os.environ['HOSTNAME'],os.environ['HOSTNAME'])) status=getstatus('%s/bin/srvctl stop home -o %s -s %s/Grid_%s.stat -n %s' \ %(self.GridHome, self.GridHome, self.SWLocation, os.environ['HOSTNAME'],os.environ['HOSTNAME'])) CheckStatus(status=status, stage='Succesfully finish command on %s' % os.environ['HOSTNAME']) for other in self.OtherHosts: self.logger.error('Running: ssh %s %s/bin/srvctl stop home -o %s -s %s/Grid_%s.stat -n %s' \ %(other, self.GridHome, self.GridHome, self.SWLocation, other,other)) status=getstatus('ssh %s %s/bin/srvctl stop home -o %s -s %s/Grid_%s.stat -n %s' \ %(other, self.GridHome, self.GridHome, self.SWLocation, other,other)) CheckStatus(status=status, stage='Succesfully finish command on %s' % other)
def loop_over_refs(refs): print("Checking %r" % refs) response = input("Is that okay? ") if response != 'y': sys.exit(0) for ref in refs: time.sleep(3) ref = ref[:reflength] commands.getstatus('git checkout %s' % ref) print("Running on", ref) results[ref] = do_scan() return results
def loop_over_refs(refs): print "Checking %r" % refs response = raw_input("Is that okay? ") if response != 'y': sys.exit(0) for ref in refs: time.sleep(3) ref = ref[:reflength] commands.getstatus('git checkout %s' % ref) print "Running on", ref results[ref] = do_scan() return results
def LVRTSLibary(self): RacStop(GridHome=self.GridHome) status=getstatus('su -c "cp -f %s/lib/libskgxn2.so %s/lib/libskgxn2.so.oracle.bck" oracle' \ %(self.OracleHome, self.OracleHome)) CheckStatus(status=status, stage='Succesfully: cp -rf %s/lib/libskgxn2.so %s/lib/libskgxn2.so.oracle.bck' \ %(self.OracleHome, self.OracleHome)) status=getstatus('su -c "cp -f %s/lib/libodm11.so %s/lib/libodm11.so.oracle.bck" oracle' \ %(self.OracleHome, self.OracleHome)) CheckStatus(status=status, stage='Succesfully: cp -rf %s/lib/libodm11.so %s/lib/libodm11.so.oracle.bck' \ %(self.OracleHome, self.OracleHome)) status=getstatus('su -c "rm -rf %s/lib/libodm11.so" oracle' %(self.OracleHome)) status=getstatus('su -c "rm -rf %s/lib/libskgxn2.so" oracle' %(self.OracleHome)) CheckStatus(status=status, stage='Succesfully: rm -rf %s/lib/libodm11.so && %s/lib/libskgxn2.so' \ %(self.OracleHome, self.OracleHome)) status=getstatus('su -c "ln -s /usr/lib64/libodm.so %s/lib/libodm11.so" oracle' %(self.OracleHome)) CheckStatus(status=status, stage='Succesfully: ln -s /usr/lib64/libodm.so %s/lib/libodm11.so' % self.OracleHome) status=getstatus('su -c "cp -f %s/lib/libskgxn2.so %s/lib/libskgxn2.so.oracle.bck" oracle' %(self.GridHome, self.GridHome)) CheckStatus(status=status, stage='Succesfully: cp -rf %s/lib/libskgxn2.so %s/lib/libskgxn2.so.oracle.bck '\ %(self.GridHome, self.GridHome)) status=getstatus('su -c "cp -f /usr/lib64/libvcsmm.so %s/lib/libskgxn2.so" oracle' %(self.GridHome)) CheckStatus(status=status, stage='Succesfully: cp -rf /usr/lib64/libvcsmm.so %s/lib/libskgxn2.so' %(self.GridHome)) status=getstatus('su -c "ln -s %s/lib/libskgxn2.so %s/lib/libskgxn2.so" oracle' %(self.GridHome, self.OracleHome)) CheckStatus(status=status, stage='Succesfully: ln -s %s/lib/libskgxn2.so %s/lib/libskgxn2.so' \ %(self.GridHome, self.OracleHome)) RacStart(GridHome=self.GridHome, Stage='StartOnly')
def RVRTSLibary(self): for other in self.OtherHosts: RacStop(GridHome=self.GridHome, ssh='ssh', server=other ) status=getstatus('ssh %s \'su oracle -c "cp -f %s/lib/libskgxn2.so %s/lib/libskgxn2.so.oracle.bck" \'' \ %(other, self.OracleHome, self.OracleHome)) CheckStatus(status=status, stage='Succesfully: cp -rf %s/lib/libskgxn2.so %s/lib/libskgxn2.so.oracle.bck on %s' \ %(self.OracleHome, self.OracleHome, other)) status=getstatus('ssh %s \'su oracle -c "cp -f %s/lib/libodm11.so %s/lib/libodm11.so.oracle.bck"\'' \ %(other, self.OracleHome, self.OracleHome)) CheckStatus(status=status, stage='Succesfully: cp -rf %s/lib/libodm11.so %s/lib/libodm11.so.oracle.bck on %s' \ %(self.OracleHome, self.OracleHome, other)) status=getstatus('ssh %s \'su oracle -c "rm -rf %s/lib/libodm11.so"\'' %(other, self.OracleHome)) status=getstatus('ssh %s \'su oracle -c "rm -rf %s/lib/libskgxn2.so"\'' %(other, self.OracleHome)) CheckStatus(status=status, stage='Succesfully: rm -rf %s/lib/libodm11.so && %s/lib/libskgxn2.so on %s' \ %(self.OracleHome, self.OracleHome, other)) status=getstatus('ssh %s \'su oracle -c "ln -s /usr/lib64/libodm.so %s/lib/libodm11.so" \'' %(other, self.OracleHome)) CheckStatus(status=status, stage='Succesfully: ln -s /usr/lib64/libodm.so %s/lib/libodm11.so on %s ' \ % (self.OracleHome, other)) status=getstatus('ssh %s \'su oracle -c "cp -f %s/lib/libskgxn2.so %s/lib/libskgxn2.so.oracle.bck" \'' \ %(other, self.GridHome, self.GridHome)) CheckStatus(status=status, stage='Succesfully: cp -rf %s/lib/libskgxn2.so %s/lib/libskgxn2.so.oracle.bck on %s'\ %(self.GridHome, self.GridHome, other)) status=getstatus('ssh %s \'su oracle -c "cp -f /usr/lib64/libvcsmm.so %s/lib/libskgxn2.so" \'' %(other, self.GridHome)) CheckStatus(status=status, stage='Succesfully: cp -rf /usr/lib64/libvcsmm.so %s/lib/libskgxn2.so on %s' \ %(self.GridHome, other)) status=getstatus('ssh %s \'su oracle -c "ln -s %s/lib/libskgxn2.so %s/lib/libskgxn2.so" \'' \ %(other, self.GridHome, self.OracleHome)) CheckStatus(status=status, stage='Succesfully: ln -s %s/lib/libskgxn2.so %s/lib/libskgxn2.so on %s' \ %(self.GridHome, self.OracleHome, other)) RacStart(GridHome=self.GridHome, ssh='ssh', server=other, Stage='StartOnly')
def Fvx_mount(self, lvm): if self.config['Share_FS'].upper() == 'YES': share = '-t vxfs -o cluster' else: share = '-t vxfs' for k,hash in lvm.iteritems(): self.logger.error('Running: /opt/VRTS/bin/mount %s /dev/vx/dsk/%s/%s %s' %(share, hash.values()[0].split(',')[1].strip(), hash.keys()[0].strip(),\ hash.values()[0].split(',')[0].strip())) if getstatus('/opt/VRTS/bin/mount %s /dev/vx/dsk/%s/%s %s %s' %(share, hash.values()[0].split(',')[1].strip(), hash.keys()[0].strip(),\ hash.values()[0].split(',')[0].strip(), self.log))[0] != 0: if getstatus('/opt/VRTS/bin/df %s' %( hash.values()[0].split(',')[0].strip()))[0] != 0: self.logger.error('Faild to run: \'/opt/VRTS/bin/mount %s /dev/vx/dsk/%s/%s %s\'' %(share, hash.values()[0].split(',')[1].strip(),\ hash.keys()[0].strip(), hash.values()[0].split(',')[0].strip())) raise Exception, '%113%'
def Fvx_mkfs(self, lvm): for k,hash in lvm.iteritems(): if not re.search('NONE_[0-9]+' ,hash.keys()[0]) : self.logger.error('Running: /opt/VRTS/bin/mkfs -t vxfs /dev/vx/rdsk/%s/%s' %(hash.values()[0].split(',')[1].strip(), hash.keys()[0].strip())) if getstatus('/opt/VRTS/bin/mkfs -t vxfs /dev/vx/rdsk/%s/%s %s' %(hash.values()[0].split(',')[1].strip(), hash.keys()[0].strip(), self.log))[0] != 0: self.logger.error('Faild to run: \'/opt/VRTS/bin/mkfs -t vxfs /dev/vx/rdsk/%s/%s\'' %(hash.values()[0].split(',')[1].strip(), hash.keys()[0].strip())) raise Exception, '%113%'
def addstatmenu(w, files): w.statmenu = m = w.menucreate('Stat') m.files = files m.action = [] for file in files: m.additem(commands.getstatus(file)) m.action.append(stataction)
def run(self): try: if self.sys_command =='ping': status,self.Runresult=commands.getstatusoutput('ping '+self.hosts+' -c 3') elif self.sys_command =='top': str1='top -n 1 |grep Cpu |sed \'s/.*(s) ://g \'|cut -d , -f 4|cut -d % -f 1|cut -d \' \' -f 2' status,self.Runresult=commands.getstatusoutput(str1) elif self.sys_command =='mem': #str2 = 'grep MemTotal /proc/meminfo' #status,self.Runresult=commands.getstatusoutput(str2) s1=commands.getoutput('grep MemFree /proc/meminfo') status=commands.getstatus('grep MemFree /proc/meminfo') s2=commands.getoutput('grep MemTotal /proc/meminfo') s2=s2.split('MemTotal:')[1].split(' kB') s1=s1.split('MemFree:')[1].split(' kB') s = float(s1[0])/float(s2[0]) self.Runresult=str(s) #self.Runresult=s2[0]+","+s1[0] #self.Runresult=s2.strip('metoalkb') elif self.sys_command =='network': #str1='top -n 1 |grep Cpu |sed \'s/.*(s) ://g \'|cut -d , -f 4|cut -d % -f 1|cut -d \' \' -f 2' #str1='cat /proc/stat' str1='ifconfig eth0 |grep \'接收字节\'' status,self.Runresult=commands.getstatusoutput(str1) else: status,self.Runresult=commands.getstatusoutput(self.sys_command) except Exception,e: print str(e) return str(e)
def LocalDBscripts(self): self.logger.debug('Runnig %s/root.sh on %s' %(self.OracleHome, os.environ['HOSTNAME'])) status=getstatus('%s/root.sh %s' %(self.OracleHome, self.log)) Log=getstatus("ls -tr %s/install/root_%s*.log " %(self.OracleHome, os.environ['HOSTNAME'])) if Log[0] is 0: Log=getstatus("ls -tr %s/install/root_%s*.log |tail -1 " %(self.OracleHome, os.environ['HOSTNAME'])) Counter,Flag = CheckLog(Log=Log[1], Line='"Finished product-specific root actions."') if Flag != 0: if Flag == 1: self.logger.error("Successfully: Finish RAC DB scripts on %s server" % os.environ['HOSTNAME']) elif Flag == 2: self.logger.error("Failed: RAC DB scripts on %s server" % os.environ['HOSTNAME']) raise Exception, 'Failed: RAC DB scripts' else: self.logger.error("Failed: RAC DB scripts on %s server" % os.environ['HOSTNAME']) raise Exception, 'Failed: RAC DB scripts'
def run(self): result = self.grid.run() if result == self.cancel_btn: logging.info("Cancel") return 0 else: logging.info("Accept") for idx, val in enumerate(self.all_nics): if idx == self.list_svc_nic.current(): mgmt_nic = val svc_nic = val retcode, output = commands.getstatusoutput( "cd /tmp && /usr/bin/abiquo-dhcp-relay -r %s -s %s -v %s-%s -x %s -n %s && mv /tmp/relay-config /etc/init.d/relay-config" % (mgmt_nic, svc_nic, self.entry_vlan_from.value(), self.entry_vlan_to.value(), self.entry_dhcp_server.value(), self.entry_svc_net.value())) if retcode == 0: logging.info("Successfully reconfigured DHCP relay.") logging.info("Restarting service.") retcode = commands.getstatus("/etc/init.d/relay-config restart") if retcode == 0: logging.info("Done.") else: logging.error("Something went wrong restarting service. Check system logs.") else: logging.error("Error configuring DHCP relay.") logging.error(output) return 1 return 0
def cmd_filestatus(file): """ return status :param file: :return: """ return commands.getstatus(file)
def getLatestDockerTemplateFiles(): # Check out the docker template repo files from Git if not "DKTEMP_HOME" in os.environ: print('The DKTEMP_HOME environment variable is not set!') sys.exit() temp_home = os.environ.get("DKTEMP_HOME") if not "DKTEMP_GIT_REPO" in os.environ: print('DKTEMP_GIT_REPO environment variable is not set!') sys.exit() git_repo = os.environ.get("DKTEMP_GIT_REPO") # Make sure the docker template home directory has the lastest teamplate files. # First make sure the directory exists and if not create it if not os.path.exists(temp_home): os.makedirs(temp_home) os.chdir(temp_home) print(commands.getstatus('/usr/bin/git clone ' + git_repo)) repo = git.Repo.init(temp_home) if not repo.remotes: origin = repo.create_remote('origin', git_repo) origin = repo.remotes.origin origin.fetch() origin.pull(origin.refs[0].remote_head) return
def main(appname): print "Making tarball for webapp '%s'..." % appname, if appname == "frog": versionfile = "frog/frog/__init__.py" distname = "Frog-" elif appname == "filemgr": versionfile = "filemgr/filemgr/__init__.py" distname = "FileMgr-" else: raise ValueError("I don't know webapp '%s'" % appname) for line in open(versionfile): m = re.match('^VERSION.+=.*"(.+)"', line) if m: distname += m.group(1) break print distname if not os.path.isdir("dist"): os.mkdir("dist") distname = "dist/%s.tar.gz" % distname status, output = commands.getstatusoutput("tar --exclude=CVS -czf %s %s" % (distname, appname)) if status != 0: print "ERROR" print output else: print "Done!", commands.getstatus(distname)
def addstatmenu(w, files): w.statmenu = m = w.menucreate("Stat") m.files = files m.action = [] for file in files: m.additem(commands.getstatus(file)) m.action.append(stataction)
def run_blogger(retry, single=(False, [])): #Clear error log if not single[0]: with open(lab_name+"_exe_error.log", 'w'): pass cmds = [ "javac -Xlint -cp junit.jar:guava-13.0.1.jar:gson-2.2.2.jar:url/:. ./url/*.java" ] commands.getstatus(cmds[0]) for line in open("urls.txt",'r'): words = line.split(' ') score_dict[words[0]] = 0 cmd = "java -Durl="+words[1].strip()+" -cp junit.jar:guava-13.0.1.jar:gson-2.2.2.jar:url/:. org.junit.runner.JUnitCore " + test_name print "running " + words[0] exeResult = commands.getstatusoutput(cmd) score_dict[words[0]] = grep_score2(exeResult[1]) print exeResult[1] if not single[0] and not retry: output_score()
def AssignIpAdress(self,interface,ip,mask): self.logger.debug("ifconfig %s %s netmask %s %s" %(interface,ip,mask,self.log)) status=getstatus("ifconfig %s %s netmask %s %s" %(interface,ip,mask,self.log)) if status[0] is 0: self.logger.error("[I] bring online %s with IP %s and mask %s" %(interface,ip,mask)) else: self.logger.error("[E] Can't bring online %s with IP %s and mask %s" %(interface,ip,mask)) raise Exception, 'Error: %s' % status[1]
def icmp(self, ip): import commands cmd = 'ping %s -c 1 ' % ip status = commands.getstatus(cmd) if status == 0: return "alive" else: return "dead"
def ls_advanced(a=0, A=0, p=0): if (a == 1): if p == 1: print bcolors.ENDC + (commands.getstatus('.')).rsplit( ' ', 1)[0] + bcolors.OKBLUE + " ." + "/ " print bcolors.ENDC + (commands.getstatus('..')).rsplit( ' ', 1)[0] + bcolors.OKBLUE + " .." + "/ " else: print bcolors.ENDC + (commands.getstatus('.')).rsplit( ' ', 1)[0] + bcolors.OKBLUE + " ." + " " print bcolors.ENDC + (commands.getstatus('..')).rsplit( ' ', 1)[0] + bcolors.OKBLUE + " .." + " " all_files = os.listdir(os.curdir) for x in sorted(all_files, key=str.lower): if x[0] != '.' or a == 1 or A == 1: if os.path.isfile(x) and x[0] != '.': print bcolors.ENDC + (commands.getstatus(x)).rsplit( ' ', 1)[0] + " " + x + " " elif p == 1 and not (os.path.isfile(x)): print bcolors.ENDC + (commands.getstatus(x)).rsplit( ' ', 1)[0] + bcolors.OKBLUE + " " + x + "/ " else: print bcolors.ENDC + (commands.getstatus(x)).rsplit( ' ', 1)[0] + bcolors.OKBLUE + " " + x + " " print bcolors.ENDC
def post_actions(base_dir, data_path, latest_file): index_file = data_path + INDEX_POSTFIX if (os.path.islink(index_file)): os.remove(index_file) os.symlink(latest_file, index_file) cmd = "bash " + base_dir + "/bin/clean.sh" status = commands.getstatus(cmd) if status != 0: return False return True
def detec_apache_install_way(binPath): """ 检测apache的安装方式 """ commands.getoutput(binPath + """ -V | grep "HTTPD_ROOT" | awk -F "=" '{print $2}' | awk -F '"' '{print $2}' > .tmpfile""") isRPM = commands.getoutput("""grep "/etc/httpd" .tmpfile""") if isRPM.strip() == '': isRPM = False else: isRPM = True isMake = commands.getoutput(""" grep -v '/etc/httpd' .tmpfile""") if isMake.strip() == '': isMake = False else: isMake = True commands.getstatus('rm -f .tmpfile') return [{'RPM':isRPM}, {'Make':isMake}]
def OfflineBackup(): DB2_DIR=os.getenv("DB2_DIR") #Get DB2 BACKUP Directory if DB2_DIR==None: print "Fail To Get DB2_DIR" return False else: if os.path.isdir(DB2_DIR): pass else: print "Backup Directiory Does Not Exist" return False DBNAME=os.getenv("DBNAME") #Get DBNAME what to do if more than one db? if DBNAME==None: print "Fail To Get DBNAME" return False Result=commands.getstatusoutput('db2 force applications all') #Stop Applications if Result[0]==1024: print "No start database manager command was issued" return False elif Result[0]==0: Result=commands.getstatusoutput('db2 list applications') if Result[0]==0: commands.getstatus('db2 force applications all') else: print 'Applications Stopped' cmd='db2 backup database '+DBNAME+' to '+DB2_DIR try: Result=commands.getstatusoutput(cmd) if Result[0]==0: return True else: print 'Backup Fail' except Exception as e: print e sys.exit()
def ls(command): detailedList, toPrint = [], [] cmdLen = len(command) if cmdLen == 1: toPrint = getElementsForLS('.') for element in toPrint: if os.path.isdir(element): pcolor(str(element), blue) elif 'x' in commands.getstatus(element)[0:10]: pcolor(str(element), green) else: pcolor(str(element), normal) print elif cmdLen == 2: if command[1] == "-l": elements = getElementsForLS('.') for element in elements: detailedList.append(commands.getstatus(element)) for element in detailedList: if element.startswith('d'): pcolor(str(element), blue) elif 'x' in element[0:10]: pcolor(str(element), green) else: pcolor(str(element), normal) print else: try: path = command[1] if not path.endswith('/'): path += '/' toPrint = getElementsForLS(path) for element in toPrint: if commands.getstatus(path + element).startswith('d'): pcolor(str(element), blue) elif 'x' in commands.getstatus(path + element)[0:10]: pcolor(str(element), green) else: pcolor(str(element), normal) print except: pcolor("wrong directory", red) print elif cmdLen == 3: try: if command[1] == '-l': path = command[2] else: path = command[1] if not path.endswith('/'): path += '/' if os.path.isdir(path): elements = getElementsForLS(path) for element in elements: detailedList.append(commands.getstatus(path + element)) for element in detailedList: if element.startswith('d'): pcolor(str(element), blue) elif 'x' in element[0:10]: pcolor(str(element), green) else: pcolor(str(element), normal) print elif os.path.isfile(path): output = commands.getstatus(path) if output.startswith('d'): pcolor(str(element), blue) elif 'x' in output[0:10]: pcolor(str(element), green) else: pcolor(str(element), normal) except: pcolor("wrong syntax", red) print
import commands ''' 1.commands.getstatusoutput('cmd') 此函数是返回命令执行返回值以及执行结果 ''' result1 = commands.getstatusoutput('ls /Users/hushiwei/Downloads/learnsql') print "result1 `s stutas : ", result1[0] print "result1 `s output : ", result1[1] ''' 2.commands.getoutput('cmd') 此函数只返回结果,不返回返回值 ''' result2 = commands.getoutput('ls /Users/hushiwei/Downloads/learnsql') print result2 ''' 3.commands.getstatus('file') 此函数返回ls -ld file 的执行结果 ''' result3 = commands.getstatus('ls /Users/hushiwei/Downloads/learnsql') print result3 sta, content = commands.getstatusoutput('java -version') print str(sta) + " --- " + content[0:14]
files = (paths[1] + '/SudokuClient.java', paths[1] + '/SudokuServer.java', paths[1] + '/SudokuSolver.java') if not (tup[2] in SDB.all_EID()): drop.append(tup[2]) for path in paths: if not os.path.exists(path): print tup[2] + " has no path " + path #print "instead, it has: " wrong = True break if not wrong: for file in files: if not os.path.exists(file): stat = commands.getstatus("svn rename " + paths[1] + "/SudokuTest.java " + paths[1] + "/TestSudoku.java") if stat[0] == 0: commands.getstatus("svn ci " + paths[1] + " -m 'fixed test file naming'") print tup[2] + " has no file " + os.path.basename(file) wrong = True if wrong: idiot_list.append(tup[2]) if drop: print "Dropped:" print drop print "Idiots of this week are: " print idiot_list pickle.dump(idiot_list, open("Lab3_failed_update.pickle", "wb")) print "Email them: "
# -*- coding: utf-8 -*- import commands import popen2 print(commands.getstatusoutput("/bin/echo / | xargs ls")) print(commands.getoutput("/bin/echo / | xargs ls")) # This one is safe. print(commands.getstatus("/bin/echo / | xargs ls")) print(popen2.popen2("/bin/echo / | xargs ls")[0].read()) print(popen2.popen3("/bin/echo / | xargs ls")[0].read()) print(popen2.popen4("/bin/echo / | xargs ls")[0].read()) print(popen2.Popen3("/bin/echo / | xargs ls").fromchild.read()) print(popen2.Popen4("/bin/echo / | xargs ls").fromchild.read())
print 'Source :' show_file_info('6.5-shutil.copy.py') shutil.copy2('6.5-shutil.copy.py', 'data') print 'Destination :' show_file_info('data/6.5-shutil.copy.py') print ## 6.5.2 Copying File Metadata # importing commands... wooooooooooo # to copy file permissions from one file to another, use copymode() with open('data/6.5-shutil.copy.py', 'wt') as f: f.write('These are the new contents of this file.') os.chmod('data/6.5-shutil.copy.py', 0444) print 'BEFORE:' print commands.getstatus('data/6.5-shutil.copy.py') shutil.copymode('6.5-shutil.copy.py', 'data/6.5-shutil.copy.py') print 'AFTER :' print commands.getstatus('data/6.5-shutil.copy.py') print # copying other metadata can be done with copystat() with open('data/6.5-shutil.copy.py', 'wt') as f: f.write('These are the new contents of this file.') os.chmod('data/6.5-shutil.copy.py', 0444) print 'BEFORE:' show_file_info('data/6.5-shutil.copy.py') shutil.copystat('6.5-shutil.copy.py', 'data/6.5-shutil.copy.py') print 'AFTER :' show_file_info('data/6.5-shutil.copy.py') print
def dpvs_ctrl(bind_num, FILE_CONTENT, DPVS_VERSION, gitpro=False, spec=""): '''build DPVS with source''' os.chdir(r'/root/dpvs') cmd0 = "" if os.system("git --version") != 0: os.system("yum install git -y") '''checkout to branch to test, if gitpro=True''' if gitpro: #checkout to DPVS_VERSION branch to compile DPVS checkout_cmd = "git checkout " + DPVS_VERSION s, r = commands.getstatusoutput(checkout_cmd) if s != 0: return { "status": False, "content": FILE_CONTENT, "detail": "checkout to DPVS_VERSION branch failed" } #environmental variable RET_SDK set if not os.environ["RTE_SDK"]: os.environ["RTE_SDK"] = "/root/dpdk-dependencies" elif os.environ["RTE_SDK"] != "/root/dpdk-dependencies": os.system( "echo \"export RTE_SDK=/root/dpdk-dependencies\" >> ~/.bashrc") commands.getstatus("source /root/.bashrc") f = open("/root/.bashrc", "r") count = str(len(f.readlines()) - 1) cmd0 = "sed '%s, $d' -i /root/.bashrc \nsource /root/.bashrc \n" % count FILE_CONTENT = cmd0 + FILE_CONTENT #delete last environ line #compile DPVS compile_cmd = "make -j32" if os.system(compile_cmd) != 0: return { "status": False, "content": FILE_CONTENT, "detail": "DPVS compile FAILED" } #install DPVS make_cmd = "make install" stats, _ = commands.getstatusoutput(make_cmd) if stats != 0: return { "status": False, "content": FILE_CONTENT, "detail": "DPVS install FAILED" } #backup /etc/dpvs.conf if os.path.exists("/etc/dpvs.conf"): backup_cmd = "mv /etc/dpvs.conf /etc/dpvs.conf-bak" cmd0 = "mv /etc/dpvs.conf-bak /etc/dpvs.conf \n" stats, _ = commands.getstatusoutput(backup_cmd) if stats != 0: print "DPVS conf backup FAILED" #add logging else: FILE_CONTENT = cmd0 + FILE_CONTENT #copy conf to /etc/dpvs.conf if bind_num == 1: cp_cmd = "cp conf/dpvs.conf.single-nic.sample /etc/dpvs.conf" if bind_num == 2: cp_cmd = "cp conf/dpvs.conf.sample /etc/dpvs.conf" stats = os.system(cp_cmd) #copy conf file to /etc/dpvs.conf if stats != 0: return { "status": False, "content": FILE_CONTENT, "detail": "DPVS conf update FAILED" } #NAT64 IPV6-6 OR IPV4 only if spec == "ip6": '''change mode from default perfect to signature''' s, _ = commands.getstatusoutput( "sudo sed -i \"s/perfect/signature/g\" /etc/dpvs.conf") if s != 0: return { "status": False, "content": FILE_CONTENT, "detail": "DPVS ip6-6 conf update FAILED" } #run DPVS run_cmd = "./bin/dpvs &" if os.system(run_cmd) != 0: return { "status": False, "content": FILE_CONTENT, "detail": "DPVS start FAILED" } time.sleep(13) #wait for thread up cmd0 = "ps -ef | grep dpvs | awk '{print $2}'| xargs kill -9 \n" FILE_CONTENT = cmd0 + FILE_CONTENT #test again run_cmd = "./bin/dpip link show" stats, res = commands.getstatusoutput(run_cmd) if bind_num == 1 and res.find("dpdk0") == -1: return { "status": False, "content": FILE_CONTENT, "detail": "DPVS run FAILED" } if bind_num == 2 and res.find("dpdk1") == -1: return { "status": False, "content": FILE_CONTENT, "detail": "DPVS run FAILED" } return { "status": True, "content": FILE_CONTENT, "detail": "DPVS environment settled" }
#!/usr/bin/env python import commands numToSep = 30 files = commands.getoutput('ls somewhere').split() N = len(files) / numToSep fTmpLists = [''] * (N + 1) i = 0 j = 0 for file in files: fTmpLists[j] = '{} {}'.format(fTmpLists[j], file) i += 1 if i == N: i = 0 j += 1 fTmps = '' for idx in range(N + 1): commands.getstatus('hadd /tmp/ltsai/tmpFile_{:02}.root {}'.format( idx, fTmpLists[idx])) fTmps += ' /tmp/ltsai/tmpFile_{:02}.root'.format(idx) commands.getstatus('hadd total.root {}'.format(fTmps))
def dpdk_env_setup(FILE_CONTENT, ifaces): '''test the dpdk env and record old one''' bind_stats_cmd = "dpdk-devbind --status" cmd_status, cmd_result = commands.getstatusoutput(bind_stats_cmd) if cmd_status != 0: dpdk_deployed = False else: dpdk_deployed = True rec = record_old_dpdk_env(FILE_CONTENT) FILE_CONTENT = rec["content"] '''testify system: NUMA struct or not''' sys_struct = "grep -i numa /var/log/dmesg" _, res = commands.getstatusoutput(sys_struct) if res.find("No NUMA configuration found") != -1: return { "status": False, "content": FILE_CONTENT, "detail": "single-node system, skip..." } '''DPDK hugepages set''' numa_nodes = "grep -i numa /var/log/dmesg | grep node" _, res_numa = commands.getstatusoutput(numa_nodes) total = len(res_numa.split("\n")) cmd_str = "ls /sys/devices/system/node/node0/hugepages/ | grep hugepages-" _, res = commands.getstatusoutput(cmd_str) for i in range(total): huge_cmd = "echo 8192 > /sys/devices/system/node/node" + str( i) + "/hugepages/" + res + "/nr_hugepages" stats, _ = commands.getstatusoutput(huge_cmd) if stats != 0: return { "status": False, "content": FILE_CONTENT, "detail": "failed set huge pages" } '''DPDK dependencies installation''' cmd = "yum install -y automake libnl3 libnl-genl-3.0 openssl popt popt-devel numactl gcc make meson ninja-build" os.system(cmd) #yum install failed but could start dpvs, just in case '''DPDK drivers load -- files:/root/dpdk-dependencies/kmod''' uio_cmd = "modprobe uio" s, res = commands.getstatusoutput(uio_cmd) if s == 0: cmd0 = "rmmod uio \n" FILE_CONTENT = cmd0 + FILE_CONTENT elif res.find("File exists") != -1: pass else: return { "status": False, "content": FILE_CONTENT, "detail": "DPDK drivers loading failed" } uio_cmd = "insmod /root/dpdk-dependencies/kmod/igb_uio.ko" s, res = commands.getstatusoutput(uio_cmd) if s == 0: cmd0 = "rmmod /root/dpdk-dependencies/kmod/igb_uio.ko \n" FILE_CONTENT = cmd0 + FILE_CONTENT elif res.find("File exists") != -1: pass else: return { "status": False, "content": FILE_CONTENT, "detail": "DPDK drivers loading failed" } uio_cmd = "insmod /root/dpdk-dependencies/kmod/rte_kni.ko" s, res = commands.getstatusoutput(uio_cmd) if s == 0: cmd0 = "rmmod /root/dpdk-dependencies/kmod/rte_kni.ko \n" FILE_CONTENT = cmd0 + FILE_CONTENT elif res.find("File exists") != -1: pass else: return { "status": False, "content": FILE_CONTENT, "detail": "DPDK drivers loading failed" } '''bind NIC with dpdk-dependencies/usertools/dpdk-devbind.py''' stats_cmd = "/root/dpdk-dependencies/usertools/dpdk-devbind.py --status | grep " bind_cmd = "/root/dpdk-dependencies/usertools/dpdk-devbind.py -b igb_uio " unbind_cmd = "/root/dpdk-dependencies/usertools/dpdk-devbind.py -b ixgbe " #bind kernel othbind_cmd = "/root/dpdk-dependencies/usertools/dpdk-devbind.py -b " #bind other known network bind_num = 0 #bind kernel network first to have "if" key stats, res = commands.getstatusoutput(stats_cmd + "igb_uio") niclist = res.split("\n") for nic in niclist: if nic.find("drv=") == -1: #other networks tmp_bind = unbind_cmd + nic.split()[0] #bind kernel stats, _ = commands.getstatusoutput(tmp_bind) if stats != 0: return { "status": False, "content": FILE_CONTENT, "bind_num": 0, "detail": "DPDK devbind failed" } elif nic.find("drv=ixgbe") == -1 or nic.find( "drv=igb_uio") == -1: #networks not other or kernel for elem in nic.split(): if elem.find("drv=") != -1: tmp_bind = unbind_cmd + nic.split()[0] #bind kernel stats, _ = commands.getstatusoutput(tmp_bind) if stats != 0: return { "status": False, "content": FILE_CONTENT, "bind_num": 0, "detail": "DPDK devbind failed" } cmd0 = othbind_cmd + elem.split( "=")[1] + " " + nic.split()[0] + "\n" FILE_CONTENT = cmd0 + FILE_CONTENT elif nic.find("drv=igb_uio") != -1: #bind DPDK already bind_num = bind_num + 1 #iface can bind:down or get from sys_argv / intern NIC usable_face = [] if ifaces == "": #no given ifaces--need tcpdump to determine faces = netifaces.interfaces() commands.getstatus("yum install -y tcpdump") for face in faces: if face == 'lo' or face.find("eth") == -1: continue up_cmd = "ifup " + face s, _ = commands.getstatusoutput(up_cmd) if s != 0: usable_face.append(face) continue dump_cmd = "tcpdump -c 3 -i " + face _, res = commands.getstatusoutput(dump_cmd) if res.find("IP 172.") != -1: continue usable_face.append(face) else: for face in ifaces.split(","): usable_face.append(face) #trasverse interfaces:not Active or external network one can bind for face in usable_face: stats, res = commands.getstatusoutput(stats_cmd + face) if res.find("Active") != -1 or stats != 0: continue down_cmd = "ifconfig " + face + " down" commands.getstatus(down_cmd) stats, _ = commands.getstatusoutput(bind_cmd + res.split()[0]) if stats == 0: bind_num += 1 cmd0 = unbind_cmd + res.split()[0] + "\n" FILE_CONTENT = cmd0 + FILE_CONTENT if bind_num == 2: break return { "status": True, "content": FILE_CONTENT, "bind_num": bind_num, "detail": "DPDK environment settled" }
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @desc: commands 模块.Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果 @author: ronny @contact: [email protected] @site: www.lemon.pub @software: PyCharm @since:python 3.5.2(32bit) on 2016/11/11.18:41 """ import commands # 用Python写运维脚本时,经常需要执行linux shell的命令,这就需要用到commands模块 # 下面是commands模块的3个主要函数: # 1. commands.getoutput('shell command') # 执行shell命令,返回结果(string类型) commands.getoutput('pwd') #echo :'/home/ronny' # 2. commands.getstatus('file') # 该函数已被python丢弃,不建议使用,目前可以用,它返回 ls -ld file 的结果(String)(返回结果太奇怪了,难怪被丢弃) commands.getstatus('addon.zip') # echo : '-rw-rw-r-- 1 ronny ronny 873751 10\xe6\x9c\x88 19 11:09 addon.zip' # 3. commands.getstatusoutput('shell command') # 执行shell命令, 返回两个元素的元组tuple(status, result),status为int类型,result为string类型。# # cmd的执行方式是{ cmd ; } 2>&1, 故返回结果包含标准输出和标准错误. commands.getstatusoutput('pwd') #echo :(0, '/home/ronny')
def print_table(table): width = max([len(key) for key in items.keys()]) headers = [' ' * width] + [ref.ljust(reflength) for ref in table.keys()] rows = [headers] + [["-" * width] + ["-" * reflength] * len(table)] for item in items: rows.append([item.ljust(width)] + [("%0.2fs" % table[ref][item]).ljust(reflength) for ref in table]) for row in rows: print("|" + "|".join(row) + "|") if __name__ == '__main__': results = collections.OrderedDict() head = commands.getoutput('git rev-parse --abbrev-ref HEAD') reflength = max([reflength, len(head)]) refs = sys.argv[1:] if not refs: refs = [head] try: results = loop_over_refs(refs) finally: print("Returning you to %s" % head) commands.getstatus('git checkout %s' % head) print_table(results)
def stataction(w, m, item): # Menu item action for stat menu file = m.files[item] try: m.setitem(item, commands.getstatus(file)) except os.error: stdwin.message('Can\'t get status for ' + file)
def infoFile(filename): status = commands.getstatus(filename) print status
import commands import popen2 print(commands.getstatusoutput('/bin/echo / | xargs ls')) print(commands.getoutput('/bin/echo / | xargs ls')) # This one is safe. print(commands.getstatus('/bin/echo / | xargs ls')) print(popen2.popen2('/bin/echo / | xargs ls')[0].read()) print(popen2.popen3('/bin/echo / | xargs ls')[0].read()) print(popen2.popen4('/bin/echo / | xargs ls')[0].read()) print(popen2.Popen3('/bin/echo / | xargs ls').fromchild.read()) print(popen2.Popen4('/bin/echo / | xargs ls').fromchild.read())
#!/usr/bin/python #coding:utf-8 import json import commands (status, output) = commands.getstatusoutput('date') print status print output print type(output) print "##################" print commands.getstatus('discover_mysql.py')