Executing Shell Script from PHP. Apache log Syntax Error: ")" unexpected - php

I can't find the error, the scripts runs fine from console, I thought it was PHP thing but couldn't fine anything.
Maybe permissions things? Exec things? I really don't know much, hope someone can help me. Thanks!
BASH Script (SIMPLY SENDS A JOB TO A PRINTER, but I put it enterely just in case):
#!/bin/bash
PBOX_DIR_TMP="/tmp"
DESDE=$1
HASTA=$2
FORMULARIO=$3
COLA=$4
FECHA=$(date +%F)
SPOOL="/spool.$$.txt"
ARCHIVOSALIDA="${PBOX_DIR_TMP}"/salida.$$.txt
RETURNCODE=0
echo "$DESDE"
echo "$HASTA"
echo "#PBSSFORM ${FORMULARIO}" > "${SPOOL}"
for ((i = ${DESDE};i <= ${HASTA};i++))
do
if [ $i > ${DESDE} ]
then
echo -e "\f${FECHA}" >> "${SPOOL}"
echo "${i}" >> "${SPOOL}"
else
echo "${FECHA}" >> "${SPOOL}"
echo "${i}" >> "${SPOOL}"
fi
done
cat "${SPOOL}" | pboxsvc ${PBOX_DIR_BIN}/pboxlib.bin AplicarHostForm > "${ARCHIVOSALIDA}"
lp -d "${COLA}" "${ARCHIVOSALIDA}"
RETURNCODE=$?
rm "${ARCHIVOSALIDA}"
rm "${SPOOL}"
if [ ${RETURNCODE} -eq 0 ]
then
exit 1
else
exit 0
fi
PHP call:
$cmd = "/printb/imprimirFormPlano.bin 1 2 FILE.PS Cola1";
Apache Log:
/printb/imprimirFormPlano.bin 1: Syntax error: ")" unexpected
sh: 2: not found.
The scripts works if I call it from shell like this: ./printb/imprimirFormPlano.bin 1 2 FILE.PS Cola1

It looks like your apache's default shell is sh rather than bash. Try changing your command to
$cmd = '/bin/bash /printb/imprimirFormPlano.bin 1 2 FILE.PS Cola1';

Related

Kubeclt exec issues with variable via php script

New to kubernetes and php, so I'm having some issues. Any and all help is greatly appreciated!
<?php
$postgres = 'kubectl get pods -n migrationnamespace | grep postgres | cut -d " " -f1 2>&1';
$postgres_pod = shell_exec($postgres);
echo $postgres_pod;
$list2 = 'kubectl exec -it -n migrationnamespace ' . $postgres_pod . ' -- psql -U postgres -c \'SELECT * FROM mywhales\'; 2>&1';
echo "<pre>";
echo shell_exec($list2);
echo "<pre>";
?>
results in error
postgres-7957478b7d-tmw6m
error: you must specify at least one command for the container
sh: line 1: --: command not found
When switching '.$postgres_pod.' for postgres-7957478b7d-tmw6m as below - it executes fully
$list2 = 'kubectl exec -it -n migrationnamespace postgres-7957478b7d-tmw6m -- psql -U postgres -c \'SELECT * FROM mywhales\';';
postgres-7957478b7d-tmw6m
whale
---------
16:117
......
561:539
(17 rows)
Thanks - Mike
There can sometimes be extra whitespace before or after strings, especially return characters which don't always show up when echoing the result.
Using trim($postgres_pod) will ensure they are removed.

AvantFAX repair bash scripting error

