Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OPTION 1: You can re-use a ssh process by redirecting input using a PIPE. </p> <p>Here is a basic example:</p> <pre><code>[(Z) &lt;/tmp&gt; ]% touch input_file [(Z) &lt;/tmp&gt; ]% tailf input_file | ssh &lt;remote_host&gt; </code></pre> <p>Now try writing something into the file</p> <pre><code>[(Z) &lt;/tmp&gt; ]% echo "date" &gt;&gt; /tmp/input_file </code></pre> <p>Here is a way to make use of this in Python using subprocess module.</p> <pre><code>import subprocess SSH_CMD = "cat -| /usr/bin/ssh -o PasswordAuthentication=no -T -x %s " HOSTNAME = "127.0.0.1" s = subprocess.Popen(SSH_CMD%HOSTNAME , shell=True, close_fds=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) </code></pre> <p>This starts a subprocess which can be re-used. Please note that <code>close_fds=True</code> is required because of a known bug (<a href="http://bugs.python.org/issue2320" rel="nofollow">http://bugs.python.org/issue2320</a>). </p> <pre><code>&gt;&gt;&gt; REMOTE_CMD = "date" &gt;&gt;&gt; s.stdin.write( REMOTE_CMD + ... "\necho 'remote command completed with exit code = '$?\n") &gt;&gt;&gt; s.stdout.readline() 'Thu Feb 16 20:01:36 PST 2012\n' &gt;&gt;&gt; s.stdout.readline() 'remote command completed with exit code = 0\n' </code></pre> <p><code>echo 'remote command completed with exit code = '$?\n</code> line is used to know that the remote command finished and it is done writing to s.stdout. This is also useful to know the exit code of the remote command. </p> <p>To use the same subprocess for executing another remote command:</p> <pre><code>&gt;&gt;&gt; REMOTE_CMD = "uptime" &gt;&gt;&gt; s.stdin.write( REMOTE_CMD + ... "\necho 'remote command completed with exit code = '$?\n") &gt;&gt;&gt; s.stdout.readline() ' 20:02:17 up 28 days, 9:15, 48 users, load average: 0.01, 0.02, 0.05\n' &gt;&gt;&gt; s.stdout.readline() 'remote command completed with exit code = 0\n' </code></pre> <p>Coming back to your question, once you create a ssh subprocess, you can keep sending the remote commands. Once the user is quits, you can kill the subprocess.</p> <pre><code>&gt;&gt;&gt; s.kill() </code></pre> <p>OPTION 2: I have never used this, but ssh has a ControlMaster option for re-using ssh. Check man page for ssh_config(5)</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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