Question

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.

Was it helpful?

Solution

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");

OTHER TIPS

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!";
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top