Running a command on a remote server to find the uptime in seconds
<p>Here's what I have so far:</p>

<pre><code>#!/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 &gt; check_time:
            print("it rebooted", result_int, "minutes ago")

        time.sleep(5)
</code></pre>

<p>Results:</p>

<pre class="lang-none prettyprint-override"><code>checking uptime
Traceback (most recent call last):
  File "./uptime.py", line 23, in &lt;module&gt;
    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
</code></pre>

<p>You can see I have two attempts, one using the <code>sh</code> library and one with check_output, both result in the same error about not being able to run <code>/usr/bin/cat /proc/uptime</code>.</p>

<p>However as you can see at the end of the traceback:</p>

<pre><code>RAN: '/usr/bin/sshpass -p pass ssh theo@localhost "cat /proc/uptime"'
</code></pre>

<p>That appears to be a perfectly valid line, and if I copy paste it into a terminal it works.</p>

<p>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 <em>thought</em> it would be) :)</p>

<p>I'm using python 3.5.2</p>

<p>Someone on IRC suggested I use <code>command_string = "cat /proc/cpu/"</code> and</p>

<pre><code>result = uptime("-p", "pass", "ssh", "theo@localhost", command_string)
</code></pre>

<p>Which works!</p>

python linux python-3.x