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


Accessing shell script on remote server

所以首先我有两个服务器:

  1. 树莓派
  2. 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

在圆周率的网站上显示温度效果很好。但是,我在linux服务器上有另一个脚本,它接受pi脚本的值:

#!/bin/bash
x=$(ssh pi@172.16.248.210 sudo /home/pi/CurrTemp;)
echo $x

这也有效。但是,当我想在Linux服务器网站上显示Temp(使用shell_exec())时,它不起作用。var_dump为空。

PHP:

我试着用"sudo"来写。

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

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

你必须选择(后者绝对是首选!):

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

  • 在PI上创建一个小的web服务,并以纯文本、xml、json或其他形式输出温度。使用file_get_contents()通过HTTP获取文件,就像这样使用纯文本文件:

temperature.php在树莓pi:上

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

temperature.php在Debian:上

<?php
$temperature = file_get_contents('http://172.16.248.210/temperature.php');
echo $temperature;