简体   繁体   中英

PHP Net_SSH2 - exec() how to get the command return code

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??

$ssh->getExitStatus()能做你想要的吗?

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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM