Question

How can I get the return code for the command that is executed :

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('cat /tmp/file_tmp');
//verify the return code
echo $ssh->exec('echo $?');

?>

echo $?(Linux) and echo %errorlevel%(Win) don't work.

Any Ideas??

Was it helpful?

Solution 2

Found one option (a workaround). PHP ssh2_exec channel exit status?

The below code worked for me, serves my purpose:

$command .= ';echo "[return_code:$?]"';
$output = $ssh->exec($command);
preg_match( '/\[return_code:(.*?)\]/', $output, $match );
$return_code = $match[1];
echo "\r\n". $output;
echo "\r\n".$return_code;

Thanks everyone for your inputs. Appreciate it!!

OTHER TIPS

Does $ssh->getExitStatus() do what you're wanting?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top