【问题标题】:PHP exec() TRACERT.exe how to pass desired arguments to Windows commandPHP exec() TRACERT.exe 如何将所需的参数传递给 Windows 命令
【发布时间】:2014-02-21 23:06:46
【问题描述】:

我想用 PHP exec() 在 Windows 机器上远程执行 tracert。我有:

<?php
    echo exec("C:\\Windows\\System32\\TRACERT.exe");
    echo "<br/>Success!";
?>

这不会给我错误,它会打印“成功!”。 但是如何将参数(例如 IP 地址)传递给 tracert.exe 并将结果打印到变量或数组中? 我不知道传递看起来像这样的参数的语法: tracert 等。

【问题讨论】:

    标签: php windows cmd exec traceroute


    【解决方案1】:

    我更喜欢 passthru(),因为 traceroute 输出可以在浏览器中即时查看,而不必等到完成。

    $IP=$_REQUEST['IP'];
    set_time_limit(120);
    echo "<h1>Traceroute $IP</h1><pre>";
    passthru("tracert.exe -h 8 $IP");
    

    【讨论】:

      【解决方案2】:

      默认exec将只返回执行命令的最后一行。

      您应该使用shell_exec,如下所示:

      <?php
          $result = shell_exec("C:\\Windows\\System32\\TRACERT.exe www.google.com");
          print $result;
          echo "<br/>Success!";
      ?>
      

      【讨论】:

      • 因此,要将其分配给变量,可以执行 $output = shell_exec()
      • 我收到一个超时错误“致命错误:超过 30 秒的最大执行时间”我之前没有收到。我得到“-6 Force using IPv6”。在这之前显然是 tracert.exe 执行和等待参数的一部分。
      • tracert 命令似乎花费了 30 多秒。因此,您可以增加 PHP 中的时间限制(使用set_time_limit(300))或使用tracert 中的-h 参数来限制跃点数(tracert -h www.google.com)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2021-03-31
      相关资源
      最近更新 更多