PHP-运行远程SSH脚本并将结果打印在网页中

First of all PHP is nowhere near my forte but I’m determined to learn. I’ve probably not been assigned the easiest of tasks for a first PHP project so bear with me whilst I try and explain. I want to within a simple form enter a server name, click submit and the following to occur there after:

  1. Copy a script
  2. Execute the script
  3. Return the results

Rather than tackle it all in one chunk, It made sense (to me) to break it down and start small:

<?php

$conn = ssh2_connect('10.x.x.x', 22);
ssh2_auth_password($conn, 'user', 'password');

ssh2_scp_send($conn, '/path/to/script', '/path/to/script', 0700);

$stream = ssh2_exec($conn, '/path/to/script');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);

?>

# php –f name-of-script.php

This worked fine from the command line and the expected response was returned.

I wanted to extend this and have the same functionality within a browser so I created a simple form as follows:

<form action="name-of-script.php" method="POST">
Server Name: <input type="text" name="node" size="20">
<input type="submit" value="Submit">
</form> 

Then altered my code slightly:

<?php

$node = $_POST["node"];

$conn = ssh2_connect($node, 22);
ssh2_auth_password($conn, 'user', 'password');

ssh2_scp_send($conn, '/path/to/script', '/path/to/script', 0700);

$stream = ssh2_exec($conn, '/path/to/script');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);

?>

Have I made a fundamental mistake by expecting all the SSH2 functions to produce the same result as the command line within a browser? All I see at the moment is a blank page after entering the server name and clicking submit.

doudiecai1572
doudiecai1572 谢谢回复。我添加了以上内容,但它似乎没有任何区别。我相信(无论如何我可以告诉)libssh2扩展已经安装。我当然可以在使用phpinfo()的浏览器中看到它;
5 年多之前 回复
doubishi8303
doubishi8303 也许Web服务器(或者如果CLI脚本在Web服务器上运行,至少是通过Apache的PHP)没有安装libssh2扩展。尝试添加ini_set('display_errors',1);在文件的顶部,看看它说的是什么。
5 年多之前 回复

1个回答



一个完整的男生错误! 我处理的文件是</ strong>后缀.html而不是.php! 一旦我重命名文件一切都很好。</ p>

工作代码:</ p>

  $ conn = ssh2_connect('10 .xxx',22  );; 
ssh2_auth_password($ conn,'user','password');

ssh2_scp_send($ conn,'/ path / to / script','/ path / to / script',0700);

$ stream = ssh2_exec($ conn,'/ path / to / script');
stream_set_blocking($ stream,true);
$ stream_out = ssh2_fetch_stream($ stream,SSH2_STREAM_STDIO);
echo stream_get_contents($ stream_out);
</ code> </ pre>
</ div>

展开原文

原文

A complete school boy error on my part! The file I was dealing with was suffixed .html not .php! Once I renamed the file all was well.

Working Code:

$conn = ssh2_connect('10.x.x.x', 22);
ssh2_auth_password($conn, 'user', 'password');

ssh2_scp_send($conn, '/path/to/script', '/path/to/script', 0700);

$stream = ssh2_exec($conn, '/path/to/script');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