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

时间:2014-12-04 19:47:57

标签: linux apache bash cgi

必填信息: 我在家用电脑网络服务器,Fedora 20和yum repo的httpd上运行它。 我从http://www.instructables.com/id/Simple-linux-commands-from-a-web-page/获取了我的.cgi文件的基本版本,并且一直在尝试根据我的需要修改它。

我的问题 - 我可以非常轻松地使cgi脚本运行uname,lspci,ls,以及/ usr / bin文件夹中只显示文本的任何脚本。 但是,我不能运行一个实际上有某些东西的脚本。例如,我无法让cgi运行“rhythmbox-client --next”来跳过Rhythmbox媒体播放器上的一首歌。

我的意图:我为我的朋友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,同样的问题。

要执行命令的脚本部分如下:     回声“”     回应“精英音乐机器人控制面板”     PATH = “/ bin中:在/ usr / bin中:在/ usr / UCB:在/ usr /选择/斌”     export $ 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它什么都不做。

请帮忙。

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

编辑:为Banshee运行'next'脚本作为apache用户..

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 

1 个答案:

答案 0 :(得分:0)

问题源于rhythmbox-client期望连接到当前用户的dbus会话以收集rhythmbox信息/问题命令。 (感谢Etan Reisner指出这一点)

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

文件1:留在所需的文件夹中,添加到开始项目,制作可执行文件(chmod + x)名称&#34; Get.DBus.sh&#34;

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

文件2:制作可执行文件,移至/ usr / bin,制作可执行文件(chmod + x)名称&#34; Run.DBus.sh&#34;

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

从..

更改CGI中所需命令的执行命令
command)
  /bin/command
;;

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

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

感谢大家的帮助,尽管这是一个DBus问题XD