以下是我到目前为止:
#!/usr/bin/python import sys import os import subprocess from subprocess import check_output import time import sh from sh import sshpass import re import time,datetime check_time = 0 with open("log.txt","a") as f: while 1: #out = check_output(["sshpass","-p","pass","ssh",# "theo@localhost",""/usr/bin/cat",# "/proc/uptime""]) #print (out) #out = str(out) uptime = sh.Command("/usr/bin/sshpass") result = uptime("-p","theo@localhost",""cat","/proc/uptime"") result = str(result) print (result) result = result.split(' ',1)[0] print (result) f.write(result) result_int = int(result) if result_int > check_time: print("it rebooted",result_int,"minutes ago") time.sleep(5)
结果:
checking uptime Traceback (most recent call last): File "./uptime.py",line 23,in <module> result = uptime("-p","/proc/uptime"") File "/usr/lib/python3.5/site-packages/sh.py",line 1021,in __call__ return RunningCommand(cmd,call_args,stdin,stdout,stderr) File "/usr/lib/python3.5/site-packages/sh.py",line 486,in __init__ self.wait() File "/usr/lib/python3.5/site-packages/sh.py",line 500,in wait self.handle_command_exit_code(exit_code) File "/usr/lib/python3.5/site-packages/sh.py",line 516,in handle_command_exit_code raise exc(self.ran,self.process.stdout,self.process.stderr) sh.ErrorReturnCode_127: RAN: '/usr/bin/sshpass -p pass ssh theo@localhost "cat /proc/uptime"' STDOUT: STDERR: zsh:1: no such file or directory: cat /proc/uptime
你可以看到我有两个尝试,一个使用sh库和一个使用check_output,都导致了不能运行/usr/bin/cat /proc/uptime 。
不过,正如您在回溯结束时所看到的那样:
Shell脚本中的浮点比较
parsing代理url时出错。 坏的端口号
把阻塞(没有O_NONBLOCK)文件parsing器在Linux中select/轮询是否合适?
将域名映射到Azure Linux VM App
RAN: '/usr/bin/sshpass -p pass ssh theo@localhost "cat /proc/uptime"'
这似乎是一个完全有效的路线,如果我复制粘贴到terminal它的工作。
有任何想法吗? 如果我只是把“正常运行时间”,而不是编辑输出,以获得在几秒钟内的时间,我的命令确实工作,我虽然这样做会更容易(至less我认为这将是): )
我正在使用python 3.5.2
在启动过程中自动挂载独立分区时systemd / udev依赖性失败
页面分配失败的顺序和模式
我只能在特定的Python版本下导入Stripe Python库
LabVIEW Linux C ++开发 – 初学者的问题
result = uptime("-p","cat","/proc/uptime")
它看起来像“cat / proc / uptime”在你的原代码中被ssh解释为一个参数。
有人在IRC上建议我使用command_string = "cat /proc/cpu/"和
result = uptime("-p",command_string)
哪个有效!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。