Linux 通过PHP在远程机器上执行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17028536/
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
Execute commands on remote machine via PHP
提问by Stroes
To give background on my environment:
提供有关我的环境的背景信息:
I have 3 machines A, B & C
我有 3 台机器 A、B 和 C
A = Webserver, running a php website which basically acts as an interface for B & C
A = Webserver,运行一个 php 网站,基本上充当 B 和 C 的接口
B = Linux Ubuntu machine, i have root access, SSH and all the needed goodness to work on the machine via a SSH client (i have a .ppk private key file for this server)
B = Linux Ubuntu 机器,我有 root 访问权限、SSH 以及通过 SSH 客户端在机器上工作所需的所有优点(我有一个用于该服务器的 .ppk 私钥文件)
C = MySql Database server running on Linux
C = 在 Linux 上运行的 MySql 数据库服务器
I can successfully execute queries from A (php) on C (Mysql) and return the results. But now im trying to execute linux commands on B from A.
我可以在 C (Mysql) 上从 A (php) 成功执行查询并返回结果。但是现在我试图从 A 在 B 上执行 linux 命令。
Eg.
例如。
I have a script thats running on B and would like to execute a command from A (php) to show the status of the script.
我有一个在 B 上运行的脚本,并想从 A (php) 执行命令以显示脚本的状态。
In Command line to do this is easy - ./SomeScript status
在命令行中执行此操作很容易 - ./SomeScript status
But i want to show the status of this script in the website im hosting on Server A.
但我想在服务器 A 上托管的网站中显示此脚本的状态。
Even just check the uptime of Server B on Server A.
甚至只是检查服务器 A 上服务器 B 的正常运行时间。
Is this in anyway possible. i have googled forever as it seems but im not getting anywhere, Im not too phased if the connection is secure or not as this is a closed network with no outside access to this network.
这无论如何可能。我一直在谷歌上搜索,但我没有找到任何地方,如果连接安全与否,我不会太分阶段,因为这是一个封闭的网络,无法从外部访问该网络。
Any advise would be highly appreciated.
任何建议将不胜感激。
Thanks
谢谢
采纳答案by amaster
Run SSH commands through PHP on server A to server B.
通过 PHP 在服务器 A 到服务器 B 上运行 SSH 命令。
Here is how to run ssh commands with the command line in linux: http://www.youtube.com/watch?NR=1&feature=fvwp&v=YLqqdQZHzsU
以下是如何在 linux 中使用命令行运行 ssh 命令:http: //www.youtube.com/watch?NR =1&feature =fvwp& v=YLqqdQZHzsU
In order to run commands on linux with PHP use the exec()command.
为了使用 PHP 在 linux 上运行命令,请使用exec()命令。
I hope this will get you started looking in the right direction.
我希望这会让你开始寻找正确的方向。
Look at these two posts for automating the password prompt
查看这两篇文章以自动提示密码
- https://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password
- https://serverfault.com/questions/187036/execute-ssh-command-without-password
- https://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password
- https://serverfault.com/questions/187036/execute-ssh-command-without-password
Here is a quick example with non-workingcode to get you thinking:
这是一个带有非工作代码的快速示例,可以让您思考:
<?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>';
?>
The recommended flow is to run a command on server A to SSH to server B
推荐的流程是在服务器 A 上运行命令以通过 SSH 连接到服务器 B