Author Topic: rcon_client v1.0 - a rcon client with a multiple servers support  (Read 2234 times)

mRokita

  • Autococker
  • Posts: 598
Hi!
I've wrote it for myself some time ago, and i think it can be useful for some people.
Download: http://htmldp.com/share.php?download=rcon_client
Commands:
Quote from: rcon_client help
servers - displays a list of configured servers (not only active)
switch - changes the active server list. Options:
   switch all - switches to all available servers
   switch <ID> (Example: switch 0) - switches to <ID> server
   switch <ID1>, <ID2>, <ID3> - switches to servers <ID1>, <ID2> and <ID3>
makeconf - opens the config manager (use it to edit the serverlist)
Code: [Select]
# ----------------------------------------------
#|               rcon_client v1.0               |
#| Copyright (c) hTml 2014. All Rights Reserved |
#|                www.htmldp.com                |
# ----------------------------------------------
import socket
import webbrowser as www
from urllib import urlopen
import sys
import urllib2
from time import time
from time import sleep
import os
import webbrowser
def update(url):
   webbrowser.open_new_tab(url)
  
ver="1.0"
try:
   act=urlopen('http://htmldp.com/ver/rcon_client').read().split('\n')
   if ver!=act[0]:
      print "New version available ("+act[0]+")!\nDo you wish to download it now?"
      if raw_input("[Y/N]: ")[0].lower()=="y":
         print act[1]
         update(act[1])
         sys.exit()
except Exception as e:
   print(e)
   raw_input("No connection to the update server. Press enter to quit.")
   sys.exit()

  
print " ----------------------------------------------"
print "|               rcon_client v1.0               |"
print "| Copyright (c) hTml 2014. All Rights Reserved |"
print "|                www.htmldp.com                |"
print "|      Type 'help' for available commands.     |"
print " ----------------------------------------------"

try:
   fo=open("config.txt")
except:
   print "No config.txt file in the directory. Opening the config editor..."
   creating=True
   linez=[]
   while creating:
      pom1="add "
      pom1=pom1+raw_input("Enter server's hostname/IP: ")
      pom1=pom1+":"+raw_input("Enter server's port: ")+" "
      pom1=pom1+raw_input("Enter the rcon_password value: ")+"\n"
      linez.append(pom1)
      if raw_input("Do you want to add another server? [Y/N] ")[0].lower()=="n":
         creating=False
   plik=open("config.txt", "w+")
   plik.writelines(linez)
   plik.close()
fo=open("config.txt", "r")
fox=fo.readlines()
fo.close()
srv_backup=[]
srv=srv_backup
try:
   for i in fox:
      if i[-1]=="\n":
         i=i[:-1]
      if i.split(' ')[0]=="add":
         i=i[4:]
         i=i.split(' ')
         i[0]=i[0].split(':')
         i=[(i[0])[0], (i[0])[1], i[1]]
         srv_backup.append(i)
except:
   print "Invalid config file. Opening the config editor..."
   creating=True
   linez=[]
   while creating:
      pom1="add "
      pom1=pom1+raw_input("Enter server's hostname/IP: ")
      pom1=pom1+":"+raw_input("Enter server's port: ")+" "
      pom1=pom1+raw_input("Enter the rcon_password value: ")+"\n"
      linez.append(pom1)
      if raw_input("Do you want to add another server? [Y/N] ")[0].lower()=="n":
         creating=False
   plik=open("config.txt", 'w+')
   plik.writelines(linez)
   plik.close()
   fo=open("config.txt", "r")
   fox=fo.readlines()
   fo.close()
   srv_backup=[]
   for i in fox:
      if i[-1]=="\n":
         i=i[:-1]
      if i.split(' ')[0]=="add":
         i=i[4:]
         i=i.split(' ')
         i[0]=i[0].split(':')
         i=[(i[0])[0], (i[0])[1], i[1]]
         srv_backup.append(i)
   srv=srv_backup
  
def rconall(command):
   for i in srv:
      try:
         print i[0]+":"+i[1]+":\n"+rcon(i[0], int(i[1]), i[2], command)
      except:
         print "Invalid server port. Use the makeconf command to edit config.txt"
      
def rcon(host, port, rcon_password, command):
   try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
      sock.connect((host, port))
      sock.send("\xFF\xFF\xFF\xFFrcon %s %s\0" % (rcon_password, command))
      resp=sock.recv(1024)
      sock.close()
      return resp[10:]
   except:
      return "Cant connect to "+host+" at "+str(port)+"."
  
while 1:
   try:
      x=raw_input("rcon/> ")
      pom=1
      if x=="servers":
         for i in srv_backup:
            print "("+str(srv_backup.index(i))+") "+i[0]+":"+i[1]
         pom=0
        
      if x=="help":
         print "------]Available Commands[------"
         print "servers - displays a list of configured servers (not only active)"
         print "switch - changes the active server list. Options:\n   switch all - switches to all available servers\n   switch <ID> (Example: switch 0) - switches to <ID> server\n   switch <ID1>, <ID2>, <ID3> - switches to servers <ID1>, <ID2> and <ID3>"
         print "makeconf - opens the config manager (use it to edit the serverlist)"
         pom=0

      if x=="makeconf":
         creating=True
         linez=[]
         fo.close()
         while creating:
            pom1="add "
            pom1=pom1+raw_input("Enter server's hostname/IP: ")
            pom1=pom1+":"+raw_input("Enter server's port: ")+" "
            pom1=pom1+raw_input("Enter the rcon_password value: ")+"\n"
            linez.append(pom1)
            if raw_input("Do you want to add another server? [Y/N]")[0].lower()=="n":
               creating=False
         plik=open("config.txt", 'w')
         plik.writelines(linez)
         plik.close()
         fo=open("config.txt")
         fox=fo.readlines()
         fo.close()
         fo=fox
         srv_backup=[]
         for i in fo:
            if i[-1]=="\n":
               i=i[:-1]
            if i.split(' ')[0]=="add":
               i=i[4:]
               i=i.split(' ')
               i[0]=i[0].split(':')
               i=[(i[0])[0], (i[0])[1], i[1]]
               srv_backup.append(i)
         srv=srv_backup
         pom=0
        
      if x[:7]=="switch ":
         if x[7:]=="all":
            srv=srv_backup
            print "You are connected to all the configured servers."
         else:
            listka=x[7:]
            listka=listka.split(', ')
            srv=[]
            for i in listka:
               srv.append(srv_backup[int(i)])
            print "Active servers:"
            for i in srv:
               print i[0]+":"+i[1]
         pom=0
        
      if pom==1:
         rconall(x)
   except Exception as e:
      print "[ERROR]"
      print "       "+str(e)
      print "       If it appears at other commands, check config.txt."
« Last Edit: October 15, 2014, 01:52:24 PM by hTml »

xrichardx

  • 68 Carbine
  • Posts: 295
Re: rcon_client v1.0 - a rcon client with a multiple servers support
« Reply #1 on: October 16, 2014, 09:12:21 AM »
You should change your rcon() function. 1024 is a very small buffer size considering someone might want to get the answer to a "rcon status" or "rcon sv listuserip" packet which can easily get bigger than that. Also, at some point paintball splits the content to different packets if adding another line to the current packet exceeds a specific amount of bytes (I think 1500 is the limit), so you want to check for additional packets and append their content.