if os.system('lss') !=0: print 'Without the command'
if os.system('lss') !=0: print 'Without the command'
转载于:https://www.cnblogs.com/effortsing/p/10459749.html
importparamikoimportconfigparserimportreimportosimporttimeimportdatetime
config=configparser.ConfigParser()
config.read('conf.ini')defmain():print("输入命令: jobtype date -env 例如: A 2018-09-08 -d\n"
"1.jobtype:A,B\n"
"2.date:日期格式yyyy-mm-dd 如:2018-08-04\n"
"3.env:d(dev),t(test)\n\n")
code_date= ['A','B','-d','-t']whileTrue:
job_code= input('请输入执行定时任务命令:')
job_code=job_code.lower()
job_codes=job_code.split()
tmp= [val for val in job_codes if val incode_date]if len(tmp) != 2:print('任务类型或环境参数有误,请重新输入')else:#定时任务类型
if job_code.find('A') > -1:
job_type= '执行A任务命令'
elif job_code.find('B') > -1:
job_type= '执行B任务命令'
elif job_code.find('C') > -1:
job_type= '执行C任务命令'
#执行环境
if job_code.find('-d') > -1:
env_code= 'dev'
elif job_code.find('-t') > -1:
env_code= 'test'
#获取日期
date_string = re.findall(".* (.*) .*",job_code)
job_date= "".join(date_string)#获取环境配置
ip = config.get(env_code,"ip")
username= config.get(env_code,"username")
password= config.get(env_code,"password")#连接SSH
try:
ssh=paramiko.SSHClient()
key=paramiko.AutoAddPolicy()
ssh.set_missing_host_key_policy(key)
ssh.connect(ip,22, username, password ,timeout=5)#获取当前时间
#now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
#执行Job命令
job_command =job_type+job_date
stdin, stdout, stderr=ssh.exec_command(job_command)print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "开始执行")
ssh.close()except:print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "定时任务执行失败")#获取日志文件
t = paramiko.Transport((ip, 22))
t.connect(username=username, password=password)
sftp=paramiko.SFTPClient.from_transport(t)try:
result=0for i in range(1, 20):
time.sleep(60)
sftp.get('os.getcwd()+'/\\console.log')
log_content = open('console.log', 'r', encoding='UTF-8')
log_string=log_content.read()
log_content.close()if "任务开始时间" inlog_string:print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "执行中...")if "执行完毕时间" inlog_string:
time.sleep(5)print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "执行完毕")
result= 1
break
if result ==0:print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "执行失败")except:print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "获取日志失败,可能未执行成功")if __name__ == "__main__":
main()
展开全部
方法和详细的操复作步骤如下:制
1、第bai一步,将编写的bat批处理du文件拖到cmd命令zhi窗口中,以定期dao清除旧数据,见下图,转到下面的步骤。
2、第二步,执行完上面的操作之后,执行后返回的错误代码将返回一个错误,并且重复出现该错误提示该乱码字段不是内部或外部命令,见下图,转到下面的步骤。
3、第三步,执行完上面的操作之后,打开批处理文件。
批处理文件用于在1天前定期删除zip文件,见下图,转到下面的步骤。
4、第四步,执行完上面的操作之后,将语句分别粘贴到cmd命令行中并执行,见下图,转到下面的步骤。
5、第五步,执行完上面的操作之后,重新使用其他字符集来编写批处理文件。
打开编写的批处理bat文件,选择“文件”选项,然后在文件菜单中单击“另存为”选项,见下图,转到下面的步骤。
6、第六步,执行完上面的操作之后,将文件另存为ANSI编码格式的批处理文件,见下图,转到下面的步骤。
7、第七步,执行完上面的操作之后,直接重新运行bat批处理文件,或在cmd中成功运行,见下图。这样,就解决了这个问题了。
首先我们要知道如何调用系统命令:>>> os.system('ls')
anaconda-ks.cfg install.log.syslog 模板 图片 下载 桌面
install.log 公共的 视频 文档 音乐
0
>>>>>> os.system('lss')
sh: lss: command not found
32512
>>>
\\第一种,我们可以肉眼识别正确的会返回0,错误的则是非0\\第二种,使用if判断调用系统命令返回值是否为0,如为0则不输出,不为0则输出 "Without the command"-------------------------------错误------------------------------------->>> if os.system('lss') !=0:print 'Without the command'
...
sh: lss: command not found
Without the command
--------------------------------正确------------------------------------>>> if os.system('ls') !=0:print 'Without the command'
...
anaconda-ks.cfg install.log.syslog 模板 图片 下载 桌面
install.log 公共的 视频 文档 音乐
>>>