通过PHP在远程机器上执行命令
要介绍我的环境背景,请执行以下操作: 我有3台机器A、B和C A=Webserver,运行一个php网站,基本上充当B&C的接口 B=Linux Ubuntu机器,我有根访问权限、SSH以及通过SSH客户端在机器上工作所需的所有资源(我有一个用于此服务器的.ppk私钥文件) C=Linux上运行的MySql数据库服务器 我可以在C(Mysql)上成功地从A(php)执行查询并返回结果。但是现在我正试图从A在B上执行linux命令 例如 我有一个在B上运行的脚本,希望从a(php)执行一个命令来显示脚本的状态 在命令行中执行此操作很简单-./SomeScript状态 但是我想在服务器A上的im托管网站中显示此脚本的状态 甚至只需检查服务器A上服务器B的正常运行时间 无论如何,这是可能的。我一直在谷歌上搜索,但我哪儿也没找到,我也不太确定连接是否安全,因为这是一个封闭的网络,没有外部访问 如有任何建议,将不胜感激通过PHP在远程机器上执行命令,php,linux,ssh,Php,Linux,Ssh,要介绍我的环境背景,请执行以下操作: 我有3台机器A、B和C A=Webserver,运行一个php网站,基本上充当B&C的接口 B=Linux Ubuntu机器,我有根访问权限、SSH以及通过SSH客户端在机器上工作所需的所有资源(我有一个用于此服务器的.ppk私钥文件) C=Linux上运行的MySql数据库服务器 我可以在C(Mysql)上成功地从A(php)执行查询并返回结果。但是现在我正试图从A在B上执行linux命令 例如 我有一个在B上运行的脚本,希望从a(php)执行一个命令来显
谢谢在服务器A到服务器B上通过PHP运行SSH命令 以下是如何在linux中使用命令行运行ssh命令: 要在linux和PHP上运行命令,请使用以下命令 我希望这会让你开始朝着正确的方向看 请看这两篇文章,了解密码提示的自动化
<?php
$server = "serverB.example.org";
//ip address will work too i.e. 192.168.254.254 just make sure this is your public ip address not private as is the example
//specify your username
$username = "root";
//select port to use for SSH
$port = "22";
//command that will be run on server B
$command = "uptime";
//form full command with ssh and command, you will need to use links above for auto authentication help
$cmd_string = "ssh -p ".$port." ".$username."@".$server." ".$command;
//this will run the above command on server A (localhost of the php file)
exec($cmd_string, $output);
//return the output to the browser
//This will output the uptime for server B on page on server A
echo '<pre>';
print_r($output);
echo '</pre>';
?>
建议的流程是在服务器a上运行一个命令,以SSH到服务器B用于安全地SSH或SCP到远程服务器
安装
编写器需要phpseclib/phpseclib
// start connection
$connection = ssh2_connect("your remote ip address", your port);
ssh2_auth_password($connection,"your user name","your passwd");
// connection established, start to execute codes
$stream = ssh2_exec($connection, "your command");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
// block 2 streams simutaneously
stream_set_blocking($errorStream, true);
stream_set_blocking($stream,true);
// write stdout and stderr to log file
file_put_contents("your log file-path", date('Y-m-d H:i:s')."\nError: ".stream_get_contents($errorStream)."\nOutput: ".stream_get_contents($stream), FILE_APPEND)
// close 2 streams
fclose($errorStream);
fclose($stream);
// close remote connection
ssh2_exec($connection, 'exit');
我建议在远程机器上执行命令。您可以使用pecl
轻松安装它。之后,您可以使用ssh2\u exec()
函数执行命令,并使用ssh2\u fetch\u stream()
函数获取stderr。示例代码如下所示:
干杯。为什么不在
B
上放置一个脚本,提供正常运行时间等特定信息,并允许运行特定程序?不过,您需要让脚本检查IP是否确实来自服务器A
。谢谢,请看我能做什么,我的问题是在php页面的B上显示特定脚本的结果。结果可能会作为某种文本或html文件转储。在B上生成输出文件后,将其scp到A。但是首先如何从A连接到B?然后执行查询/命令,尤其是使用我拥有的公钥?是的,我有。我对A&b链接有root访问权限,但是我想直接从php向远程linux机器发送ssh命令?@Stroes请参阅编辑后的答案和一个示例,让您朝着正确的方向思考。这将直接从php运行ssh命令到远程计算机,并返回响应。@amaster我在lamp中执行了相同的代码,但没有返回输出。如果我在bash中使用相同的命令,它将给出输出。引用的youtube视频不可用
// start connection
$connection = ssh2_connect("your remote ip address", your port);
ssh2_auth_password($connection,"your user name","your passwd");
// connection established, start to execute codes
$stream = ssh2_exec($connection, "your command");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
// block 2 streams simutaneously
stream_set_blocking($errorStream, true);
stream_set_blocking($stream,true);
// write stdout and stderr to log file
file_put_contents("your log file-path", date('Y-m-d H:i:s')."\nError: ".stream_get_contents($errorStream)."\nOutput: ".stream_get_contents($stream), FILE_APPEND)
// close 2 streams
fclose($errorStream);
fclose($stream);
// close remote connection
ssh2_exec($connection, 'exit');