#!/usr/bin/perl -w use strict; use IO::Socket; use Time::HiRes qw(tv_interval gettimeofday); my $DEF_PORT = 27015; my ($host, $name, $map, $active, $max, $ping, $port); format SERVER = @<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<< @<<< $host, $name, $map, $active . '/' . $max, $ping . $~ = "SERVER"; my @servers; die "invalid usage" unless $ARGV[0]; if ($ARGV[0] eq '-f') { open(CONFIG, "<$ARGV[1]") or die "can't open $ARGV[1]: $!"; while () { chomp; ($host, $port) = split; push(@servers, [$host, ($port or $DEF_PORT)]); } close(CONFIG); } elsif ($ARGV[0]) { push(@servers, [$ARGV[0], ($ARGV[1] or $DEF_PORT)]); } foreach my $server (@servers) { my $sock = IO::Socket::INET->new(PeerAddr => $server->[0], PeerPort => $server->[1], Proto => 'udp'); unless ($sock) { warn("could not connect to ", join(':', $server->[0], $server->[1]), " ($!)"); next; } output_info($sock); $sock->close(); } sub hl_send { my $sock = shift(); my $cmd = shift(); print $sock pack("NZ*C", -1, $cmd, 0); } sub hl_recv { my $sock = shift(); my $recv = undef; recv($sock, $recv, 1024, 0); return $recv; } sub output_info { my $sock = shift(); my $t0 = [gettimeofday()]; hl_send($sock, "info"); my $recv = hl_recv($sock); unless (length($recv)) { warn "problem fetching data from remote host"; return; } $ping = tv_interval($t0); $ping = sprintf("%.0d", 1000 * $ping); my @junk; (@junk[0..4], $host, $name, $map, @junk[5..6], $active, $max, $junk[7]) = unpack("C5Z*Z*Z*Z*Z*CCC", $recv); $name =~ s/ /_/; write(); }