dongnao1908 2012-12-03 08:43
浏览 32
已采纳

包含clogin的Bash脚本在通过php执行时无效

I created a simple PHP site we would like to use to disable/enable ports on a cisco switch. I am passing over two arguments (port and action) to a bash script via the php function exec(). For debugging I set those arguments at the end inside the bash script.

The important part of the php function I am calling looks like this:

function PortAction($port, $action){
    echo "<hr /> Port:".$port."<br />Action:".$action;
    chdir('/usr/local/icinga-1.7.0/videowand/');
    echo exec('sh /usr/local/icinga-1.7.0/videowand/action2.sh 2>&1',$output, $return);
    echo "Return / Output: ".$return;
    print_r($output);    
}

The bash script "action2.sh":

#!/bin/bash
/usr/libexec/rancid/clogin -c "conf t; int Fa0/1; shutdown" 192.168.6.6

If i execute the bash script manually (or even the directly the clogin command) as apache user, it all works as expected, the port gets disabled. But as soon as I call it over the PHP function, I get the output:

Array ( [0] => no such variable [1] => (read trace on "env(HOME)") [2] => invoked from within [3] => "set password_file $env(HOME)/.cloginrc" [4] => (file "/usr/libexec/rancid/clogin" line 66)

And nothing gets done. Which indicates some problem with the password entry (as far as I can see). Permissions on this file(s) are correct, I guess they have to otherwise it wouldn't work directly inside the shell. Even tried to change the .cloginrc path using the -f switch, no success. PHP Safe-mode: Off Display errors: On

The script takes a while because a telnet connection needs to be opened. This is why I put this into a separate bash script.

  • 写回答

1条回答 默认 最新

  • dongshuql24533 2012-12-03 08:45
    关注

    Provide the full path to the sh binary, like this:

    echo exec('/bin/sh /usr/local/icinga-1.7.0/videowand/action2.sh 2>&1',$output, $return);
    

    I'm assuming /bin/sh above, as that is commonly the standard path for sh, but make sure with
    which sh command.

    /bin/sh is a soft link to your shell, which usually is /bin/bash, I'm not sure exec() can work with soft links of binaries, so if above doesn't works then just use /bin/bash instead of /bin/sh

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题