2

Currently trying to create a PHP web application to run a variety of Shell Scripts to handle website generation and cleanup on delete.

Here's a snippet from my deletePropertyProcess.php:

$ssh = new Net_SSH2($server);

if(!$ssh->login("username", "password")) {
  $result['result'] = 'ERROR';
  $result['message'] = 'Login failed to server ' . $server;
} else {
  $result = $ssh->exec("cd /var/www/sites ; sudo /usr/local/bin/delete_property_folder.sh -c $comp -p $prop");
  echo $result;
}

Where delete_property_folder.sh is a bash script on my remote server and -c $comp -p $prop are my script options/parameters.

The fun part is, all of this works flawlessly via Putty. Running this functionality from PHP via Ajax returns the exit status of the Shell Script and the anticipated output from Ajax, but none of the changes handled by my Shell Script are actually applied.

Like I said, this runs perfectly from Putty so I doubt the issue is the Shell Script but here it is anyways:

#!/bin/bash

function fix_file_permissions() {
    chown -Rf username:username $1/$2

    echo $?;
}

function delete_specified_folder() {
    rm -rf $1/$2

    echo $?;
}

##Main Script Body
POSITIONAL=()
while [[ $# -gt 0 ]]
do
    key="$1"

    case $key in
        -c|--company)
            COMPANY="$2"
            shift
            shift
        ;;
        -p|--property)
            PROPERTY="$2"
            shift
            shift
        ;;
        *) #unknown option
            POSITIONAL+=("$1") #save it in an array for later
            shift
        ;;
    esac
done
set -- "${POSITIONAL[@]}" #restore positional parameters

PERMISSIONS_FIXED=$(fix_file_permissions $COMPANY $PROPERTY)
if [ $PERMISSIONS_FIXED -eq 0 ]; then
    FOLDER_DELETED=$(delete_specified_folder $COMPANY $PROPERTY)
    echo $FOLDER_DELETED
    exit
fi
echo 1

At this point, any recommendations would be useful. Let me know if you need any more information.

Marc Bosse
  • 336
  • 1
  • 3
  • 17
  • 1
    I've managed to get it working by feeding the sudo password via --STDIN using the following: `$result = $ssh->exec("cd /var/www/sites ; echo password | sudo -S /usr/local/bin/delete_property_folder.sh -c $comp -p $prop");` – Marc Bosse Oct 19 '17 at 17:29
  • You don't say how your code is failing or what happens when you run it. What actually happens? Do you get any error messages? – Kenster Oct 19 '17 at 17:46
  • For this example, if you look at the Bash script, you'll see it's just echoing a value. Plain and simple. "The fun part is, all of this works flawlessly via Putty. Running this functionality from PHP via Ajax returns the exit status of the Shell Script and the anticipated output from Ajax, but none of the changes handled by my Shell Script are actually applied." Meaning the correct output value is returned by my shell script (i.e. 0) but it's not affecting any changes on the remote server. It's all there... – Marc Bosse Oct 19 '17 at 19:51
  • 1
    Can someone explain how this is 'off-topic' for Stack Overflow?! Which site should I use so I don't waste my time like this? – Marc Bosse Oct 19 '17 at 19:56
  • @MarcBosse - FWIW I've voted to reopen the question. Anyway, I think your first comment is on the money. The problem is probably the sudo. sudo usually prompts for a password and that prompt requires a PTY, at minimum. `$ssh->enablePTY()` would give you a PTY for use with `$ssh->exec()` or you could do an interactive shell. http://phpseclib.sourceforge.net/ssh/examples.html#interactive provides an example of doing sudo without the STDIN approach you're using. – neubert Oct 20 '17 at 03:15

1 Answers1

0

I've managed to get it working by feeding the sudo password via --STDIN to the Shell Script's full path using the following:

$result = $ssh->exec("cd /var/www/sites; echo 'password' | sudo -S /usr/local/bin/delete_property_folder.sh -c $comp -p $prop");

echo-ing your password with sudo -S allows you to pass your password via the pipe into the sudo shell script execution.

Marc Bosse
  • 336
  • 1
  • 3
  • 17
  • Problem with this is sometimes the output of the script contains the text from the sudo prompt... Not sure how to suppress this. Only happens on some scripts, oddly enough. – Marc Bosse Dec 20 '17 at 16:37