You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用curl获取远程.sh脚本执行结果失败,返回脚本代码求助

解决方案

1. 启用Apache的CGI模块

先检查并启用CGI模块:

sudo a2enmod cgi
sudo systemctl restart apache2

2. 配置Apache识别shell脚本为可执行CGI

编辑CGI目录的配置文件(通常路径为/etc/apache2/conf-available/serve-cgi-bin.conf),在<Directory "/usr/lib/cgi-bin">段落中添加shell脚本的处理器规则:

<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Require all granted
    AddHandler cgi-script .sh .cgi  # 新增这行,让Apache识别.sh后缀的可执行脚本
</Directory>

修改后重启Apache:

sudo systemctl restart apache2

3. 给脚本添加执行权限

确保你的temp.sh有可执行权限:

sudo chmod +x /usr/lib/cgi-bin/temp.sh

4. 脚本开头必须添加shebang和CGI输出头

脚本第一行必须指定解释器,并且执行前要输出HTTP内容头,示例如下:

#!/bin/bash
# 先输出CGI要求的HTTP头
echo "Content-Type: text/plain"
echo ""
# 这里放你的CPU温度获取逻辑,比如
cat /sys/class/thermal/thermal_zone0/temp | awk '{print $1/1000 "°C"}'

完成以上步骤后,重新执行curl -s http://192.168.1.5/cgi-bin/temp.sh就能直接拿到温度数据,无需通过PHP中转。

内容的提问来源于stack exchange,提问作者BigBobD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 16:15:41