PHP如何在执行bash脚本后从远程网关输出错误消息
我有一个简单的PHP函数,可以根据用户选择并提交的单选按钮选项调用脚本(up.sh或down.sh),调用远程网关。我能够从远程网关成功运行脚本,但是,每当出现问题时,我的网页不会显示错误。有没有办法在网页上回显错误 testexe.phpPHP如何在执行bash脚本后从远程网关输出错误消息,php,bash,Php,Bash,我有一个简单的PHP函数,可以根据用户选择并提交的单选按钮选项调用脚本(up.sh或down.sh),调用远程网关。我能够从远程网关成功运行脚本,但是,每当出现问题时,我的网页不会显示错误。有没有办法在网页上回显错误 testexe.php if ($_SERVER['REQUEST_METHOD'] == 'POST'){ //create the ssh connection if ($connection = @ssh2_connect($gateway, 22))
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//create the ssh connection
if ($connection = @ssh2_connect($gateway, 22)) {
ssh2_auth_password($connection, $gwUser, $gwPwd);
if(isset($_POST['option']) && $_POST['option'] == 1) {
$stream = ssh2_exec($connection, "/home/user/aust/up.sh");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo '<pre>' . stream_get_contents($stream_out) . '</pre>';
}
if(isset($_POST['option']) && $_POST['option'] == 2) {
$stream = ssh2_exec($connection, "/home/user/aust/down.sh");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo '<pre>' . stream_get_contents($stream_out) . '</pre>';
}
}
}
错误输出:
您正在使用标志
SSH2\u STREAM\u STDIO
,当事情按预期运行时,该标志应该会给您一个结果。如果有问题,我相信您需要标志SSH2\u STREAM\u STDERR
请参见ssh2\u fetch\u流
:
和
SSH2\u STREAM\u STDERR
关于预定义常数:perfect,还有最后一件事是有办法同时输出SSH2\u STREAM\u STDIO和SSH2\u STREAM\u STDIO吗?似乎您一次只能请求一个。如果您知道将有输出,但没有从SSH2\u STREAM\u STDIO
返回任何内容,则可以检查SSH2\u STREAM\u STDERR
。否则,每次都可以跳过检查。
#!/bin/bash
echo 'expect -f ./scripts/myExpectScript.exp' //I can see this on the webpage
expect -f ./scripts/myExpectScript.exp //this throws an error but i dont see the error on the page.
echo
expect -f ./scripts/myExpectScript.exp
couldn't read file "./scripts/myExpectScript.exp": no such file or directory //I want to see this on the webpage.