PHP ping Minecraft Server using cURL PHP ping Minecraft Server using cURL curl curl

PHP ping Minecraft Server using cURL


I realise you've resolved your issue but I also had a need for a command-line Minecraft 'ping' so I ported your PHP code to a standalone Perl script and thought I'd share it here in case anyone else needed something similar. The script uses only core modules so it should work anywhere you have Perl installed.

#!/usr/bin/perl################################################################################ Script:   mcping## Author:   Grant McLean <grant@mclean.net.nz>## Description:## 'ping' a minecraft server to check it's alive#use strict;use warnings;use IO::Socket;use Pod::Usage;use Getopt::Long  qw(GetOptions);use Encode        qw(decode);use Time::HiRes   qw(gettimeofday tv_interval);my(%opt);if(!GetOptions(\%opt, 'help|?')) {    pod2usage(-exitval => 1,  -verbose => 0);}pod2usage(-exitstatus => 0, -verbose => 2) if $opt{help};my $target = shift or pod2usage(    -exitval => 1,  -verbose => 0, -message => 'No host specified');my $port = 25565;if($target =~ /(.*?):(\d+)$/) {    $target = $1;    $port   = $2;}ping_server($target, $port);exit 0;sub ping_server {    my($host, $port) = @_;    my $t0 = [gettimeofday];    my $s = IO::Socket->new(        Domain   => AF_INET,        PeerAddr => $host,        PeerPort => $port,        Proto    => 'tcp',    ) || die "$!\n";    $s->autoflush(1);    print $s "\xFE";    sysread($s, my $resp, 256);    my $elapsed = tv_interval($t0);    die "Malformed response after connect\n" unless $resp =~ /^\xFF/;    substr($resp, 0, 3, '');    $resp = decode('UCS-2', $resp);    my($motd, $players, $max_players) = split /\x{A7}/, $resp;    print "Msg of the Day:  $motd\n"        . "Players Online:  $players\n"        . "Max Players:     $max_players\n";    printf "Ping Time:       %5.3fs\n", $elapsed;}__END__=head1 NAMEmcping - 'ping' a minecraft server=head1 SYNOPSIS  mcping [options] host-or-ip:port  Options:   -?     more detailed help message=head1 DESCRIPTIONAttempts to connect to a minecraft server on the specified host:port.  Onsuccess, a brief report like this will be printed:  Msg of the Day:  A Minecraft Server  Players Online:  2  Max Players:     10  Ping Time:       0.175sIf the :port is not specified, the default port number of 25565 will be used.=head1 OPTIONS=over 4=item B<-?>Display this documentation.=back=head1 AUTHOR & COPYRIGHTThis script was written by Grant McLean ( grant@mclean.net.nz ) as a Perl portof a similar PHP script here: http://stackoverflow.com/questions/10055839/This script may be freely used, copied and distributed under the same terms asPerl itself.=cut