在PHP中使用cmd ping IP

时间:2019-02-20 06:46:05

标签: php cmd

在Windows上

:我有这样的表格, 我以表格形式发送接收者IP,我需要使用cmd ping此IP。 提交此表单时,我需要打开cmd并键入ping $_POST['rcvip']

<form action="staff.php" method="post">
    receiver IP :
    <input type="text" name="rcvip" value="<?php echo $cust_info['ReceiverIP']; ?>">
    <button style="font-size: 13px" type="submit" name="host_rcv">PING</button>
</form>
<?php if($_POST['host_rcv']){
    /// what should i do to open cmd and ping my ip
    }
?>

3 个答案:

答案 0 :(得分:1)

按如下所示尝试exec-

    <?php

if(isset($_POST['host_rcv'])){
    $ip = $_POST['rcvip'];
    exec("ping -n 3 $ip", $output, $status);
    echo '<pre>';
    print_r($output);
}

?>

<form action="" method="post">
    receiver IP :
    <input type="text" name="rcvip" value="122.163.4.237">
    <button style="font-size: 13px" type="submit" name="host_rcv">PING</button>
</form>

详细了解它 here

答案 1 :(得分:0)

在Linux上,您可以使用shell_exec函数:

http://php.net/manual/ro/function.shell-exec.php

在Windows上,您可以使用:

function _exec($cmd) 
{ 
   $WshShell = new COM("WScript.Shell"); 
   $cwd = getcwd(); 
   if (strpos($cwd,' ')) 
   {  if ($pos = strpos($cmd, ' ')) 
      {  $cmd = substr($cmd, 0, $pos) . '" ' . substr($cmd, $pos); 
      } 
      else 
      {  $cmd .= '"'; 
      } 
      $cwd = '"' . $cwd; 
   }   
   $oExec = $WshShell->Run("cmd /C \" $cwd\\$cmd\"", 0,true); 

   return $oExec == 0 ? true : false; 
} 

或者甚至使用某种方式来存储您的日志:

<?php
define ('EXEC_TMP_DIR', 'C:\tmp');

function windExec($cmd,$mode=''){
    // runs a command line and returns
    // the output even for Wind XP SP2
    // example: $cmd = "fullpath.exe -arg1 -arg2"
    // $outputString = windExec($cmd, "FG");
    // OR windExec($cmd);
    // (no output since it runs in BG by default)
    // for output requires that EXEC_TMP_DIR be defined

    // Setup the command to run from "run"
    $cmdline = "cmd /C $cmd";

    // set-up the output and mode
    if ($mode=='FG'){
        $outputfile = EXEC_TMP_DIR . "\\" . time() . ".txt";
        $cmdline .= " > $outputfile";
        $m = true;
    }
    else $m = false;

    // Make a new instance of the COM object
    $WshShell = new COM("WScript.Shell");

    // Make the command window but dont show it.
    $oExec = $WshShell->Run($cmdline, 0, $m);

    if ($outputfile){
        // Read the tmp file.
        $retStr = file_get_contents($outputfile);
        // Delete the temp_file.
        unlink($outputfile);
    }
    else $retStr = "";

    return $retStr;
}

答案 2 :(得分:0)

<?php
// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
?>

<DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain">
        Domain name:
        <table>
          <input name="domainname" type="text" >
          <input type="submit" name="submitBtn" value="Ping domain">
        </table>
      </form>
<?php
    // Check whether the for was submitted
    if (isset($_POST['submitBtn'])){
        $domainbase = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
        $domainbase = str_replace("http://","",strtolower($domainbase));

        echo '<table>';

        $status = pingDomain($domainbase);
        if ($status != -1) echo "<tr><td>http://$domainbase is ALIVE ($status ms)</td><tr>";
        else  echo "<tr><td>http://$domainbase is DOWN</td><tr>";

         echo '</table>';
    }
?>
</body>
</html>

完整示例:https://tournasdimitrios1.wordpress.com/2010/10/15/check-your-server-status-a-basic-ping-with-php/