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:
<?php
include 'Net/SSH2.php';
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
$ssh = new Net_SSH2('hostname');
echo ">Logging in...
";
if (!$ssh->login('user', 'password')) {
exit('Login Failed');
}
echo ">Reading login results...
";
/*echo $ssh->exec('request password-hash password test123');*/
$output = $ssh->read('user@PA-3020>');
echo $output . "
";
echo ">Writing request...
";
$ssh->write("request password-hash password test123
");
$ssh->setTimeout(10);
echo ">Reading result...
";
$output = $ssh->read('/^\$1\$.*$/', NET_SSH2_READ_REGEX);
echo $output . "
";
echo ">Done.
";
file_put_contents ('E:\PHP53\ssh2.log', $ssh->getLog());
?>
I have two problems with the above code:
- 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.
-
The results it returns are including a bunch of stuff that shouldn't be there:
?[Kuser@PA-3020> request password-hash password test123 ?[?1h?=?[24;1H?[K $1$dgkhwrxe$kddYFmKCq9.zfiBKPAyN61
?[24;1H?[K?[?1l?>user@PA-3020>
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.
If I run the command interactively with PuTTY I get the following:
user@PA-3020> request password-hash password test123
$1$pxqhdlco$MRsVusWtItC3QiMm4W.xZ1
user@PA-3020>
UPDATE:
As per suggestions from neubert below, replacing the line with $output = $ssh->read... with the following code works:
$output = $ssh->read('/\$1\$.*/', NET_SSH2_READ_REGEX);
$output = preg_replace('/.*\$1\$/s','\$1\$', $output);