<p>I need to launch a server on the remote machine and retrieve the port number that the server process is lsitening on.  When invoked, the server will listen on a random port and output the port number on stderr.</p>

<p>I want to automate the process of logging on to the remote machine, launching the process, and retrieving the port number. I wrote a Python script called "<code>invokejob.py</code>" that lives on the remote machine to act as a wrapper that invokes the job and then returns the port number, it looks like this:</p>

<pre><code>import re, subprocess
executable = ... # Name of executable
regex = ... # Regex to extract the port number from the output
p = subprocess.Popen(executable,
    bufsize=1, # line buffered
    stderr=subprocess.PIPE
    )
s = p.stderr.readline()
port = re.match(regex).groups()[0]
print port
</code></pre>

<p>If I log in interactively, this script works:</p>

<pre><code>$ ssh remotehost.example.com
Last login: Thu Aug 28 17:31:18 2008 from localhost
$ ./invokejob.py
63409
$ exit
logout
Connection to remotehost.example.com closed.
</code></pre>

<p>(Note: successful logout, it did not hang).</p>

<p>However, if I try to invoke it from the command-line, it just hangs:</p>

<pre><code>$ ssh remotehost.example.com invokejob.py
</code></pre>

<p>Does anybody know why it hangs in the second case, and what I can do to avoid this? </p>

<p>Note that I need to retrieve the output of the program, so I can't just use the ssh "-f" flag or redirect standard output.</p>

[{'answer_id': 33486, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 3, 'selected': True, 'text': "<blockquote>\n<pre><code>s = p.stderr.readline()\n</code></pre>\n</blockquote>\n\n<p>I suspect it's the above line.  When you invoke a command directly through ssh, you don't get your full pty (assuming Linux), and thus no stderr to read from.</p>\n\n<p>When you log in interactively, stdin, stdout, and stderr are set up for you, and so your script works.</p>\n"}, {'answer_id': 33673, 'author': 'Misha M', 'author_id': 3467, 'author_profile': 'https://Stackoverflow.com/users/3467', 'pm_score': 0, 'selected': False, 'text': "<p>what if you do the following:</p>\n\n<h2><code>ssh &lt;remote host&gt; '&lt;your command&gt; ;&lt;your regexp using awk or something&gt;'</code></h2>\n\n<p>For example</p>\n\n<h2><code>ssh &lt;remote host&gt; '&lt;your program&gt;; ps aux | awk \\'/root/ {print $2}\\''</code></h2>\n\n<p>This will connect to , execute  and then print each PSID for any user root or any process with root in its description.</p>\n\n<p>I have used this method for running all kinds of commands on remote machines.  The catch is to wrap the command(s) you wish to execute in single quotation marks (') and to separate each command with a semi-colon (;).</p>\n"}]
['https://Stackoverflow.com/questions/33475', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/742/']