Execute a shell command through php and display it in browser? - php

I would like to execute a shell command through php and display it in a browser. Is there anyway to do so?
here is my php code : [test.php]
<?php
$number=$_GET["num"];
$date=$_GET["date"];
$output = shell_exec('egrep -w '2012-09-01|974' /home/myquery_test/log/push.log');
echo "<pre>$output</pre>";
?>
When I run this(test.php) file from browser nothing shows up. But when i change the
$output = shell_exec('ls')
its working fine!! Why isn't the egrep/grep command not working??

The egrep command isn't working, because you're using single quotes as a string constant delimiter: 'egreep -w' <==> 2012-09-01|974' <==> /home/myquery_test/log/push.log' <==Just use double quotes in the string, or as string delimiters OR escape the quotes.
shell_exec('egrep -w \'2012-09-01|974\' /home/myquery_test/log/push.log');
shell_exec('egrep -w "2012-09-01|974" /home/myquery_test/log/push.log');
shell_exec("egrep -w '2012-09-01|974' /home/myquery_test/log/push.log");
And, to avoid not getting the warnings and errors that would have brought this issue to light when testing, set your ini to E_STRICT|E_ALL, and fix the warnings, rather then ignoring them. [teasingly: after you're done with that, you might want to consider accepting some answers]I see you've accepted a lot while I was typing this post up :)
Using variables in your command:
$output = shell_exec("tail -f | egrep '$number.*$date' /var/www/myquery_test/log/push.log");
$output = shell_exec('tail -f | egrep "'.$number.'.*'.$date.'" /var/www/myquery_test/log/push.log');
$output = shell_exec("tail -f | egrep \"$number.*$date\" /var/www/myquery_test/log/push.log");

Related

How can i implement escapeshellarg inside a URL?

The user supplies two variables via a HTML form called username and name.
They are used to execute a shell command which is currently unsafe.
I have the following PHP code:
(exec("cat /opt/application/userdata/$username/following | grep -w $name"))
I am trying to implement escapeshellarg, but can't get it working by following the official PHP documentation, i have tried:
(exec("cat /opt/application/userdata/ .escapeshellarg($username)/following | grep -w .escapeshellarg($name)"))
But this is not working and i think its a syntax error.
How do i format this properly?
What's happening is that you are currently trying to run a function inside of a string. This is possible with extra steps, but is not desirable.
What you want to do is concatenate the string with the output of the function.
You can inline that in this manner:
exec('cat /opt/application/userdata/' . escapeshellarg($username) . '/following | grep -w ' . escapeshellarg($name))
(noticed I used single quotes ['], as no expansion is happening within the string, this is somewhat faster and keeps it separate)
Or you can perform the operation earlier, and simply include ("expand") the variables in the string, like your first example hints at:
$username = escapeshellarg($username);
$name = escapeshellarg($name);
exec("cat /opt/application/userdata/$username/following | grep -w $name")
(noticed I used double quotes ["] as there is expansion happening within the string)
The escapeshellarg() function is a PHP built-in, but you're attempting to execute it as a shell function. You can fix it by simply pulling the function out of the string scope and concatenating the results:
exec("cat /opt/application/userdata/" . escapeshellarg($username) . "/following | grep -w " . escapeshellarg($name));

Using variable in php passthru

I would like to put my variables inside my passthru,
$cut1 =DNAseq1
$cut2 = DNAseq2
I have already checked this topic but didn't find how to proceed
echo passthru('sudo docker run my_docker bash -c "-check "' .$cut1.'" "'.$cut2);
but nothing is displayed on the screen but when I wrote directly the DNAseq1:
echo passthru('sudo docker run my_docker bash -c "-check ATCG "'.$cut );
It works....but only when I wrote one variable not for 2 So what is the problem ??
Try this:
echo passthru("sudo docker run my_docker bash -c '/home/prg/soap -check $cut1 $cut2'");
I've used double quotes around the argument to passthru() so I can substitute variables more clearly.

How to store value in variable having special character and linux command in php

I want to use below in php:
$cmd = '<'.$filePath.' tail -f +'.$lineNumber.' | grep "tracker.php" | cut -d" " -f1-8';
$data = shell_exec($cmd);
return $data;
but there is something wrong in storing value of $cmd variable.
If you simply echo value of cmd variable then also it is getting printed.
Someone faced this type of issue before?

GetOpt doesn't read full URL

I have a script that need to run from a terminal or a command prompt. I'm using PHP. GetOpt is the function that I use to get data or a parameter that the user input in the terminal.
This is my script.
<?php
$opt = getopt("f:");
$input = $opt['f'];
$u = fopen($input, 'r');
echo "\n\n$input\n\n";
I tried to run it like this:
$ php myscript.php -f http://myurl.com/file.csv?city=london&status=3
My url is http://myurl.com/file.csv?city=london&status=3, but it only outputs http://myurl.com/file.csv?city=london. The status parameter is lost from the full URL.
How can I get this to work?
it's because you have to wrap your link around into quotes:
$ php myscript.php -f "http://myurl.com/file.csv?city=london&status=3"
I'll go ahead and assume you are running your script in Bash, and & in Bash might be interpreted as bitwise AND in your case:
$ echo $(( 98 & 7 ))
2

how to execute unix command in php

i am trying to execute unix command in php script like this.
<?php
echo shell_exec('head -n 1 log_list_23072014|awk -F ',' '{print $2}'');
?>
This is the file , trying to get the first column of the first row.
NODE,CGR,TERMID,VMGW,ET
but the error message i am getting
Parse error: syntax error, unexpected 'shell_exec' (T_STRING), expecting ',' or ';'.
cant able to find please help.
The string you've used is not valid, you have to escape single quotes inside your string:
<?php echo shell_exec('head -n 1 log_list_23072014|awk -F \',\' \'{print $2}\'');
You can also use exec()
There was an extra ' within your command. Using your command in a variable can help with identifying errors, and when using the standard exec it's required.
$cmd = 'head -n 1 log_list_23072014 | awk -F , \'{print $2}\'';
echo shell_exec($cmd);
changing it to the format above should work.

Categories