通过PHP在远程计算机上执行命令
要提供有关我的环境的背景:
To give background on my environment:
我有3台机器A,B和C
I have 3 machines A, B & C
A =网络服务器,运行一个php网站,该网站基本上充当B&的接口. C
A = Webserver, running a php website which basically acts as an interface for B & C
B = Linux Ubuntu计算机,我具有root访问权,SSH和通过SSH客户端在计算机上工作所需的所有好处(我有此服务器的.ppk私钥文件)
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)
C =在Linux上运行的MySql数据库服务器
C = MySql Database server running on Linux
我可以在C(Mysql)上成功地从A(php)执行查询并返回结果.但是现在我试图从A在B上执行linux命令.
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.
例如.
我有一个在B上运行的脚本,并且想从A(php)执行命令以显示脚本的状态.
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.
在命令行中这样做很容易-./SomeScript状态
In Command line to do this is easy - ./SomeScript status
但是我想在服务器A上托管的网站中显示此脚本的状态.
But i want to show the status of this script in the website im hosting on Server A.
即使只是检查服务器A上服务器B的正常运行时间.
Even just check the uptime of Server B on Server A.
这是否有可能.我似乎一直在搜索Google,但是我什么都没找到.如果连接是否安全,我不会太过分阶段,因为这是一个封闭的网络,无法从外部访问该网络.
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.
任何建议将不胜感激.
谢谢
在服务器A上通过PHP运行SSH命令到服务器B.
Run SSH commands through PHP on server A to server B.
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上使用PHP运行命令,请使用 exec()命令.
In order to run commands on linux with PHP use the exec() command.
我希望这会帮助您开始朝正确的方向看.
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 -automatic-ssh-login-with-password
- https://serverfault.com/questions/187036/execute-ssh-command-without -密码
- 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-working code 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>';
?>
建议的流程是在服务器A上运行命令以SSH到服务器B
The recommended flow is to run a command on server A to SSH to server B