gpt4 book ai didi

linux - 通过 CGI 运行服务器端 Bash 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:03 28 4
gpt4 key购买 nike

所需信息:我在家用 PC 网络服务器 Fedora 20 上运行它,并使用来自 yum 存储库的 httpd。我从以下位置获取了 .cgi 文件的基本版本:http://www.instructables.com/id/Simple-linux-commands-from-a-web-page/并一直在尝试修改它以满足我的需要。

我的问题 - 我可以非常轻松地使 cgi 脚本运行 uname、lspci、ls 以及/usr/bin 文件夹中仅显示文本的任何脚本。然而,我不能运行一个实际上做某事的脚本。例如,我无法让 cgi 运行“rhythmbox-client --next”来在 Rhythmbox Media Player 上快进一首歌曲。

我的意图:我为我的 friend ventrilo 服务器运行一个“音乐机器人”,我想为他们提供(受密码保护的)方式从网络访问该机器人、更改歌曲、将歌曲添加到播放队列等。

我需要的功能(我无法使用这些功能)是:下一首歌曲(rhythmbox-client --next)上一首歌曲 (rhythmbox-client --previous)将歌曲添加到队列(rhythmbox-clinet --enqueue NAME)清除歌曲队列(rhythmbox-client --clear-queue显示当前播放的歌曲(rhythmbox-client --print-playing)显示计算机上的歌曲列表 (ls/home/user/Music | grep ".mp3")

尽我所能,我无法让这些事情发挥作用。我也尝试过使用 Banshee 而不是 Rhythmbox,也有同样的问题。

假设执行命令的脚本部分如下: echo “” echo“精英音乐机器人控制面板” PATH="/bin:/usr/bin:/usr/ucb:/usr/opt/bin" 导出$PATH echo “” 回显“”

# test if any parameters were passed
if [ $CMD ]
then
case "$CMD" in
BotInfo)
echo "<pre>"
/bin/BotInfo
echo "System Name:" `/bin/uname -n`
echo "</pre>"
;;

next)
echo "Skipped to the next song."
/bin/rhythmbox-client --next
;;

back)
echo "Repeating the previous song. <pre>"
/bin/rhythmbox-client --previous
echo "</pre>"
;;

playing)
echo "The current song playing is... <pre>"
rhythmbox-client --print-playing
echo "</pre>"
;;

queue)
echo "The requested song has been added to the queue. <pre>"
rhythembox-client --enqueue $QUEUE_SONG
echo "</pre>"
;;

rating)
echo "The rating of the current song has been set to $RATING. <pre>"
/bin/rhythmbox-client --set-rating $RATING
echo "</pre>"
;;
ls)
echo "<pre>"
list=`ls /home/USER/Music | grep ".mp3"`
echo "$list"
echo "</pre>"
;;
*)
echo "Unknown command $CMD<br>"
;;
esac
fi

第一个是一个简单的脚本,显示系统时间(日期)、cpuinfo、meminfo 等,它确实有效。然而其他人则不然。

我尝试将 bash 命令运行为

echo "$(command)"

对于打印播放,我尝试这样做:

playing=`rhythmbox-client --print-playing`
echo "$playing"

我还尝试创建一个脚本:

#!/bin/bash
echo "This command worked!"
rhythmbox-client --next

我将其命名为 rhynext,chmod +x 并移动到/usr/bin,当在终端中运行时它可以工作,当告诉 cgi 执行/usr/bin/rhynext 时它什么也不做。

请帮忙。

我足够聪明,可以对代码进行逆向工程,如果您可以简单地给我一个可以工作的代码段/示例,我很可能能够将其转换为我的用途。提前致谢。

编辑:当以 apache 用户身份运行 Banshee 的“下一个”脚本时..

sudo -u apache /usr/bin/next
[sudo] password for OMMITED:
Unhandled Exception: System.UnauthorizedAccessException: Access to the path "/usr/share/httpd/.config" is denied.
at System.IO.Directory.CreateDirectoriesInternal (System.String path) [0x00000] in <filename unknown>:0
at System.IO.Directory.CreateDirectory (System.String path) [0x00000] in <filename unknown>:0
at System.IO.DirectoryInfo.Create () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.IO.DirectoryInfo:Create ()
at System.IO.Directory.CreateDirectoriesInternal (System.String path) [0x00000] in <filename unknown>:0
at System.IO.Directory.CreateDirectory (System.String path) [0x00000] in <filename unknown>:0
at Hyena.Paths.InitializePaths () [0x00000] in <filename unknown>:0
at Hyena.Paths.set_ApplicationName (System.String value) [0x00000] in <filename unknown>:0
at Banshee.ServiceStack.Application.InitializePaths () [0x00000] in <filename unknown>:0
at Booter.Booter.Main () [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.UnauthorizedAccessException: Access to the path "/usr/share/httpd/.config" is denied.
at System.IO.Directory.CreateDirectoriesInternal (System.String path) [0x00000] in <filename unknown>:0
at System.IO.Directory.CreateDirectory (System.String path) [0x00000] in <filename unknown>:0
at System.IO.DirectoryInfo.Create () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.IO.DirectoryInfo:Create ()
at System.IO.Directory.CreateDirectoriesInternal (System.String path) [0x00000] in <filename unknown>:0
at System.IO.Directory.CreateDirectory (System.String path) [0x00000] in <filename unknown>:0
at Hyena.Paths.InitializePaths () [0x00000] in <filename unknown>:0
at Hyena.Paths.set_ApplicationName (System.String value) [0x00000] in <filename unknown>:0
at Banshee.ServiceStack.Application.InitializePaths () [0x00000] in <filename unknown>:0
at Booter.Booter.Main () [0x00000] in <filename unknown>:0

最佳答案

问题源于节奏盒客户端期望连接到当前用户的 dbus session 来收集节奏盒信息/发出命令。 (感谢 Etan Reisner 指出这一点)

经过一番网上挖掘,我找到了解决方案。创建 2 个文件,一个用于在登录时将当前用户的 dbus session 地址保存到临时文件中,另一个用于调用 dbus 地址并通过它路由 CGI 命令。

文件 1:保留在所需文件夹中,添加到启​​动项,使可执行文件 (chmod +x) 名称为“Get.DBus.sh”

#!/bin/bash
set | grep DBUS_SESSION_BUS_ADDRESS > ~/.DBUS_temp

文件2:制作可执行文件,移动到/usr/bin,制作可执行文件(chmod+x)名称“Run.DBus.sh”

#!/bin/bash
source /home/USER/.DBUS_temp
export DBUS_SESSION_BUS_ADDRESS
$*

将CGI中所需命令的执行命令从..更改为..

command)
/bin/command
;;

command)
/bin/RunDBus.sh /bin/command
;;

问题已解决。所有命令都按预期工作。

感谢大家的帮助,万万没想到这是 DBus 问题 XD

关于linux - 通过 CGI 运行服务器端 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27302731/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com