-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
95 lines (89 loc) · 2.46 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
if(isset($_GET['ip']) && !$_GET['ip']==''){
$ip = $_GET['ip'];
} elseif(isset($_GET['ips']) && !$_GET['ips']==''){
$ip = $_GET['ips'];
} else {
echo "please set ?ip=[local lan ip] in the request";
exit;
}
$ips = array();
$ips = explode(",",$ip);
$returnarray = array();
foreach($ips as $thisip){
$result = Ping($thisip);
array_push($returnarray, $result);
}
header('Content-Type: application/json');
$returnarray = json_encode($returnarray);
echo $returnarray;
//echo ")]}',\n".$returnarray;
exit;
function Ping($ip) {
$pingurl = $ip;
$disallowed = array('http://', 'https://');
foreach($disallowed as $d) {
if(strpos($pingurl, $d) === 0) {
$thisip = strtok(str_replace($d, '', $pingurl),':');
}
}
if(!isset($thisip)){ $thisip = $pingurl; }
if(strpos($thisip, "/") != false) {
$thisip = substr($thisip, 0, strpos($thisip, "/"));
}
$returnArray = array();
$newping = array();
$sent = 0;
$lost = 0;
$timeMax = 0;
$timeMin = 0;
$timeAve = 0;
$status = 1;
if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
$pingresult = exec("ping -n 2 -w 2 $thisip", $output, $status);
if(isset($output[6])){
$exoutput = explode(',',$output[6]);
$sent = preg_replace('/\D/', '', $exoutput[0]);
$lost = $sent - preg_replace('/\D/', '', $exoutput[1]);
}
if(isset($output[8])){
$exoutput = explode(',',$output[8]);
$timeMin = preg_replace('/\D/', '', $exoutput[0]);
$timeMax = preg_replace('/\D/', '', $exoutput[1]);
$timeAve = preg_replace('/\D/', '', $exoutput[2]);
}
} else {
$pingresult = exec("/bin/ping -c2 -w2 $thisip", $output, $status);
if(isset($output[5])){
$exoutput = explode(',',$output[5]);
$sent = preg_replace('/\D/', '', $exoutput[0]);
$lost = $sent - preg_replace('/\D/', '', $exoutput[1]);
}elseif(isset($output[3])){
$exoutput = explode(',',$output[3]);
$sent = preg_replace('/\D/', '', $exoutput[0]);
$lost = $sent - preg_replace('/\D/', '', $exoutput[1]);
}
if(isset($output[6])){
$exoutput = explode('=',$output[6]);
$exoutput = explode('/',$exoutput[1]);
$timeMin = $exoutput[0];
$timeMax = $exoutput[2];
$timeAve = $exoutput[1];
}
}
$newping['sent']=$sent;
$newping['lost']=$lost;
$newping['timeMin']=$timeMin;
$newping['timeMax']=$timeMax;
$newping['timeAve']=$timeAve;
$returnArray['stats']=$newping;
$returnArray['lastUpdate']=time();
$returnArray['ip']=$ip;
if ($status == "0") {
$returnArray['status']="on";
} else {
$returnArray['status']="off";
}
return $returnArray;
}
?>