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
.
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
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.
Someone on IRC suggested I use command_string = "cat /proc/cpu/"
and
result = uptime("-p", "pass", "ssh", "theo@localhost", command_string)
Which works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.