I am working on repairing an install of AvantFAX on my Elastix box, digging around I found a script that would repair a know flaw that would cause the site to come up blank. I did not write this script and here is the original http://sourceforge.net/p/avantfax/discussion/540878/thread/441bd3dc/
It is not working when I run it. I get the error:
[root#elastix includes]# bash scriptfilename.sh
File "classes.php" edit.
scriptfilename.sh: line 4: syntax error near unexpected token `;'
scriptfilename.sh: line 4: `if ; then'
#
echo "File \"classes.php\" edit."
for i in $(ls ); do
if ; then
NAME=$i
i=${i:0:1}
LETTER=$(echo $i | sed -e 's///g')
if ; then
sed -i "/config.php/a\require_once \'$NAME\';" classes.php
sed -i "/config.php/a\require_once \'htmlMimeMail5.php\';" classes.php
fi
fi
done
echo "Function \"__autoload\" comment out."
sed -i "/function __autoload/c\/** function __autoload" classes.php
n=$(grep -niw 'function __autoload' classes.php|awk -F: '{ print $1
}');
n=$(($n+3));
sed -i "$(echo $n)i\ */" classes.php
--------------------------------------------------------------------------------
Using php5.3.7 on centos 6.0
Johann
There are several syntactic errors: The if ; then lines are not valid commands, because the condition is missing. A command returning a 0 or non 0 value at command finalization is missing in them. Which the condiciotn should be, I have not the slightest idea.
I have posted an article showing the fix for avantfax once you upgrade above php 5.3 here
this is the code:
#!/bin/bash
#
echo "File \"classes.php\" edit."
for i in $(ls ); do
if [ -f $i ]; then
NAME=$i
i=$
LETTER=$(echo $i | sed -e 's/[^A-Z]//g')
if [ ! -z $LETTER ]; then
sed -i "/config.php/a\require_once \'$NAME\';" classes.php
sed -i "/config.php/a\require_once \'htmlMimeMail5.php\';" classes.php
fi
fi
done
echo "Function \"__autoload\" comment out."
sed -i "/function __autoload/c\/** function __autoload" classes.php
n=$(grep -niw 'function __autoload' classes.php|awk -F: '{ print $1 }');
n=$(($n+3));
sed -i "$(echo $n)i\ */" classes.php

Can I call a rake task from PHP?

can I run a rake task from php?
I tried shell_exec but nothing happens.
exec('/bin/bash -l -c \'cd /Users/username/www/rails_app && [[ -s "/Users/username/.rvm/scripts/rvm" ]] && source "/Users/username/.rvm/scripts/rvm/.rvm/scripts/rvm" && rvm use 2.1.2 && RAILS_ENV=development /usr/bin/rake "ko:complete_order[2]" --silent\'', $out, $err);
The $out is an empty array.
The $err is 1.

PHP script to execute a bash script

I have 3 scripts (I have removed the help_page function from the networkstats.sh script when I pasted here to save some space):
api3.php
<?php
output = shell_exec('/bin/bash /usr/share/nginx/status/getnetworkstatsin.sh');
echo $output;
?>
getnetworkstatsin.sh
#!/bin/bash
ssh -i /tmp/id_rsa1 root#centos7clone bash -s -- -I < ./networkstats.sh
networkstats.sh
#!/bin/bash
interface=enp0s3
read -r inbytesold outbytesold < <(awk -v dev="^$interface:" '$1 ~ dev {
sub(/[^:]*:/,""); print $1, $9; exit }' /proc/net/dev)
sleep 1
read -r inbytesnew outbytesnew < <(awk -v dev="^$interface:" '$1 ~ dev {
sub(/[^:]*:/,""); print $1, $9; exit }' /proc/net/dev)
kilobitsin=$(( ( ( inbytesnew - inbytesold ) * 8 ) / 1024 ))
kilobitsout=$(( ( ( outbytesnew - outbytesold ) * 8 ) / 1024 ))
show_outgoing() {
echo $kilobitsout
}
show_all() {
echo "kilobits in: $kilobitsin"
echo "kilobits out: $kilobitsout"
}
if [[ $# -eq 0 ]];
then
help_page
exit 1
fi
for arg in "$#"
do
case $arg in
-h|--help)
help_page
;;
-I)
show_incoming
;;
-O)
show_outgoing
;;
-A|--all)
show_all
;;
esac
done
The problem I have is that when I execute the api3.php script from console, it is able to execute and return a value.
However when I try and execute from a webpage it fails to return anything.
I believe it is not even executing when I load it via the webpage by navigating to localhost/api3.php. Can someone help, what is the reason behind this? I have added
nginx ALL=NOPASSWD: /usr/share/nginx/status/getnetworkstatsin.sh
To my visudo section, I have tried to change permissions of all files involved to 777 (temporally) without success.
EDIT: I should also mention that all these scripts are located inside /usr/share/nginx/status which nginx has access to.

How to mirror local MySQL database to online (realtime) MySQL?

