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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog