Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用popen、fgets和ssh在两个远程服务器之间发送数据_Php_Ssh_Pipe_Stdin_Popen - Fatal编程技术网

Php 使用popen、fgets和ssh在两个远程服务器之间发送数据

Php 使用popen、fgets和ssh在两个远程服务器之间发送数据,php,ssh,pipe,stdin,popen,Php,Ssh,Pipe,Stdin,Popen,我尝试通过ssh和管道与两台机器通信,以从一台机器到另一台机器获取消息。 第二个使用sdtin从第一台机器读取消息并写入文本文件 我有一台机器,我有这个程序,但它不工作 $message = "Hello Boy"; $action = ('ssh root@machineTwo script.php'); $handle = popen($action, 'w'); if($handle){ echo $message; pclose($handle); } 在另一台机器上,

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

我有一台机器,我有这个程序,但它不工作

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

if($handle){
   echo $message;
   pclose($handle);
}
在另一台机器上,机器我有:

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

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

要实现您想要的,您应该尝试ssh2php库(一个有趣的链接)

在您的情况下,您可以在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?

此解决方案正在运行:

机器一号

在使用ssh连接到机器2之后,我向机器2发送一条消息。我使用
popen
fwrite

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

以下是最简单的方法(使用):


使用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在stdin上侦听,您可能会执行或使用

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