I'm a noob, is there someone could help me with this?
I have a local SQL database using Xampp thru phpMyAdmin and i want to mirror it to online MySQL in my website.
When I'm making changes to my local database, I want my online database to be updated (Realtime) with the changes made.
Is this possible? Can I have a sample php code or whatever that can do this? Thanks!!
Many times ago I wrote some script for this problem. As said above - replication it's really good solution but in my case I could not use it. So.. if you need some like replication by master->slave maybe this script will be useffull :
dumps.sh :
while getopts "c:l:" opt; do
case $opt in
c)
if [ -r "$OPTARG" ]; then
source "$OPTARG"
else
echo "Unreadable config file \"$OPTARG\""
exit 1
fi
;;
l) LOG_FILE="$OPTARG"
if [ ! -f "$LOG_FILE" ]; then
`touch $LOG_FILE`
fi
;;
\?) echo "Invalid options. -$OPTARG. USE -c config_file"
exit 1
;;
:) "Option -$OPTARG requires an argument."
;;
esac
done
logIt()
{
date_now=`date '+%D %T'`
if [ $LOG_FILE != "" ]; then
echo "$date_now : $*" >> $LOG_FILE
else
echo "$date_now : $*"
fi
}
build_tables()
{
TAB=""
logIt $#
for table in $TABLES
do
TAB="$TAB ${1}${table}"
done
echo $TAB
}
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
RM="$(which rm)"
DEST="."
MBD="$DEST/mysql"
eval $RM -fv "$MBD/*"
FILE=""
[ ! -d $MBD ] && mkdir -p $MBD || :
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST
TAB=`build_tables $DB_PREFIX`
FILE="$MBD/$DB_NAME.sql";
($MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $DB_NAME $TAB 2>> $LOG_FILE) > $FILE
input_to_mysql()
{
###############################3
CP="$(which cp)"
SED="$(which sed)"
len=${#INSERT_DB_NAME[*]}
i=0
while [ $i -lt $len ]; do
NEW_FILE="$MBD/${INSERT_DB_NAME[$i]}.sql"
eval $CP $FILE $NEW_FILE
eval $SED -i "s/$DB_PREFIX/${INSERT_DB_PREFIX[$i]}/g" $NEW_FILE
eval $SED -i "s/^.\*!.*$//g" $NEW_FILE
let i++
done
i=0
while [ $i -lt $len ]; do
NAME="$MBD/${INSERT_DB_NAME[$i]}.sql"
if [ -e $NAME ]; then
$MYSQL -u${INSERT_DB_USER[$i]} -p${INSERT_DB_PASS[$i]} -h${INSERT_DB_HOST[$i]} ${INSERT_DB_NAME[$i]} < $NAME 2>> $LOG_FILE
#echo "$MYSQL -u${INSERT_DB_USER[$i]} -p${INSERT_DB_PASS[$i]} -h${INSERT_DB_HOST[$i]} ${INSERT_DB_NAME[$i]} < $NAME"
logIt "IMPORT TO ${INSERT_DB_NAME[$i]}"
else
logIt "File $NAME not exist";
fi
let i++
done
}
check_dump()
{
FILE_TMP_DUMP="$MBD/tmp_dump_${INSERT_DB_NAME[0]}.sql";
FILE_DIFF_RESS="$MBD/diff_res.diff"
tmp_tables=`build_tables ${INSERT_DB_PREFIX[0]}`
($MYSQLDUMP -u ${INSERT_DB_USER[0]} -h ${INSERT_DB_HOST[0]} -p${INSERT_DB_PASS[0]} ${INSERT_DB_NAME[0]} $tmp_tables 2>>$LOG_FILE) > $FILE_TMP_DUMP
DIFF="$(which diff)"
$DIFF $FILE $FILE_TMP_DUMP > FILE_DIFF_RESS
[ -s "$SMB/diff_res.diff" ];
SUCCESS=$?
eval $RM -f $FILE_TMP_DUMP $FILE_DIFF_RESS
return $SUCCESS
}
if check_dump; then
input_to_mysql
else
logIt "No need to dump"
fi
so run like dump.sh -c config -l log.file
Where config like this:
MyHOST="master_host"
MyUSER="master_user"
MyPASS="master_password"
DB_NAME="master_db_name"
DB_PREFIX="master_db_prefix_" # leave empty if you haven't table prefix
TABLES="table1 table2 table3" // list of tables - leave empty for all tables
###############################
INSERT_DB_NAME=(slave_dbname1 slave_dbname2)
INSERT_DB_HOST=(slave_host1 slave_host2)
INSERT_DB_USER=(slave_user1 slave_user2)
INSERT_DB_PASS=(slave_pass1 slave_pass2)
INSERT_DB_PREFIX=(slave_db_prefix1 slave_db_prefix1) //
I putted it to cron and all works fine for me.
Of course you can do all this manually ...
The word you are looking for, is Replication. It's not real time, but almost.

Categories