【问题标题】:Send data between two remote servers with popen, fgets and ssh使用 popen、fgets 和 ssh 在两个远程服务器之间发送数据
【发布时间】:2013-04-16 17:47:00
【问题描述】:

我尝试通过 ssh 和管道与两台机器通信,以从一台机器到另一台机器获取消息。 第二个使用 sdtin 从第一台机器读取消息并写入文本文件。

我有一台机器,我有这个程序,但它不起作用......

$message = "Hello Boy";
$action = ('ssh root@machineTwo script.php'); 
$handle = popen($action, 'w');

if($handle){
   echo $message;
   pclose($handle);
}

在另一台机器上,我有 machineTwo:

 $filename = "test.txt";    
     if(!$fd = fopen($filename, "w");
     echo "error";
        }
     else {
            $action = fgets(STDIN);
            fwrite($fd, $action);
    /*On ferme le fichier*/
    fclose($fd);}

【问题讨论】:

    标签: php ssh pipe stdin popen


    【解决方案1】:

    这是最简单的方法(使用phpseclib, a pure PHP SSH2 implementation):

    <?php
    include('Net/SSH2.php');
    
    $ssh = new Net_SSH2('www.domain.tld');
    if (!$ssh->login('username', 'password')) {
        exit('Login Failed');
    }
    
    echo $ssh->exec('php script.php');
    ?>
    

    使用 RSA 私钥:

    <?php
    include('Net/SSH2.php');
    
    $ssh = new Net_SSH2('www.domain.tld');
    $key = new Crypt_RSA();
    $key->loadKey(file_get_contents('privatekey'));
    if (!$ssh->login('username', $key)) {
        exit('Login Failed');
    }
    
    echo $ssh->exec('php script.php');
    ?>
    

    如果 script.php 在标准输入上侦听,您可以使用 read() / write() 或使用 enablePTY()

    【讨论】:

      【解决方案2】:

      此解决方案有效:

      一体机

      在通过 ssh 连接到 MACHINE TWO 后,我向 MACHINE TWO 发送了一条消息。我使用popenfwrite

      //MACHINE ONE
      $message = "Hello Boy";
      $action = ('ssh root@machineTwo script.php');  //conection by ssh-rsa
      $handle = popen($action, 'w'); //pipe open between machineOne & two
      
      if($handle){
         fwrite($handle, $message); //write in machineTwo
         pclose($handle);
      }
      

      机器二

      我用fopen 打开一个文件并用fgets(STDIN); 得到MACHINE ONE 的消息。我将消息写入打开的文件中。

      //MACHINETWO
      $filename = "test.txt";    
      if(!$fd = fopen($filename, "w");
          echo "error";
      }
      
      else
      {   
          $message = fgets(STDIN);
          fwrite($fd, $message); //text.txt have now Hello World !
          /*we close the file*/
          fclose($fd);    
      }
      

      【讨论】:

        【解决方案3】:

        Popen 主要用于让两个本地程序使用“管道文件”进行通信。

        要实现你想要的,你应该试试 SSH2 PHP 库(一个有趣的链接http://kvz.io/blog/2007/07/24/make-ssh-connections-with-php/

        在你的情况下,你会在 machineOne 上为你的 php 脚本做类似的事情:

        if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
        if (!($con = ssh2_connect("machineTwo", 22))) {
            echo "fail: unable to establish connection\n";
        } else {
            if (!ssh2_auth_password($con, "root", "yourpass")) {
                echo "fail: unable to authenticate\n";
            } else {
                echo "okay: logged in...\n";
        
                 if (!($stream = ssh2_exec($con, "php script.php"))) { //execute php script on machineTwo
                        echo "fail executing command\n";
                    } else {
                        // collect returning data from command
                        stream_set_blocking($stream, true);
                        $data = "";
                        while ($buf = fread($stream,4096)) {
                            $data .= $buf;
                        }
                        fclose($stream);
                        echo $data; //text returned by your script.php
                    }
            }
        }
        

        我认为您有充分的理由这样做,但为什么要使用 PHP 呢?

        【讨论】:

          猜你喜欢
          • 2013-10-21
          • 1970-01-01
          • 1970-01-01
          • 2014-01-21
          • 1970-01-01
          • 2011-02-10
          • 1970-01-01
          • 2023-03-14
          • 1970-01-01
          相关资源
          最近更新 更多