Tuesday, 15 January 2013

phpseclib - Parsing SSH2 Results in PHP -



phpseclib - Parsing SSH2 Results in PHP -

i running php vc9 nts 5.3.28 on windows server 2003 standard 32bit phpseclib 0.3.6. trying creating script connect palo alto networks firewall , execute command hash password. have next code:

<?php include 'net/ssh2.php'; define('net_ssh2_logging', net_ssh2_log_complex); $ssh = new net_ssh2('hostname'); echo ">logging in...\n"; if (!$ssh->login('user', 'password')) { exit('login failed'); } echo ">reading login results...\n"; /*echo $ssh->exec('request password-hash password test123');*/ $output = $ssh->read('user@pa-3020>'); echo $output . "\n"; echo ">writing request...\n"; $ssh->write("request password-hash password test123\n"); $ssh->settimeout(10); echo ">reading result...\n"; $output = $ssh->read('/^\$1\$.*$/', net_ssh2_read_regex); echo $output . "\n"; echo ">done.\n"; file_put_contents ('e:\php53\ssh2.log', $ssh->getlog()); ?>

i have 2 problems above code:

if leave out settimeout(10) code never exists next $ssh->read. if have in, code exists after timeout homecoming results.

the results returns including bunch of stuff shouldn't 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 want line starts $1$ (line 3 above). figure has regex can't figure out what.

if run command interactively putty following:

user@pa-3020> request password-hash password test123 $1$pxqhdlco$mrsvuswtitc3qimm4w.xz1 user@pa-3020>

update:

as per suggestions neubert below, replacing line $output = $ssh->read... next code works:

$output = $ssh->read('/\$1\$.*/', net_ssh2_read_regex); $output = preg_replace('/.*\$1\$/s','\$1\$', $output);

the results returns including bunch of stuff shouldn't 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 ansi escape codes. can utilize file_ansi remove them. more info:

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

anyway, guess need redo regex. eg.

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

instead of doing this:

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

the thing is... ^ matches @ start of line , $ matches @ end. when $ssh->write(...) command echo'd , there's new line , output back. that'd prevent ^ working. , $ @ end.. per own illustration $1$ doesn't occur @ end of line. that's why code isn't working.

php phpseclib

No comments:

Post a Comment