Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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
Javascript 允许PHP脚本通过SSH连接到多个服务器_Javascript_Php_For Loop_Ssh_Phpseclib - Fatal编程技术网

Javascript 允许PHP脚本通过SSH连接到多个服务器

Javascript 允许PHP脚本通过SSH连接到多个服务器,javascript,php,for-loop,ssh,phpseclib,Javascript,Php,For Loop,Ssh,Phpseclib,我有一个CSV文件,它位于Raspberry PI上,并输出: 2018-03-22 12:43:21,NM_Test.h264,-2 在我的主机服务器上,我有一个PHP脚本,它获取CSV文件的输出,并在我的网页上显示为HTML表: $command = "ssh -p 97 -i /var/www/html/test.rsa pi@192.168.xxx.xxx tail -1 /var/log/playlog.csv"; $output = exec($command); $array =

我有一个CSV文件,它位于Raspberry PI上,并输出:

2018-03-22 12:43:21,NM_Test.h264,-2
在我的主机服务器上,我有一个PHP脚本,它获取CSV文件的输出,并在我的网页上显示为HTML表:

$command = "ssh -p 97 -i /var/www/html/test.rsa pi@192.168.xxx.xxx tail -1 /var/log/playlog.csv";
$output = exec($command);
$array = explode(',',$output);

echo '<div class="container"><table class="table table-striped">
 <tr>
 <th>Status</th>
 <th>Name</th>
 <th>Date/Time</th>
 <th>Playing</th>
 <th>Error</th>
 </tr>
 <tr>
 <td>';
 if(in_array('0', $array, true)){
  echo '<div id="circleGreen"></div>';
 }

 if (in_array('-2', $array, true)){
  echo '<div id="circleRed"></div>';
 }
 echo'</td>
 <td>Guildford test</td>
 <td>'.$array[0].'</td>
 <td>'.$array[1].'</td>
 <td>';
但我希望有一个更有效的解决方案来解决我的问题

更新

根据建议,我使用phpsec库将SSH连接到PI上:

<?php
include('Net/SSH2.php');
include('phpseclib1.0.10/Crypt/RSA.php');

$ssh = new Net_SSH2('192.xxx.xxx.xxx', 97);
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('test.rsa'));

if (!$ssh->login('pi', $key)){
 exit ('Login Failed');
}

echo $ssh->exec('ls -la');
?>

但是通过这样做,我仍然必须键入IP地址和RSA密钥名。我想要一个能够让我快速ssh到多个pi并执行exec命令的解决方案


我能想到的一个可能的解决方案是允许我的脚本读取已知的\u hosts文件?有可能吗?

这就是我要做的。。。(创建我自己的配置文件)

然后在
config.php中

return [
    [
        "ip" : "127.0.0.1",
        "port": 97,
        "key" : 'ssh-dss AAAAB3NzaC1kc3MAA...c3=',
    ]
    [ ... ]
];
您甚至可以通过如下操作以编程方式修改配置文件

  file_put_contants('config.php', '<?php'."\nreturn ".var_export($config,true).";\n");

file\u put\u contants('config.php','这就是我要做的…(制作我自己的配置文件)

然后在
config.php中

return [
    [
        "ip" : "127.0.0.1",
        "port": 97,
        "key" : 'ssh-dss AAAAB3NzaC1kc3MAA...c3=',
    ]
    [ ... ]
];
您甚至可以通过如下操作以编程方式修改配置文件

  file_put_contants('config.php', '<?php'."\nreturn ".var_export($config,true).";\n");

file\u put\u contants('config.php',我建议使用PHPSecLib2.0,它比SSH(sFTP)的其他任何东西都好IMO>你能给我举一个如何使用它的例子吗?没有太多的文档。有很多文档,你只需要知道它在哪里看起来比ssh2扩展更容易使用。exec的另一个问题是它不是一个交互式shell,每个命令本身都是无状态的。你必须/可以尝试使用ssh2扩展,最后我知道它在windows上被破坏了。作为奖励,它还可以使用
AES
SSL
(比如制作自签名证书)还有很多其他加密的东西。我发现它唯一缺少的是没有PGP加密,但是没有真正易于使用的库。谢谢你的详细回复和链接。我成功地获得了phpseclib!我将更新我的答案,以便你能看到我建议使用PHPSecLib2.0的内容,这是wa对于SSH(sFTP),y比其他任何东西都好IMO>你能给我举一个如何使用它的例子吗?没有太多的文档。有很多文档,你只需要知道它在哪里看起来比ssh2扩展更容易使用。exec的另一个问题是它不是一个交互式shell,每个命令本身都是无状态的。你必须/可以尝试使用ssh2扩展,最后我知道它在windows上被破坏了。作为奖励,它还可以使用
AES
SSL
(比如制作自签名证书)还有很多其他加密的东西。我发现它唯一缺少的是没有PGP加密,但是没有真正易于使用的库。谢谢你的详细回复和链接。我设法让phpseclib工作起来了!我会更新我的答案,这样你就可以看到我所做的,没有问题,没有问题