doukong1901 2014-06-23 16:43
浏览 87
已采纳

在PHP中解析SSH2结果

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:

  1. 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.
  2. 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);
  • 写回答

1条回答 默认 最新

  • douhuo3696 2014-06-23 19:05
    关注

    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>

    Those are ANSI escape codes. You can use File_ANSI to remove them. More info:

    http://phpseclib.sourceforge.net/ssh/examples.html#top

    Anyway, my guess would be that you need to redo your regex. eg.

    $output = $ssh->read('/^\$1\$.*$/', NET_SSH2_READ_REGEX);
    

    Instead of doing that do this:

    $output = $ssh->read('/\$1\$/', NET_SSH2_READ_REGEX);
    

    The thing is... ^ matches at the start of the line and $ matches at the end. Usually when you do $ssh->write(...) the command is echo'd back to you and then there's a new line and then you get your output back. So that'd prevent ^ from working. And as for the $ at the end.. well per your own example $1$ doesn't occur at the end of a line. So that's why your code isn't working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?