title: pexpect determine if ssh exists: using ps and grep
body: <p>I am trying to do a script to automate ssh tunnelling on Ubuntu 13.04 64 bit. It is based on the ssh_tunnel example found in the Pexpect module (<a href="http://www.noah.org/wiki/pexpect#ssh_tunnel.py" rel="nofollow">http://www.noah.org/wiki/pexpect#ssh_tunnel.py</a>)</p>
  
  <p>But there is a problem with running the code on my machine. I wanted to check if there are any active, non-defunct ssh tunnels already running. If I check this in the terminal, I get ssh-agent and ssh  coming up:</p>
  
  <pre><code>    user@comp$ ps -e|grep ssh
       3578 ?        00:00:00 ssh-agent
       9686 pts/0    00:10:31 ssh &lt;defunct&gt;
      10955 pts/0    00:00:02 ssh
  </code></pre>
  
  <p>So I tried filtering the results using the -v option (invert selection) of grep, I am able to get active tunnels. This will allow me to determine if I need to open a new tunnel:</p>
  
  <pre><code>    user@comp$ ps -e|grep ssh|grep -v agent|grep -v def
      10995 pts/0    00:00:00 ssh
  </code></pre>
  
  <p>Unfortunately, if I call the above command using pexpect.spawn, and then do the expect as follows:</p>
  
  <pre><code>ps = pexpect.spawn ('ps -e|grep ssh | grep -v agent | grep -v def')
  res1 = ps.expect (['ssh', pexpect.EOF, pexpect.TIMEOUT])
  </code></pre>
  
  <p>res1 returns as 1, indicating pexpect.EOF (there is no ssh process found)</p>
  
  <p>What am I doing wrong here? Else, is there another way to check if there is already an ssh running so that I will not open another</p>