Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.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 在远程服务器上访问shell脚本_Php_Linux_Bash_Raspberry Pi_Shell Exec - Fatal编程技术网

Php 在远程服务器上访问shell脚本

Php 在远程服务器上访问shell脚本,php,linux,bash,raspberry-pi,shell-exec,Php,Linux,Bash,Raspberry Pi,Shell Exec,首先,我有两台服务器: 覆盆子皮 Linux Debian服务器 pi有一个显示房间当前温度的脚本 以下是脚本: #!/bin/bash i2cset -y 1 0x48 0xEE dte=$(date +%Y-%m-%d) tme=$(date +%H:%M:%S) hexraw=$(i2cget -y 1 0x48 0xAA w) msb=$(echo ${hexraw:4:2}) lsb=$(echo ${hexraw:2:1}) dec=$(printf "%d\n" "0x$msb$l

首先,我有两台服务器:

  • 覆盆子皮
  • Linux Debian服务器
  • pi有一个显示房间当前温度的脚本

    以下是脚本:

    #!/bin/bash
    i2cset -y 1 0x48 0xEE
    dte=$(date +%Y-%m-%d)
    tme=$(date +%H:%M:%S)
    hexraw=$(i2cget -y 1 0x48 0xAA w)
    msb=$(echo ${hexraw:4:2})
    lsb=$(echo ${hexraw:2:1})
    dec=$(printf "%d\n" "0x$msb$lsb")
    temp=$(echo "scale=1; $dec/16" | bc)
    
    echo $temp
    exit
    
    在pi的网站上显示温度效果很好。 但是我在linux服务器上有另一个脚本,它接受pi脚本的值:

    #!/bin/bash
    x=$(ssh pi@172.16.248.210 sudo /home/pi/CurrTemp;)
    echo $x
    
    这也行得通。但是当我想在我的Linux服务器网站上显示Temp时(使用shell_exec()),它不起作用。var_转储为空

    PHP:

    我试着用“sudo”来写


    我已将脚本所有者更改为root。

    ssh
    身份验证不起作用,因为web服务器无法(也应该)访问您的ssh密钥

    您必须选择(后者肯定是首选!):

    • 在Debian服务器上为web服务器创建另一个ssh密钥,并将其添加到PI上的
      allowed\u keys
      。您可以将密钥限制为仅允许该命令

    • 在PI上创建一个小web服务,并以纯文本、xml、json或其他形式输出温度。使用
      file\u get\u contents()
      通过HTTP获取文件,如使用纯文本文件:

    raspberry pi上的temperature.php:

    <?php
    
    echo shell_exec('/home/pi/CurrTemp');
    

    对不起,我忘了提到我已经在两边创建了RSA密钥。你的意思是在json文件中写入我的“pi”脚本的输出吗?不,你应该通过HTTP输出值。只需
    echo
    。在第一次测试中使用纯文本。那么你的意思是我应该忽略linux服务器上的一个脚本,并将currTemp的输出放在HTTP中?如果是,它是如何工作的?在pi的网站上,我使用.htaccess,这会阻止通信吗?为什么不测试一下?我无法回答这个问题,因为我无法访问你的pi。
    <?php
    
    $temperature = file_get_contents('http://172.16.248.210/temperature.php');
    
    echo $temperature;