0

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.

2 Answers2

0

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!";
?>
Razvan
  • 2,436
  • 18
  • 23
  • so, to assign it to a variable, could do $output = shell_exec() – SyntaxLAMP Feb 21 '14 at 23:14
  • I am getting a timeout error "Fatal error: Maximum execution time of 30 seconds exceeded" I did not get before. I got "-6 Force using IPv6." before which clearly is part of tracert.exe executing and waiting for an argument. –  Feb 21 '14 at 23:22
  • It seems that the `tracert` command is taking more than 30 seconds. So you could either increase the time limit in PHP (with `set_time_limit(300)`) or use the `-h` parameter in `tracert` to limit the number of hops (`tracert -h www.google.com`). – Razvan Feb 21 '14 at 23:36
0

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");
vitsoft
  • 5,515
  • 1
  • 18
  • 31