Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

php - get result from ssh2_exec

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.

So I do this:

$stream = ssh2_exec($conn, 'php -v');

but I can't get it to show the response, var_dump returns 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_out returns an empty string.

So is it possible to print the response as result of the script?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

$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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...