php มีใครเคยใช้คำสั่ง Ping ไปที่ IP Server ตรวจสอบ Server Offline / Online (จะใช้ PHP เขียน) บ้างครับ รบกวนหน่อยครับ
Ping
<?
echo exec("ping /k google.co.th");
?>
http://php.net/manual/en/function.exec.php
ประวัติการแก้ไข 2011-06-13 13:57:51
Date :
2011-06-13 13:57:05
By :
dekkuza
อย่าลืมดัดแปลงไฟล์ sudoers ด้วยนะครับต้องสั่งให้ apache เป็น root ด้วยเด้อ
Date :
2012-01-10 17:49:32
By :
zenix
Code
<?php
/*
*
* https://gist.github.com/k0nsl/733955a3c3093832de49
*
*/
$title = "Server Status"; // website's title
$servers = array(
'202.9.90.89:80' => array(
'ip' => '202.9.90.89',
'port' => 80,
'info' => 'Hosted by The Cloud',
'purpose' => 'Web Search'
),
'202.9.90.89:22' => array(
'ip' => '202.9.90.89',
'port' => 22,
'info' => 'SSL',
'purpose' => 'SFTP'
),
'202.9.90.89:1433' => array(
'ip' => '202.9.90.89',
'port' => 1433,
'info' => 'Sql',
'purpose' => 'mssql'
)
,
'202.9.90.89:38101' => array(
'ip' => '202.9.90.89',
'port' => 38101,
'info' => 'Logig',
'purpose' => 'cabal login'
)
,
'202.9.90.89:38121' => array(
'ip' => '202.9.90.89',
'port' => 38121,
'info' => 'channal 38121',
'purpose' => 'cabal chat'
)
,
'202.9.90.89:38111' => array(
'ip' => '202.9.90.89',
'port' => 38111,
'info' => 'channal 38111',
'purpose' => 'cabal channal'
)
,
'202.9.90.89:38112' => array(
'ip' => '202.9.90.89',
'port' => 38112,
'info' => 'channal 38112',
'purpose' => 'cabal channal'
),
'202.9.90.89:38113' => array(
'ip' => '202.9.90.89',
'port' => 38113,
'info' => 'channal 38113',
'purpose' => 'cabal channal'
),
'gooseza.com 80' => array(
'ip' => 'gooseza.com',
'port' => 80,
'info' => 'gooseza.com',
'purpose' => '80'
),
'gooseza.com 21' => array(
'ip' => 'gooseza.com',
'port' => 21,
'info' => 'gooseza.com',
'purpose' => '21'
)
);
if (isset($_GET['host'])) {
$host = $_GET['host'];
if (isset($servers[$host])) {
header('Content-Type: application/json');
$return = array(
'status' => test($servers[$host])
);
echo json_encode($return);
exit;
} else {
header("HTTP/1.1 404 Not Found");
}
}
$names = array();
foreach ($servers as $name => $info) {
$names[$name] = md5($name);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/2.3.2/cosmo/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css">
<style type="text/css">
/* Custom Styles */
</style>
</head>
<body>
<div class="container">
<h1><?php echo $title; ?></h1>
<table class="table">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Host</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<?php foreach ($servers as $name => $server): ?>
<tr id="<?php echo md5($name); ?>">
<td><i class="icon-spinner icon-spin icon-large"></i></td>
<td class="name"><?php echo $name; ?></td>
<td><?php echo $server['info']; ?></td>
<td><?php echo $server['purpose']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript">
function test(host, hash) {
// Fork it
var request;
// fire off the request to /form.php
request = $.ajax({
url: "<?php echo basename(__FILE__); ?>",
type: "get",
data: {
host: host
},
beforeSend: function () {
$('#' + hash).children().children().css({'visibility': 'visible'});
}
});
// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR) {
var status = response.status;
var statusClass;
if (status) {
statusClass = 'success';
} else {
statusClass = 'error';
}
$('#' + hash).removeClass('success error').addClass(statusClass);
});
// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown) {
// log the error to the console
console.error(
"The following error occured: " +
textStatus, errorThrown
);
});
request.always(function () {
$('#' + hash).children().children().css({'visibility': 'hidden'});
})
}
$(document).ready(function () {
var servers = <?php echo json_encode($names); ?>;
var server, hash;
for (var key in servers) {
server = key;
hash = servers[key];
test(server, hash);
(function loop(server, hash) {
setTimeout(function () {
test(server, hash);
loop(server, hash);
}, 16000);
})(server, hash);
}
});
</script>
</body>
</html>
<?php
/* Misc at the bottom */
function test($server) {
$socket = @fsockopen($server['ip'], $server['port'], $errorNo, $errorStr, 3);
if ($errorNo == 0) {
return true;
} else {
return false;
}
}
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
?>
Date :
2020-12-05 13:26:16
By :
gooseza
Load balance : Server 01