简体   繁体   English

PHP Net_SSH2 - exec()如何获取命令返回码

[英]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. (Linux)和echo %errorlevel% (Win)不起作用。

Any Ideas?? 有任何想法吗??

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

Found one option (a workaround). 找到一个选项(解决方法)。 PHP ssh2_exec channel exit status? PHP ssh2_exec通道退出状态?

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!! 欣赏它!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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