通过PHP在远程机器上执行命令

Str*_*oes 7 php linux ssh

给出我的环境背景:

我有3台机器A,B和C.

A = Webserver,运行一个php网站,它基本上充当B&C的界面

B = Linux Ubuntu机器,我有root访问权限,SSH以及通过SSH客户端在机器上工作所需的所有优点(我有一个用于此服务器的.ppk私钥文件)

C =在Linux上运行的MySql数据库服务器

我可以在C(Mysql)上成功执行来自A(php)的查询并返回结果.但是现在我试图在A上从B执行linux命令

例如.

我有一个在B上运行的脚本,并希望从A(php)执行命令以显示脚本的状态.

在命令行中执行此操作很简单 - ./SomeScript状态

但我想在服务器A上托管的网站上显示此脚本的状态.

甚至只检查服务器A上服务器B的正常运行时间.

无论如何这是可能的.我已经google了看似永远,但我没有得到任何地方,如果连接是安全的,我不会分阶段,因为这是一个没有外部访问该网络的封闭网络.

任何建议将受到高度赞赏.

谢谢

ama*_*ter 10

通过服务器A上的PHP运行SSH命令到服务器B.

以下是如何使用linux中的命令行运行ssh命令:http://www.youtube.com/watch? NR = 1& feature = fvwp&v = YLqqdQZHzsU

要使用PHP在Linux上运行命令,请使用exec()命令.

我希望这会让你开始寻找正确的方向.

查看这两个帖子以自动化密码提示

这是一个使用非工作代码的快速示例,可以让您思考:

<?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>';
?>
Run Code Online (Sandbox Code Playgroud)

建议的流程是在服务器A上运行命令到SSH到服务器B.

  • 引用的 YouTube 视频不可用 (2认同)