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

时间:2021-01-22 14:43:27

So first of all I have 2 Servers:

首先我有两个服务器

  1. Raspberry pi
  2. 覆盆子π
  3. Linux Debian server
  4. Debian Linux服务器

The pi has a script that displays the current Temperature in a room.

pi有一个显示房间当前温度的脚本。

This is the script :

这是剧本:

#!/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

Displaying the temperature on the pi's website works fine. But then I have another script on the linux server that takes the value of the pi's script:

在pi的网站上显示温度可以正常工作。但是我在linux服务器上有另一个脚本,它取pi的脚本的值:

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

This works too. But when I want to display the Temp on my Linux server website(with shell_exec()) it doesn't work. The var_dump is empty.

这个作品。但是,当我想在我的Linux服务器网站(使用shell_exec()))上显示温度时,它就不能工作了。var_dump是空的。

PHP:

PHP:

I've tried to write it with a "sudo".

我试着用“sudo”来写。

I've changed the script owner to root.

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

1 个解决方案

#1


2  

ssh authentication does not work since the web server can't (and should) access your ssh keys.

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

You have to options (where the latter is definitely preferred!):

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

  • Create another ssh key for the web server on the Debian server and add to allowed_keys on the PI. You may restrict the key to allow only that command.

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

  • Create a little web service on the PI and output the temperature in plain text, xml, json or whatever. The use file_get_contents() to obtain the file over HTTP, like this using a plain text file:

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

temperature.php on raspberry pi:

温度。php覆盆子π:

<?php

echo shell_exec('/home/pi/CurrTemp');

temperature.php on Debian:

温度。php在Debian上:

<?php

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

echo $temperature;

#1


2  

ssh authentication does not work since the web server can't (and should) access your ssh keys.

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

You have to options (where the latter is definitely preferred!):

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

  • Create another ssh key for the web server on the Debian server and add to allowed_keys on the PI. You may restrict the key to allow only that command.

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

  • Create a little web service on the PI and output the temperature in plain text, xml, json or whatever. The use file_get_contents() to obtain the file over HTTP, like this using a plain text file:

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

temperature.php on raspberry pi:

温度。php覆盆子π:

<?php

echo shell_exec('/home/pi/CurrTemp');

temperature.php on Debian:

温度。php在Debian上:

<?php

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

echo $temperature;