I have a bash script that will log on to remote device and copy a file. It works perfectly from the command line either if logged in as the user (support) or by running\
sudo -u support -i '/opt/IIS/getScreenShot' <parameter>
The support user has keys set up on every device so that the password is not asked on the scp command in the getScreenShot
bash script.
I now need for this script to be able to be called/run by Apache. I have a PHP script that executes the following:
$output = shell_exec("sudo -u support -i '$command' $serial");
where $command
is /opt/IIS/getScreenShot
.
The command will start running (outputs back to the browser). Commands such as if [ ! -d /tmp/$1 ];
or mkdir -p /tmp/$1
do not seem to execute inside of the bash script if it is called through the Apache server and I am not sure why.
I would have thought the sudo command would have run it as the support user just as it did from the command line but it isn't. Ultimately I need for /opt/IIS/getScreenShot
to be able to run from the command line and from the Apache call. Any suggestions?
Edit:
sudoers entry:
apache ALL=(ALL) NOPASSWD: ALL
/opt/IIS/getScreenShot will run as echo statements will show in the browser. Just certain commands in the script will not though. getScreenShot is:
set -e
#set -x
#Check runnable
if [ $# -lt 1 ] ; then #Check there is enough command line parameters.
echo "Usage: $0 [<Serial#> | <Socket#>] "
echo " Example: $0 GESC637005W1 "
exit 1
fi
_socket=$(/opt/Allure/socketdata "$1" |awk -F '[(/: ]'+ '{print $10}')
#echo "socket $_socket"
if [ $_socket = "400" ];
then
echo "400 Serial number not found"
exit 400
fi
echo "Checking for dir /tmp/$1"
if [ ! -d /tmp/$1 ]; then
echo "mkdir -p /tmp/$1"
mkdir -p /tmp/$1;
fi
echo "test"
echo "scp -P$_socket support@localhost:/srv/samba/share/clarity-client/client-apps/digital-poster/screens/screenshot.png /tmp/$1/screenshot.png"
scp -P$_socket support@localhost:/srv/samba/share/clarity-client/client-apps/digital-poster/screens/screenshot.png /tmp/$1/screenshot.png
return=$?
if [ $return -ne 0 ];
then
echo "401 scp1 failed $return"
exit 401
fi
echo "test2"
scp /tmp/$1/screenshot.png support@172.24.16.37:/opt/digital_media/dm_content/screenshots/$1.png
return=$?
if [ $return -ne 0 ];
then
echo "402 scp2 failed $return"
fi
A quick update to clarify my actual question. The script will actually run and the parameter gets passed to it correctly. The problem is that certain of the 'commands' in the script will not run correctly and it seems mostly tied to the test/creating of the directory. If the directory does not exist, the 'test' for it does not work. Separately, if I remove the test but issue the mkdir command directly (after making sure the directory is not there - or deleting it if it is), mkdir shows the error : mkdir: cannot create directory '/tmp/GESC637005UH': File exists. I have checked the directory directly and it isn't there. I have run the locate command on the system and can't find it so I am not sure why the mkdir command thinks it is there - and might also explain why the 'test' seems to 'fail' (i.e. it thinks it exists).
" echo "" #echo "mkdir -p /tmp/$1" mkdir -p /tmp/"$1"; fi – Kent Scott Jan 05 '18 at 16:48
" – Kent Scott Jan 05 '18 at 16:51