Skip to content

Commit

Permalink
Fixed fail to ping IPv6 phpservermon#879 (phpservermon#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsl authored Apr 19, 2020
1 parent 1358929 commit b346cd9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
*.bak
__MACOSX/
.DS_Store
.buildpath
.settings/
17 changes: 14 additions & 3 deletions src/psm/Util/Server/Updater/StatusUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ protected function updatePing($max_runs, $run = 1)
}
$result = null;
// Execute ping
$txt = exec("ping -c " . $max_runs . " " . $this->server['ip'] . " 2>&1", $output);
$pingCommand = 'ping6';
$serverIp = $this->server['ip'];
if (filter_var($serverIp,FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false){
$pingCommand = 'ping';
}
$txt = exec($pingCommand . " -c " . $max_runs . " " . $serverIp . " 2>&1", $output);
// Non-greedy match on filler
$re1 = '.*?';
// Uninteresting: float
Expand All @@ -192,7 +197,9 @@ protected function updatePing($max_runs, $run = 1)
if (preg_match_all("/" . $re1 . $re2 . $re3 . $re4 . "/is", $txt, $matches)) {
$result = $matches[1][0];
}

if (substr($output[0],0,4) == 'PING' && strpos($output[count($output)-2],'packets transmitted')){
$result = 0;
}
if (!is_null($result)) {
$this->header = $output[0];
$status = true;
Expand Down Expand Up @@ -225,7 +232,11 @@ protected function updateService($max_runs, $run = 1)
// save response time
$starttime = microtime(true);

$fp = @fsockopen($this->server['ip'], $this->server['port'], $errno, $this->error, $timeout);
$serverIp = $this->server['ip'];
if (filter_var($serverIp,FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false){
$serverIp = "[$serverIp]";
}
$fp = @fsockopen($serverIp, $this->server['port'], $errno, $this->error, $timeout);

$status = ($fp === false) ? false : true;
$this->rtime = (microtime(true) - $starttime);
Expand Down

0 comments on commit b346cd9

Please sign in to comment.