Skip to content

Instantly share code, notes, and snippets.

@metasta
Created March 31, 2010 05:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metasta/349979 to your computer and use it in GitHub Desktop.
Save metasta/349979 to your computer and use it in GitHub Desktop.
Half-Life RCON tool
/**********************************
*
* hlrcon
* HLDS rcon client
* usage:
* hlrcon <addr> <port> <pass> <cmd>
*
*
* Thanks to:
* Ryozi
*
**********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
int main(int argc, char ** argv)
{
int s = -1, i = 4;
char send[256], b[262144];
struct addrinfo hints, *sv;
fd_set fds;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 200000;
if(argc < 4) exit(fputs("usage: rcon <addr> <port> <pass> <cmd>\n", stderr));
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM;
hints.ai_family = AF_INET;
getaddrinfo(argv[1], argv[2], &hints, &sv);
s = socket(sv->ai_family, sv->ai_socktype, sv->ai_protocol);
if(s == -1) exit(fputs("Failed to create Socket.", stderr));
sprintf(send, "\xFF\xFF\xFF\xFF challenge rcon\n");
sendto(s, send, strlen(send) + 1, 0, sv->ai_addr, sv->ai_addrlen);
recv(s, b, sizeof(b), 0);
strtok(b, "\n");
sprintf(send, "\xFF\xFF\xFF\xFF rcon %s \"%s\"", b + 19, argv[3]);
for(i = 4; i < argc; i++) {
strcat(send, " ");
strcat(send, argv[i]);
}
sendto(s, send, strlen(send) + 1, 0, sv->ai_addr, sv->ai_addrlen);
FD_ZERO(&fds);
FD_SET(s, &fds);
select(s + 1, &fds, NULL, NULL, &timeout);
if(FD_ISSET(s, &fds)) {
recv(s, b, sizeof(b), 0);
printf("%s", b + 5);
}
freeaddrinfo(sv);
close(s);
return 0;
}
#!/usr/bin/perl
#
# hlrcon.pl
# HLDS rcon client
# usage
# perl hlrcon.pl <cmd>
#
# SETTINGS
# 1. Your Server Address:Port. my $addr = "localhost:27015";
my $addr = "";
#
# 2. Your Server rcon_password. my $pass = "mypassword";
my $pass = "";
#
####################################
@ARGV > 0 or die "usage: hlrcon.pl <cmd>\n";
use strict;
use IO::Socket;
my $s = IO::Socket::INET->new(PeerAddr=>$addr,Proto=>'udp',Timeout=>5) or die $!;
$s->print("\xff\xff\xff\xffchallenge rcon");
my $r = $s->getline; $r =~ tr/0-9//cd;
$s->print("\xff\xff\xff\xffrcon $r \"$pass\" @ARGV") or die $!;
$s->recv(my $msg, 65535); print substr($msg, 5);
$s->close;
#!/usr/bin/ruby -Ku
def usage()
puts "usage: "+$0+" <host> <port> <pass> <cmd>"
puts " ex.)"
puts " "+$0+" localhost 27015 asdf changelevel de_nuke"
end
if ARGV.size < 4 then
usage()
exit(1)
end
host = ARGV.shift
port = ARGV.shift.to_i
pass = ARGV.shift
cmd = ARGV.join(' ')
require 'socket'
s = UDPSocket.new()
s.connect(host, port)
s.send("\xff\xff\xff\xffchallenge rcon", 0)
r = s.recvfrom(32)[0].gsub(/[^0-9]/,'')
s.send("\xff\xff\xff\xffrcon " + r + " \"" + pass + "\" "+ cmd, 0)
print s.recvfrom(65536)[0][5,65536]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment