php curl 模拟ping,PHP使用cURL ping Minecraft服务器

我意识到你已经解决了你的问题,但我也需要一个命令行Minecraft’ping’,所以我将你的PHP代码移植到一个独立的Perl脚本,并认为我会在这里分享它以防其他人需要类似的东西.该脚本仅使用核心模块,因此它应该在您安装Perl的任何位置工作.

#!/usr/bin/perl

##############################################################################

#

# Script: mcping

#

# Author: Grant McLean

#

# 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 NAME

mcping - 'ping' a minecraft server

=head1 SYNOPSIS

mcping [options] host-or-ip:port

Options:

-? more detailed help message

=head1 DESCRIPTION

Attempts to connect to a minecraft server on the specified host:port. On

success, a brief report like this will be printed:

Msg of the Day: A Minecraft Server

Players Online: 2

Max Players: 10

Ping Time: 0.175s

If 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 & COPYRIGHT

This script was written by Grant McLean ( grant@mclean.net.nz ) as a Perl port

of a similar PHP script here: https://stackoverflow.com/questions/10055839/

This script may be freely used, copied and distributed under the same terms as

Perl itself.

=cut

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值