PHP exec() TRACERT.exe how to pass desired arguments to Windows command - php

I want to remotely execute tracert in a Windows machine with PHP exec(). I have:
<?php
echo exec("C:\\Windows\\System32\\TRACERT.exe");
echo "<br/>Success!";
?>
This does not give me errors and it prints "Success!."
But how do I pass an argument (such as an IP address to tracert.exe and print the result in a variable or array?
I do not know the syntax to pass an argument that looks like: tracert , etc.

I prefer passthru() as the traceroute output can be watched in browser on the fly, not having to wait until completed.
$IP=$_REQUEST['IP'];
set_time_limit(120);
echo "<h1>Traceroute $IP</h1><pre>";
passthru("tracert.exe -h 8 $IP");

By default exec will return only the last line of the executed command.
You should use shell_exec as follows:
<?php
$result = shell_exec("C:\\Windows\\System32\\TRACERT.exe www.google.com");
print $result;
echo "<br/>Success!";
?>

Related

Calling a PHP script from within PHP

I have a PHP script that I'd like to run both via the browser and from the command line. When I run the script from the command line, it executes without a problem. However, when I call it via a function such as exec or passthru, it doesn't work. I'm not getting any output from the call and I see no errors in the logs. I'm very confused...
echo exec('php /usr/share/nginx/www/function.php arg1');
Any ideas?
The output is captured in a parameter variable as an array and not returned back, when you use exec(). So do something like this:
exec('php /usr/share/nginx/www/function.php arg1', $output);
print_r($output);
#David,
A few things to check.
Create a PHP Info page and see if exec or passthru is enabled. Hosts disable it for security.
Try:
<?php
$exe = exec('php /usr/share/nginx/www/function.php arg1');
var_dump($exe);
?>
You could even do:
<?php
if(function_exists('exec')) {
echo "exec is enabled";
}
?>

Trying to execute a C program from PHP and output to a web browser

I have a C program which has been compiled to an executable. I can run this program from my console. I am trying to get the output from this program through my web browser so I am using the exec command in PHP. My PHP script works fine when I execute it from the command line, but when I call it over a browser I get no input. Here is my PHP program
<?php
echo exec('/var/www/html/./readcard');
?>
The readcard program has 777 permissions. I am guess the issue is something to do with permissions?
You aren't capturing the output. The second argument to exec consists of an array to which the output lines will be placed.
<?php
$output=array();
$rv=0;
exec('/var/www/html/./readcard',$output,$rv);
if($rv!=0)
{
die("readcard failed, got a return value of $rv\n");
}
foreach($output as $line)
{
echo("<p>$line</p>\n");
}
?>
You probably just echo the return code of the script, which is zero. You can either redirect the output to a file and then serve that from php, or pipe the output stream directly back to php code.
Try
<?php
$output = array();
exec('/var/www/html/./readcard', &$output);
?>

how to hide system() output

i am working on windows XP . i can successfully run a system() command through my browser by calling a TCL script that automates a ssh session. I also return a value from the script. however my problem is that the script dumps the entire ssh session in the browser.
my php script looks like :
$lastline=system('"C:\tcl\bin\tclsh.exe" \path to file\filename.tcl '.$username.' '.$pass,$val);
filename.tcl:
spawn plink -ssh $user#$host
expect "password:"
send "$pass\r"
expect "\prompt:/->"
set $return_value [string compare /..string../ $expect_out(buffer)]
/...some code...this runs fine/
exit $return_value
everything runs fine and i get $return_value back correctly but the php file prints the result of the execution of the entire ssh session in my browser which looks like:
Using username "admin". admin#10.135.25.150's password: === /*some text*/ === \prompt:/->.../some text/
i want to prevent the system() function from printing this in my browser
i have used the shell_exec() function but it returns the entire ssh session result (which i have parsed in the tcl script and got a precise value to return to the php script)
is there a way i can do this without using shell_exec() but using system() instead
thanks in advance
The documentation for system() specifically says:
Execute an external program and display the output
On that page are listed alternatives. If you use the exec function instead, it will only execute the commands without displaying any output.
Example:
<?php
echo "Hello, ";
system("ls -l");
echo "world!\n";
?>
will display the output of system:
$ php -q foo.php
Hello, total 1
-rw-r--r-- 1 bar domain users 59 Jul 15 16:10 foo.php
world!
while using exec will not display any output:
<?php
echo "Hello, ";
exec("ls -l");
echo "world!\n";
?>
$ php -q foo.php
Hello, world!
use ob_start(); before and ob_clean(); after calling it
http://sandbox.phpcode.eu/g/850a3.php
<?php
ob_start();
echo '<pre>';
$last_line = system('ls');
ob_clean();
echo 'nothing returned!';
?>
In general if you want to prevent anything to output to the browser you can use ob_start() before your system() call and then ob_end_clean(). See http://php.net/manual/en/function.ob-start.php

issuing things to shell and forgetting about it in php

how do you issue commands to the shell and then forget it's output and such? for example:
<?php
echo `sleep 2;echo hi`;
echo "foo";
?>
the result for this is hifoo. i would want a result that gives me foohi. why? i want the command issued to the shell simply issued and forgotten, i am confused about why PHP will wait for the result. is such a result possible?
(the idea behind this is setting up the correct number of selenium grid RC instances programatically. currently, it will stop after the first process is opened)
From php.net exec()
If a program is started with this
function, in order for it to continue
running in the background, the output
of the program must be redirected to a
file or another output stream. Failing
to do so will cause PHP to hang until
the execution of the program ends.
The same applies for all shell commands.
It's like anything you do at the command prompt, unless you take measures to put the new command in the background, the shell (and PHP ) will block until the command exits. Try this:
<?php
echo `(sleep 2; echo hi) &`;
echo 'foo';
?>
Note the brackets around your command, without that, the & woulud apply only to the echo , and you'd still have the 2 second pause.

How to run a .cmd file from within PHP and display the results

I need to run a .cmd batch file from within a php script.
The PHP will be accessed via an authenticated session within a browser.
When I run the .cmd file from the desktop of the server, it spits out some output to cmd.exe.
I'd like to route this output back to the php page.
Is this doable?
Yes it is doable. You can use
exec("mycommand.cmd", &$outputArray);
and print the content of the array:
echo implode("\n", $outputArray);
look here for more info
$result = `whatever.cmd`;
print $result; // Prints the result of running "whatever.cmd"
I prefer to use popen for this kind of task. Especially for long-running commands, because you can fetch output line-by-line and send it to browser, so there is less chance of timeout. Here's an example:
$p = popen('script.cmd', 'r');
if ($p)
{
while (!feof($p))
echo gets($p); // get output line-by-line
pclose($p);
}
You can use shell_exec, or the backticks operator, to launch a command and get the output as a string.
If you want to pass parameters to that command, you should envisage using escapeshellargs to escape them before calling the command ; and you might take a look at escapeshellcmd too ^^
Use the php popen() function

Categories