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;
}
delete_property_folder.sh
-c $comp -p $prop
#!/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
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.