Advertisement
jmarler

checkminecraft-17.pl

Dec 7th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. use IO::Socket;
  6. use Pod::Usage;
  7. use Getopt::Long qw(GetOptions);
  8. use Encode qw(decode encode);
  9. use Time::HiRes qw(gettimeofday tv_interval);
  10. use LWP::Simple;
  11. use JSON qw(decode_json);
  12. use Data::Dumper;
  13.  
  14.  
  15. my(%opt);
  16.  
  17. if(!GetOptions(\%opt, 'help|?')) {
  18. pod2usage(-exitval => 1, -verbose => 0);
  19. }
  20.  
  21. pod2usage(-exitstatus => 0, -verbose => 2) if $opt{help};
  22.  
  23. my $target = shift or pod2usage(
  24. -exitval => 1, -verbose => 0, -message => 'No host specified'
  25. );
  26.  
  27. my $port = 25565;
  28. if($target =~ /(.*?):(\d+)$/) {
  29. $target = $1;
  30. $port = $2;
  31. }
  32.  
  33. ping_server($target, $port);
  34.  
  35. exit 0;
  36.  
  37. sub ping_server {
  38. my($host, $port) = @_;
  39.  
  40. my $t0 = [gettimeofday];
  41. my $s = IO::Socket->new(
  42. Domain => AF_INET,
  43. PeerAddr => $host,
  44. PeerPort => $port,
  45. Proto => 'tcp',
  46. ) || die "$!\n";
  47.  
  48. $s->autoflush(1);
  49.  
  50. # Packet identifier for a handshake packet
  51. my $packeta = "\x00";
  52.  
  53. # Protocol Version
  54. $packeta .= "\x04";
  55.  
  56. # Length of hostname
  57. my $hostnamelength = pack 'c', length($host);
  58. $packeta .= $hostnamelength;
  59.  
  60. # Hostname
  61. $packeta .= $host;
  62.  
  63. # Port
  64. my $packedport = pack 'n', $port;
  65. $packeta .= $packedport;
  66.  
  67. # Next state (1 for status)
  68. $packeta .= "\x01";
  69. my $packetalen = pack 'c' , length($packeta);
  70. print $s $packetalen;
  71. print $s $packeta;
  72. my $fullpacketa = $packetalen . $packeta;
  73.  
  74. # Status request packet
  75. my $packetb = "\x01\x00";
  76. print $s $packetb;
  77.  
  78. #sysread($s, my $resp, 4096);
  79.  
  80. sysread($s, my $resp, 4096);
  81.  
  82. my $elapsed = tv_interval($t0);
  83.  
  84. #print "Packet A: \n";
  85. #hdump($fullpacketa);
  86.  
  87. #print "Packet B: \n";
  88. #hdump($packetb);
  89.  
  90. # Clean the response
  91. $resp =~ s/^[^{]*{/{/;
  92. $resp =~ s/\xc2|\xa7.//g;
  93. $resp =~ s/[^ -~]//g;
  94.  
  95. #print "\nResponse: \n" ;
  96. #hdump($resp);
  97. #print "\n$resp\n";
  98.  
  99. # Decode the json
  100. my $decoded_resp = decode_json($resp);
  101.  
  102. #print Dumper($decoded_resp);
  103.  
  104. # Find the bits
  105. my $protocol = $decoded_resp->{'version'}{'protocol'};
  106. my $version = $decoded_resp->{'version'}{'name'};
  107. my $motd = $decoded_resp->{'description'};
  108. my $players = $decoded_resp->{'players'}{'online'};
  109. my $max_players = $decoded_resp->{'players'}{'max'};
  110.  
  111. print "Protocol Version: $protocol\n"
  112. . "Minecraft Version: $version\n"
  113. . "Msg of the Day: $motd\n"
  114. . "Players Online: $players\n"
  115. . "Max Players: $max_players\n";
  116.  
  117. printf "Ping Time: %5.3fs\n", $elapsed;
  118. }
  119.  
  120.  
  121. sub hdump {
  122. my $offset = 0;
  123. my(@array,$format);
  124. foreach my $data (unpack("a16"x(length($_[0])/16)."a*",$_[0])) {
  125. my($len)=length($data);
  126. if ($len == 16) {
  127. @array = unpack('N4', $data);
  128. $format="0x%08x (%05d) %08x %08x %08x %08x %s\n";
  129. } else {
  130. @array = unpack('C*', $data);
  131. $_ = sprintf "%2.2x", $_ for @array;
  132. push(@array, ' ') while $len++ < 16;
  133. $format="0x%08x (%05d)" .
  134. " %s%s%s%s %s%s%s%s %s%s%s%s %s%s%s%s %s\n";
  135. }
  136. $data =~ tr/\0-\37\177-\377/./;
  137. printf $format,$offset,$offset,@array,$data;
  138. $offset += 16;
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement