Pass php variable to exec - php

I need to get dimm info using ipmitool as follows:
exec("/usr/bin/ipmitool -I lan -H $spip -U root -P '$thepassword' sunoem cli 'show System/Memory/DIMMs/$a' | grep -i location", $dimm_loc, $ipmiretval);
$a is previously defined as:
$a=$dimm[$i]
The return value for the above exec command is 1. If I replace $a with its vaule, i.e
exec("/usr/bin/ipmitool -I lan -H $spip -U root -P '$thepassword' sunoem cli 'show System/Memory/DIMMs/D5' | grep -i location", $dimm_loc, $ipmiretval);
The exec command executes as expected. So it looks like $a cannot be used in the above example. How else can I pass the variable to the exec command?
Thanks!!

Run var_dump($a) and see what actualy it contains. Also dump the executed command as a string to see if command is formated properly.

One more tip. Instead of doing an exec("some very long command line"), put your command line in a variable, then both LOG and exec() the variable. For example:
$fmt="/usr/bin/ipmitool -I lan -H %s -U root -P '%s' sunoem cli 'show System/Memory/DIMMs/%s' | grep -i location";
$cmd=sprintf($fmt, $spip, $thepassword, $a);
exec($cmd, $dimm_loc, $ipmiretval);
syslog(LOG_DEBUG, "Running: $cmd");
if ($ipmiretval > 0) {
syslog(LOG_ERR, "exec FAILED: $cmd");
} else {
syslog(LOG_DEBUG, "exec: $cmd");
}

Related

PsExec from PHP does not run me

Executing the PsExce command from the shell_exce or exce does not generate any action on the machine that sent the command
My php code is
$execute = "C:\\pstools\\PsExec.exe \\\\".$ip." -u ".$user." -p xxxxxxxx -i -h -d C:\\app\\ejecutar.bat 1";
exec($execute,$out);
print_r($out);
The message that comes back to me is this
If that command is executed from the console if you run it on the remote machine

Can't run Linux "awk" command in script from PHP

I have the shell script "test.sh":
#!/system/bin/sh
PID=$(ps | grep logcat | grep root |grep -v grep | awk '{print $2}')
echo "Using awk: $PID"
PID=$(ps | grep logcat | grep root |grep -v grep | cut -d " " -f 7 )
echo "Using cut: $PID"
When I run the script from PHP:
exec("su -c sh /path/to/my/script/test.sh");
I got this output:
Using awk:
Using cut: 6512
So "cut" command is work but "awk" command doesn't when I run the script from PHP, but when I run it from terminal:
# sh test.sh
I can get both awk and cut work fine! This how look like the output of "ps":
USER PID PPID VSIZE RSS WCHAN PC NAME
root 6512 5115 3044 1108 poll_sched b6e4bb0c S logcat
Do I missed something?
You should learn how to debug first
You said
So "cut" command is work but "awk" command doesn't when I run the
script from PHP, but when I run it from terminal:
I wonder how ?
actually throws error like below, in CLI
$ php -r 'exec("su -c sh /path/to/my/script/test.sh");'
su: user /path/to/my/script/test.sh does not exist
You first need below syntax while debugging code
// basic : stdin (0) stdout (1) stderr (2)
exec('your_command 2>&1', $output, $return_status);
// to see the response from your command
// su: user /path/to/my/script/test.sh does not exist
print_r($output);
Remember :
su gives you root permissions but it does not change the PATH variable and current working directory.
The operating system assumes that, in the absence of a username, the
user wants to change to a root session, and thus the user is prompted
for the root password
[akshay#localhost Desktop]$ su
Password:
[root#localhost Desktop]# pwd
/home/akshay/Desktop
[root#localhost Desktop]# exit
exit
[akshay#localhost Desktop]$ su -
Password:
[root#localhost ~]# pwd
/root
Solution:
You should allow executing your script without password prompt ( don't use su use sudo )
To allow apache user to execute your script and some commands you may make entry like below in /etc/sudoers
# which awk => give you awk path
# same use in your script also, or else set path variable
www-data ALL=NOPASSWD: /path/to/my/script/test.sh, /bin/cut, /usr/bin/awk
So it becomes :
// assuming your script is executable
exec("sudo /path/to/my/script/test.sh 2>&1", $output);
print_r($output);

exec issue with netstat and lsof

I want to check if a certain tunnel exists from inside PHP using (any of these commands):
$(which lsof) -i -n | grep ssh
$(which netstat) -a | grep "localhost:ssh"
The issue is that when I run the commands in the shell everything is fine but from php running them like:
$reply = exec(CMD);
always return nothing.
Any ideas?
Thank you!
You could redirect stderr to stdout and get the $output and $return_var. To do that, change your exec() call like this:
exec('$(which lsof) -i -n | grep ssh 2>&1', $output, $return_var);
var_dump($return_var);
var_dump($output);
More info about exec here: http://php.net/manual/en/function.exec.php (have a look at $output and $return_var parameters).
I think the issue is more to do with how PHP interprets your command...
In this case (assuming instead of CMD you write same command you try in shell), it would try to:
$reply = exec($(which lsof) -i -n | grep ssh);
means it would try to substitute the bold part as a PHP variable, and try to execute the resultant string. As the output of "-i -n |grep ssh" is null, so you get nothing as a result.
I would suggest you to instead:
$lsof = exec(which lsof);
$reply = exec($lsof -i -n | grep ssh);

use "wine" with php's shell_exec

I am currently trying to use a "wine" command from my PHP-Script.
If i execute this:
$shell = shell_exec("/usr/bin/wine --version");
All is working fine and WINE version is displayed in $shell.
But, if i try to make it like this:
$run = shell_exec("/usr/bin/wine ".$workdir."/bin/tool.exe -m ".$workdir."/bin/std.maps -a ".$workdir."/bin/alias.file -n ".$workdir."/files/".$project_name."/upload/dump.bin -o ".$workdir."/files/".$project_name."/maps/definitions.list");
Which results in:
$run = shell_exec("/usr/bin/wine /var/www/html/bin/tool.exe -m /var/www/html/bin/std.maps -a /var/www/html/bin/alias.file -n /var/www/html/files/1-59374-94700/upload/dump.bin -o /var/www/html/files/1-59374-94700/maps/definitions.list 2>&1");
I get the following output:
wine: chdir to /.wine : No such file or directory
What i am doing wrong? If i enter the command above directly to the shell, all is working fine. If i do it without /usr/bin/ in front of wine, the output's are the same.
Br, Chris
I haven't try it myself but I think it is because wine is run as different user with different environment settings. Try create .wine directory inside /var/www and make www-data user as owner of this directory and make /var/www/.wine HOME directory.
$run = shell_exec("HOME=/var/www/.wine /usr/bin/wine ".$workdir."/bin/tool.exe -m ".$workdir."/bin/std.maps -a ".$workdir."/bin/alias.file -n ".$workdir."/files/".$project_name."/upload/dump.bin -o ".$workdir."/files/".$project_name."/maps/definitions.list");

exec() is not working

I am doing the conversion from youtube link to gif image ,but i faced some problem while executing exce() function.
echo $ret = exec("youtube-to-gif -u https://www.youtube.com/watch?v={$vidID} -b $start_second -d $different_second -s 480x? -o {$filePath}{$fileName} -f 10", $out, $err);
I am using exec() ,but its not returning any value .Here i am not getting why it is not working.
Thanks ,any suggestion will highly appreciate.
First, store your command in a variable and try echoing it and runnining in a terminal to see if it's valid at all:
$command = "youtube-to-gif -u https://www.youtube.com/watch?v={$vidID} -b $start_second -d $different_second -s 480x? -o {$filePath}{$fileName} -f 10";
echo $command . PHP_EOL;
echo $ret = exec($command, $out, $err) . PHP_EOL;
If it works fine when you run it manually, try full path to youtube-to-gif. Assuming you are running php on Linux, you should be able to do it with this command:
which youtube-to-gif
Now replace youtube-to-gif with the full path in $command.

Categories