Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP在远程服务器中执行命令_Php_Unix_Exec_Server_Remote Server - Fatal编程技术网

使用PHP在远程服务器中执行命令

使用PHP在远程服务器中执行命令,php,unix,exec,server,remote-server,Php,Unix,Exec,Server,Remote Server,我有一个用PHP编写的网页,我正试图对远程服务器执行一系列命令。当运行这些命令时,我需要在另一台机器上运行它们,以便能够通过防火墙 我可以寻找的总体思路是在远程服务器中执行此操作的输出 foreach ($filter as $filtered) { echo './script_to_execute_in_a_remote_server '.$filtered } ./script_to_execute_in_a_remote_server.sh attribute1 attribu

我有一个用PHP编写的网页,我正试图对远程服务器执行一系列命令。当运行这些命令时,我需要在另一台机器上运行它们,以便能够通过防火墙

我可以寻找的总体思路是在远程服务器中执行此操作的输出

foreach ($filter as $filtered) {
    echo './script_to_execute_in_a_remote_server '.$filtered
}
./script_to_execute_in_a_remote_server.sh attribute1 attribute2
例如,我将在远程服务器上执行以下操作

foreach ($filter as $filtered) {
    echo './script_to_execute_in_a_remote_server '.$filtered
}
./script_to_execute_in_a_remote_server.sh attribute1 attribute2
我使用SSH连接到中间服务器,连接到防火墙

 ssh root@firewall;
ssh root@server1;
从那里,我使用SSH连接到远程服务器(这次是从防火墙)

一旦我最终通过SSH连接到远程服务器,我就会执行之前打印的命令

./script_to_execute_in_a_remote_server.sh attribute1 attribute2

我能做到这一点的最佳方法是什么?

一旦通过php ssh2连接到防火墙,就可以通过php ssh2会话传递常规cli ssh命令

ssh root@server1 ./script_to_execute_in_a_remote_server.sh attribute1 attribute2

发送此命令将通知防火墙连接到server1,执行该命令,然后在完成后断开连接。

当前版本是否不工作?还是你有兴趣优化它?@MarkBaker非常感谢你的回复!我知道如何使用ssh2exec,但不知道如何通过防火墙将命令传递到服务器(两个连接)。带有php网页的Web服务器---->防火墙---->我想从php代码执行命令的服务器欢迎使用堆栈溢出!这个问题有很多可移动的部分,但是你已经很好地定义了它。我已经改写了很多内容,以使执行流程更加清晰,并将核心问题移到最后(这样人们就知道“这”是什么)。我唯一建议的是添加一些您尝试过使用的代码,并询问一个关于它为什么不起作用的特定问题。