php 从 ssh2_exec 获取结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12086474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get result from ssh2_exec
提问by acrobat
I have a script that makes a SSH connection to a server (this works fine). Now I want to execute a command and echo the result I get from this command.
我有一个脚本可以建立到服务器的 SSH 连接(这很好用)。现在我想执行一个命令并回显我从这个命令得到的结果。
So I do this:
所以我这样做:
$stream = ssh2_exec($conn, 'php -v');
but I can't get it to show the response, var_dumpreturns resource(3) of type (stream).
但我无法让它显示响应,var_dump返回resource(3) of type (stream).
I have tried to use:
我曾尝试使用:
$stream = ssh2_exec($conn, 'php -v');
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
but the $stream_outreturns an empty string.
但$stream_out返回一个空字符串。
So is it possible to print the response as result of the script?
那么是否可以打印响应作为脚本的结果?
回答by acrobat
Ok i found the solution, so i post it for future reference
好的,我找到了解决方案,所以我将其发布以供将来参考
So to output the result of a command executed by ssh2_exec you should use following code setup
因此,要输出 ssh2_exec 执行的命令的结果,您应该使用以下代码设置
$stream = ssh2_exec($conn, 'php -v');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);
回答by TheHe
add:
添加:
echo stream_get_contents($stream);
the result is the STREAM and you have to fetch it's contents first...
结果是 STREAM,你必须先获取它的内容......
stream-fetch is only for fetching alternate sub-streams... (afaik)
stream-fetch 仅用于获取备用子流......(afaik)

