隐藏ping命令

时间:2015-09-29 16:52:43

标签: php windows windows-xp ping

我目前正在尝试通过PHP在Windows XP中ping主机。目前,我能够这样做,但是当我尝试从我的机​​器上ping主机时,“帮助”会一直显示,所以我想知道是否有办法隐藏“帮助”菜单。

这是我能够在XP上运行的代码

<br><input type='text' name='ip2'>
          <input type='submit' value='ping'>
          <?php

          $ip= "";
          $status="";
          $output="";

          function test_input($data){

            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
          }

          if($_SERVER["REQUEST_METHOD"] =="POST"){
            if(empty($_POST["ip2"])){
                echo "IP must not be empty!";
            } else{

                $ip=test_input($_POST["ip2"]);

            }
          }
            $output=shell_exec('ping ' .$ip. '');
            echo"<pre>$output</pre>";


          ?>

提前致谢!

1 个答案:

答案 0 :(得分:0)

没有办法压制帮助。除非命令在没有参数的情况下运行,否则在运行命令时不应显示它。更多:https://technet.microsoft.com/en-us/library/cc737478%28v=ws.10%29.aspx

这是运行你正在做的事情的一种方法:

<br />
<input type='text' name='ip2' />
<input type='submit' value='ping' />
<?php
$ip= "";
$status="";
$output="";

function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if(!isset($_POST['ip2'])){
    echo "IP must not be empty!";
} else {
    if(empty($_POST["ip2"])){
        echo "IP must not be empty!";
    } else{
        $ip=test_input($_POST["ip2"]);
        $output=shell_exec("ping $ip\r\n");
        echo"<pre>$output</pre>";
    }
}
?>

这可确保在执行ping之前满足所有条件。

如果仍然执行并显示帮助文字,我们可以查看字词和行(使用explode()strpos())并查看Usage:中是否存在$output 。那看起来像是:

$output=shell_exec('ping ' .$ip. '');
if(stripos($output, "usage:") === false){
    echo "<pre>$output</pre>";
} else {
    $lines = explode("\r\n", output); // Explode output by end of line
    echo "<pre>";
    $suppress = true;
    for($i=0;$i<count($lines);$i++){
        if(stripos($lines[$i], "pinging") === true){
            // 1st line of ping response: "Pinging ..."
            $supress = false;
        }
        if(!$supress){
            echo $lines[$i] . "\r\n";
        }
    }
    echo "</pre>\r\n";
}
相关问题