[英]Running a command on a remote server to find the uptime in seconds
Here's what I have so far: 这是我到目前为止的内容:
#!/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", "pass", "ssh", "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)
Results: 结果:
checking uptime
Traceback (most recent call last):
File "./uptime.py", line 23, in <module>
result = uptime("-p", "pass", "ssh", "theo@localhost", "\"cat", "/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
You can see I have two attempts, one using the sh
library and one with check_output, both result in the same error about not being able to run /usr/bin/cat /proc/uptime
. 您可以看到我有两次尝试,一次尝试使用
sh
库,另一次尝试使用check_output,都导致关于无法运行/usr/bin/cat /proc/uptime
的相同错误。
However as you can see at the end of the traceback: 但是,如您在回溯末尾所看到的:
RAN: '/usr/bin/sshpass -p pass ssh theo@localhost "cat /proc/uptime"'
That appears to be a perfectly valid line, and if I copy paste it into a terminal it works. 这似乎是一条非常有效的行,如果我将其粘贴到终端,则可以正常工作。
Any ideas? 有任何想法吗? The command does actually work if I just put in "uptime", but rather than editing the output of that to get the time in seconds, I though it would be easier to do it this way (at least I thought it would be) :)
如果我仅输入“正常运行时间”,该命令确实可以工作,但是比起以秒为单位的时间来编辑输出,尽管这样做会更容易(至少我认为是这样): )
I'm using python 3.5.2 我正在使用python 3.5.2
Based on the output, you need to modify your uptime line to look something like this: 根据输出,您需要修改正常运行时间行,如下所示:
result = uptime("-p", "pass", "ssh", "theo@localhost", "cat",
"/proc/uptime")
It looks like "cat /proc/uptime" is interpreted as one argument by ssh in your original code. 看起来“ cat / proc / uptime”在您的原始代码中被ssh解释为一个参数。
Someone on IRC suggested I use command_string = "cat /proc/cpu/"
and IRC上有人建议我使用
command_string = "cat /proc/cpu/"
并
result = uptime("-p", "pass", "ssh", "theo@localhost", command_string)
Which works! 哪个有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.