PDA

View Full Version : queryshell script


arit93
9th November, 2002, 03:52 AM
Just playing around with perl and server query. all that is required to run this is perl. If you on windows I would suggest ActiveSate's perl. Not sure if this will be useful to anyone let me know if it is.


arit93

[code:1:b07a92a943]
#!/usr/bin/perl

###################################################################
# Author: Arit93
# Date: 11-8-2002
# Title: queryshell.pl
# Description: This will open a socket to the Unreal Tournament
# server. you can then query away for information. This script
# needs the IP of hostname of the server running an Ureal Tournament
# server and the port that the gamespy network is listening on.
####################################################################

use strict;
use IO::Socket;

#Global socket object
my $sock;

#opensocket($host, $port) will open a por and start listening
sub opensocket($$){
my($host,$port) = @<hidden>;
$sock = new IO::Socket::INET(
PeerAddr =&gt; $host,
PeerPort =&gt; $port,
Proto =&gt; 'udp',
);
die &quot;Could not open socket: $!\n&quot; unless $sock;
}
#sendquery(Query string) sends the query to the server
sub sendquery($){

#send the query wrapped in backslashes
$sock-&gt;send(&quot;\\@<hidden>\\&quot;);
#receive the response
$sock-&gt;recv(my $line,15000)or die&quot;$!&quot;;

#parse the results into a hash the will
#contain the data element and the value
my @<hidden> = split /\\/,$line;
my %info;
my $i;
#eliminate the leading space that is placed there
#on the split. This is because the string returned by
#the server starts with a \
shift(@<hidden>);

#load the hash
for($i=0;$i&lt;@<hidden>;$i+=2){
if($messages[$i] ne 'final'){
$info{$messages[$i]}=$messages[$i+1];
}
}

#Display to the user
foreach my $message(sort keys %info){
print &quot;$message - $info{$message}\n&quot;;
}

}
#closesock will close down the socket
sub closesock(){
close($sock);
}

#prints the help dialog
sub helpmess(){
print &lt;&lt;END_OF_HELP;

exit - Leave the queryshell
help - print this message

queries
basic - basic game information:
Game Name
Game Version
Location

info - Full information
Hostname
Host port
Host ip
Map Name
Game Type
Number of Players
Max Players
Game Mode

rules - rules of the current game
Time Limit
Frag Limit
Multiplayer Bots
Change Levels
Admin Name
Admin Email

players - list of players
Name
Score
Team
Skin
Mesh
Ping
Response

status - Combines basic + info + rules + players

echo\\echo_string - parrots the echo_string back at ya

secure\\challenge_string used by gamespy to ensure its a
valid UT server

Please address any additional questions to the nearest
brick wall

END_OF_HELP
}

###########################################
# begin mainline execution#

my $b=1; #boolean to control execution
my $comm; #query command to server
my $host = $ARGV[0]; #host to connect to
my $port = $ARGV[1]; #prot to connect to

#make sure we have a host and port
if($host eq '' || $port eq ''){
print &quot; usage\: queryshell.pl server port\n&quot;;
$b=0;
}

#connect to the host
&amp;opensocket($host,$port);

#start the shell
while($b){
#give the user a prompt
print &quot;\$&quot;;
#take the input
chomp($comm = &lt;STDIN&gt;);
#check to see if we are shutting down
if($comm eq 'exit'){
$b=0;
}elsif($comm eq 'help'){ #does the user need help?
&amp;helpmess;
}else{ #send the query
eval{
#catch $SIG{ALRM} This is important because if
#we send the server an unknown command we will get
#no return and that will lock us up. Thats a bad thing
local $SIG{ALRM}= sub{print &quot;request timed out\n&quot;};
#set the alarm to fire in 5 seconds
alarm 5;
#send the query
sendquery($comm);
#clear the alarm
alarm 0;
};
#according to the book this will avoid the race condition
alarm 0;
}
}
#clean up and go home
&amp;closesock;


[/code:1:b07a92a943]

PHX_Big_Deal
20th January, 2003, 06:27 PM
Hey you show me here something what i am looking for....i look a long time for a script in pl or cgi...what must i do to run that sript? :thumbup:

SnapsProvolone
20th January, 2003, 11:41 PM
I think all you have to do is save the script, CHMOD it to give Execute permissions then run it. It then needs parameters supplied for Host and Port (the port is the port that the gamespy network is listening on). Call the script with host and port vars and it should work...

PHX_Big_Deal
21st January, 2003, 05:02 PM
so imust call it like .../script.php?123.123.123.123:7777

or must i do it different?
Thx

SnapsProvolone
21st January, 2003, 11:57 PM
The script is written to run from the shell prompt. Don't name the file PHP it's a perl file name it either *.cgi or *.pl

Try calling it like this from the shell prompt.

./queryshell.cgi HOSTIP PORT

It's like you call a program from the command line in DOS (Except for the ./ part LoL)

The Port is the gamespy query port I think but I am not sure, haven't been able to get it to do anything except say "time out".

After you fire the script off you just get another command prompt, type HELP and this should give you the list of commands.

Again, I have to state that this is VERY untested and buggy, if your not a perl developer then this may be a bit tough to get working. Personally I will stick with UT2003 Area Script from http://www.ut2003area.net/

PHX_Big_Deal
23rd January, 2003, 04:13 PM
:( I now that this other script works fine, but i have only php 3.0.18 on my webserver and it needs php 4.2.2 to work.
It look likes that nobody have a running script for php3, cgi or pl ???