简体   繁体   中英

Bash script containing clogin not working when executed via 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM