Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You probably want to use an SSH library like <a href="https://pypi.python.org/pypi/paramiko" rel="nofollow"><code>paramiko</code></a> (or <a href="https://pypi.python.org/pypi/spur" rel="nofollow"><code>spur</code></a>, <a href="https://pypi.python.org/pypi/Fabric" rel="nofollow">Fabric</a>, etc.… just google/PyPI/SO-search for "Python SSH" to see all the options and pick the one that best matches your use case). There are demos included with <code>paramiko</code> that do exactly what you want to do.</p> <p>If you insist on scripting the command-line <code>ssh</code> tool, you (a) almost certainly want to use <code>subprocess</code> instead of <code>os.system</code> (as the <code>os.system</code> docs explicitly say), and (b) will need to do some bash-scripting (assuming the remote side is running bash) to pass the value back to you (e.g., wrap it in a one-liner script that prints the exit status on <code>stderr</code>).</p> <p>If you just want to know why your existing code doesn't work, let's take a look at it:</p> <pre><code>os.system("ssh -qt hostname 'sudo yum list updates --security &gt; /tmp/yum_update_packagelist.txt';echo $?") </code></pre> <p>First, you're running an <code>ssh</code> command, then a separate <code>echo $?</code> command, which will echo the exit status of <code>ssh</code>. If you wanted to echo the status of the <code>sudo</code>, you need to get the semicolon into the <code>ssh</code> command. (And if you wanted the status of the <code>yum</code>, inside the <code>sudo</code>.)</p> <p>Second, <a href="http://docs.python.org/2/library/os.html#os.system" rel="nofollow"><code>os.system</code></a> doesn't look at what gets printed to <code>stdout</code> anyway. As the docs clearly say, "the return value is the exit status of the process". So, you're getting back the exit status of the <code>echo</code> command, which is pretty much guaranteed to be 0.'</p> <p>So, to make this work, you'd need to get the echo into the right place, and then read the stdout by using <code>subprocess.check_output</code> or similar instead of <code>os.system</code>, and then parse that output to read the last line. If you do all that, it should work. But again, you shouldn't do all that; just use <code>paramiko</code> or another SSH library.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload