forked from winny-/mcstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcstat_program.php
executable file
·66 lines (54 loc) · 1.82 KB
/
mcstat_program.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
#!/usr/bin/env php
<?php
require_once dirname(__FILE__).'/mcstat.php';
/*
=========================
Program portion of mcstat
=========================
Make sure to add a shebang to the first line to use as a cli program. Note
the shebang will be visible in webpages, so don't use a shebanged copy in
a website. An example shebang as follows:
#!/usr/bin/env php
Invocation like so:
$ mcstat uberminecraft.com
uberminecraft.com v1.7.4 2714/5000 131ms
Uberminecraft Cloud | 22 Games
1.7 Play Now!
*/
// This is PHP's idiom to check if script is being invoked directly.
// http://stackoverflow.com/questions/2413991/php-equivalent-of-pythons-name-main
if (!count(debug_backtrace())) {
error_reporting(E_ERROR | E_PARSE);
$STDERR = fopen('php://stderr', 'w+');
$errorCount = 0;
$args = array_slice($argv, 1);
foreach ($args as $arg) {
$hostWithPort = explode(':', $arg);
$len = count($hostWithPort);
$host = $hostWithPort[0];
$port = 25565;
if ($len == 2) {
$port = $hostWithPort[1];
} elseif ($len != 1) {
print('Invalid host '.$arg);
exit(++$errorCount);
}
$m = new MinecraftStatus($host, $port);
$reply = $m->ping();
if (!$reply) {
fwrite($STDERR, 'Error pinging '.$host.':'.$port.' ('.$m->lastError.")\n");
$errorCount++;
continue;
}
$motd = preg_replace("/\\x{00A7}./u", '', $reply['motd']);
$message = $host;
$message .= ($port == 25565) ? '' : ':'.$port;
$message .= ' '.$reply['server_version'];
$message .= ' '.$reply['player_count'].'/'.$reply['player_max'];
$message .= ' '.$reply['latency'].'ms'."\n";
$message .= $motd."\n";
print($message);
}
exit($errorCount);
}
?>