简体   繁体   中英

Phpseclib SSH2 not executing Shell Scripts

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.

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.

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