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

11

关于我的环境背景:

我有3台机器A、B和C

A = Web服务器,运行一台PHP网站,基本上作为B和C的接口

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

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

我可以成功地从A(php)在C(Mysql)上执行查询并返回结果。但现在我正在尝试从A执行B上的linux命令。

例如:

我有一个在B上运行的脚本,并且想要从A(php)执行一个命令来显示脚本的状态。

在命令行中,这样做很容易 - ./SomeScript status

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

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

这有任何可能吗?我已经搜索了很长时间,但似乎找不到答案。如果连接不安全也没关系,因为这是一个没有对外部访问的封闭网络。

任何建议将不胜感激。

谢谢。


为什么不在服务器B上放置一个脚本,可以提供特定的信息,比如运行时间,并允许运行特定的程序?但是你需要让脚本检查IP确实来自服务器A - Dave Chen
谢谢,我知道我能做到的,但我遇到的问题是在A的php页面上显示来自B上特定脚本的结果。 - Stroes
结果可能会被转储为某种文本或HTML文件。在B上生成输出文件后,将其通过scp传输到A。 - kainaw
但是我如何从A首先连接到B?然后,如何使用我拥有的公钥执行查询/命令? - Stroes
你有A的root访问权限和运行命令的能力吗? - amaster
1
是的,我有。我在A和B上拥有root访问权限。 - Stroes
3个回答

12

在服务器A上通过PHP执行SSH命令连接到服务器B。

以下是如何在Linux命令行中运行SSH命令的方法:http://www.youtube.com/watch?NR=1&feature=fvwp&v=YLqqdQZHzsU

要在Linux上使用PHP运行命令,请使用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>';
?>

推荐的步骤是在服务器A上运行一个命令来SSH到服务器B。


谢谢提供链接,但我想直接从PHP向远程Linux机器发送SSH命令? - Stroes
@Stroes,请查看编辑后的答案,其中包含一个示例,以便您朝着正确的方向思考。这将直接从PHP运行ssh命令到远程机器并返回响应。 - amaster
@amaster,我在我的LAMP中执行了相同的代码,但没有返回输出。如果我在我的bash中使用相同的命令,它会给出输出。 - ShaanSetia
2
所引用的 YouTube 视频不可用。 - anastymous

2

使用phpseclib安全地SSH或SCP到远程服务器

安装方法: composer require phpseclib/phpseclib

use phpseclib\Crypt\RSA;
use phpseclib\Net\SSH2;
use phpseclib\Net\SCP;

// Load your private key
$key = new RSA();
$key->loadKey('private key string');

// Connect to the server
$ssh = new SSH2('ip_address', 'port', 'timeout');
if (!$ssh->login('username', $key)) {
    throw new Exception("Unable to connect");
}

// Run a remote command
echo $ssh->exec('whoami');

// SCP put a string
$result = (new SCP($ssh))->put('remotePath', 'content to put');
// SCP put a file
$result = (new SCP($ssh))->put('remotePath', 'localPath', SCP::SOURCE_LOCAL_FILE);

// SCP get a file
$result = (new SCP($this->ssh))->get('remotePath', 'localPath');

// $result is true or false

1

我建议使用SSH2来在远程机器上执行命令。你可以使用pecl轻松安装它。安装完成后,你可以使用ssh2_exec()函数来执行命令,使用ssh2_fetch_stream()函数来获取stderr。以下是示例代码:

// 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');

干杯。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接