【问题标题】:Execute commands on remote machine via PHP通过 PHP 在远程机器上执行命令
【发布时间】:2013-06-06 09:12:21
【问题描述】:

提供我的环境背景:

我有 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 status

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

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

这是否可能。我一直在谷歌上搜索,但我没有到达任何地方,如果连接是否安全,我不会太分阶段,因为这是一个封闭的网络,没有外部访问这个网络。

任何建议将不胜感激。

谢谢

【问题讨论】:

  • 为什么不在B 上放置一个脚本,提供正常运行时间等特定信息,并允许运行特定程序?不过,您需要让脚本检查 IP 确实来自服务器 A
  • 谢谢,我能做到,我遇到的问题是在 A 上的 php 页面上显示 B 上特定脚本的结果
  • 结果可能被转储为某种文本或 html 文件。在 B 上生成输出文件后,将其 scp 到 A。
  • 但是我如何首先从 A 连接到 B?然后使用我拥有的公钥执行查询/命令?
  • 是的,我愿意。我在 A 和 B 上具有 root 访问权限

标签: php linux ssh


【解决方案1】:

通过服务器 A 上的 PHP 向服务器 B 运行 SSH 命令。

这里是如何在 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>';
?>

推荐的流程是在服务器 A 上运行命令以 SSH 到服务器 B

【讨论】:

  • 感谢链接,但是我想直接从 php 发送 ssh 命令到远程 linux 机器?
  • @Stroes 请查看带有示例的已编辑答案,以使您朝着正确的方向思考。这将直接从 php 运行 ssh 命令到远程机器并返回响应。
  • @amaster 我已经在我的灯中执行了相同的代码,但它没有返回输出。如果我在我的 bash 中使用相同的命令,它会给出输出。
  • 引用的 youtube 视频不可用
【解决方案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

【讨论】:

    【解决方案3】:

    我推荐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');
    

    干杯。

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 2012-07-27
      • 2013-11-22
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多