title: Parsing SSH2 Results in PHP
body: <p>I am running PHP vc9 NTS 5.3.28 on Windows Server 2003 Standard 32bit with phpseclib 0.3.6.  I am trying to creating a script that will connect to a Palo Alto Networks firewall and execute a command to hash a password.  I have the following code:</p>
  
  <pre><code>&lt;?php
  include 'Net/SSH2.php';
  
  define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
  
  $ssh = new Net_SSH2('hostname');
  echo "&gt;Logging in...\n";
  if (!$ssh-&gt;login('user', 'password')) {
      exit('Login Failed');
  }
  echo "&gt;Reading login results...\n";
  /*echo $ssh-&gt;exec('request password-hash password test123');*/
  $output = $ssh-&gt;read('user@PA-3020&gt;');
  echo $output . "\n";
  echo "&gt;Writing request...\n";
  $ssh-&gt;write("request password-hash password test123\n");
  $ssh-&gt;setTimeout(10);
  echo "&gt;Reading result...\n";
  $output = $ssh-&gt;read('/^\$1\$.*$/', NET_SSH2_READ_REGEX);
  echo $output . "\n";
  echo "&gt;Done.\n";
  file_put_contents ('E:\PHP53\ssh2.log', $ssh-&gt;getLog());
  ?&gt;
  </code></pre>
  
  <p>I have two problems with the above code:</p>
  
  <ol>
  <li>If I leave out the setTimeout(10) then the code never exists the next $ssh->read.  If I have it in, then the code exists only after the timeout but does return results.</li>
  <li><p>The results it returns are including a bunch of stuff that shouldn't be there:</p>
  
  <p>?[Kuser@PA-3020> request password-hash password test123
  ?[?1h?=?[24;1H?[K
  $1$dgkhwrxe$kddYFmKCq9.zfiBKPAyN61</p>
  
  <p>?[24;1H?[K?[?1l?>user@PA-3020></p></li>
  </ol>
  
  <p>I only want the line that starts with $1$ (line 3 above).  I figure it has something to do with the regex but I can't figure out what.</p>
  
  <p>If I run the command interactively with PuTTY I get the following:</p>
  
  <pre><code>user@PA-3020&gt; request password-hash password test123
  
  $1$pxqhdlco$MRsVusWtItC3QiMm4W.xZ1
  
  user@PA-3020&gt;
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>As per suggestions from neubert below, replacing the line with $output = $ssh->read... with the following code works:</p>
  
  <pre><code>$output = $ssh-&gt;read('/\$1\$.*/', NET_SSH2_READ_REGEX);
  $output = preg_replace('/.*\$1\$/s','\$1\$', $output);
  </code></pre>