Another Admin System (Python)

vlncvlnc Join Date: 2010-09-07 Member: 73921Members, Squad Five Blue
edited July 2012 in Server Discussion
Hey guys,

I wrote this program to manage a ns2 server using integrated web admin system. Don't be unkind with me, I started to learn Python 2 weeks ago and I'm not developping every day.

In parallel, i'm coding an irc bot who will implement the features of ns2admin.py. I will post the code later.

So here it looks like for now :

<img src="http://i.imgur.com/BRupZ.png" border="0" class="linked-image" />

This is just a simple menu where you type command and interact with your server. It doesn't handle multiple servers connection for now. You have to put the IP and PORT at the launch.

Exemple : status

<img src="http://i.imgur.com/Udoio.png" border="0" class="linked-image" />

<!--coloro:#FF8C00--><span style="color:#FF8C00"><!--/coloro-->
<!--sizeo:4--><span style="font-size:14pt;line-height:100%"><!--/sizeo-->What you need to change in the .py file to make it works with your server :<!--sizec--></span><!--/sizec--><!--colorc--></span><!--/colorc-->

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--># Credentials
username = ""
passsword = ""<!--c2--></div><!--ec2-->

<!--coloro:#FF8C00--><span style="color:#FF8C00"><!--/coloro-->
<!--sizeo:4--><span style="font-size:14pt;line-height:100%"><!--/sizeo-->Informations displayed<!--sizec--></span><!--/sizec--><!--colorc--></span><!--/colorc-->

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--># server banner
ServerBanner1 = "name_of_your_server"<!--c2--></div><!--ec2-->

if the map doesn't exist in the list below, you will be asked for a correct/existing map name

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--># map list
listMap = ["ns2_summit", "ns2_docking", "ns2_tram", "ns2_mineshaft"]<!--c2--></div><!--ec2-->

<!--coloro:#FF8C00--><span style="color:#FF8C00"><!--/coloro-->
<!--sizeo:4--><span style="font-size:14pt;line-height:100%"><!--/sizeo-->How to launch the program<!--sizec--></span><!--/sizec--><!--colorc--></span><!--/colorc-->

1/ get python on python.org
2/ open a prompt
3/ python ns2admin.py

Don't mind of IP and PORT, you will be ask to enter them when you launch the program.

I miss some control like int in str variable, stuff like that.
Ban and unban doesn't work for now
I haven't test rr, eject or slay but I think they work cause the url sent looks good (server is always empty :()

If you can test it and send your feedback about bugs, better way to code or else ...

File : <a href="http://www.filedropper.com/ns2admin_1" target="_blank">http://www.filedropper.com/ns2admin_1</a>

Thank you

The complete code :

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->#!/usr/bin/env python

import urllib2
import urllib
import cookielib
import socket
import json
import os
import getpass

# bold string
bold = "\033[1m"
reset = "\033[0;0m"

# server banner
ServerBanner1 = ""

ip = raw_input("ip: > ")
port = raw_input("port: > ")

# Master URL
theurl = "http://" + ip + ":" + port + "/"

# status
status = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_status"

# Credentials
username = ""
password = ""

# Map list
listMap = ["ns2_summit", "ns2_docking", "ns2_tram", "ns2_mineshaft"]

def cleaner ():
    try:
        raw_input('Hit enter to continue')
        os.system('clear')
    except:
        print 'Something went wrong here'
        exit(leaving)


def auth():

        passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
        passman.add_password(None, theurl, username, password)
        authhandler = urllib2.HTTPDigestAuthHandler(passman)
        opener = urllib2.build_opener(authhandler)
        urllib2.install_opener(opener)
            
def doSay ():

    try:
            print "\n" + bold + "Send a message to all\n" + reset
            message = raw_input("message: ")
            space = chr(32)
        
            spaceMessage = message.replace(space, "%20")
            
            sayAll = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_say%20" + spaceMessage
            pagehandleSay = urllib2.urlopen(sayAll, timeout=2)
            urlSayAll = pagehandleSay.geturl()

          
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlSayAll)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
        
    else:
            print '\nDEBUG -> url : %s' % (urlSayAll)
            cleaner()
            
def doStatus ():
     try:

            urlStatus = urllib2.urlopen(status, timeout=2).geturl()
            data = urllib2.urlopen(status, timeout=1).read()
            
            
     except urllib2.URLError, e:
            print "URLError -> %s" % (status)
            exit("exiting ...")
            
    
     except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
     else:
            
            if data == "Server not running":
                print "\nno json data\n"
                cleaner()
            else:
                jr = json.loads(data)
                
                # Server Informations
                ns2ServerName = jr['server_name']
                ns2Map = jr['map']
                ns2Cheats = jr['cheats']
                ns2PlayersOnline = jr['players_online']
                ns2Marines = jr['marines']
                ns2Aliens = jr['aliens']
                ns2FrameRate = jr['frame_rate']
                ns2DevMode = jr['devmode']
                ns2UpTime = jr['uptime']
            
            
            
                print bold + "\nPlayer List" + reset
                
                for i in range(len(jr['player_list'])):
                
                        print "\n N:", jr['player_list'][i]['name'], "SID:", jr['player_list'][i]['steamid'], "P:", jr['player_list'][i]['ping'], "ms", "IP:", jr['player_list'][i]['ipaddress']
                                              
            
                
                # Player informations available
                #playerName =  jr['player_list'][0]['name']
                #playerSteamId = jr['player_list'][0]['steamid']
                #playerPing = jr['player_list'][0]['ping']
                #playerDeaths = jr['player_list'][0]['deaths']
                #playerScore = jr['player_list'][0]['score']
                #playerComm = jr['player_list'][0]['iscomm']
                #playerIp = jr['player_list'][0]['ipaddress']
                #playerBot = jr['player_list'][0]['isbot']
                #playerRes = jr['player_list'][0]['resources']
                #playerKills = jr['player_list'][0]['kills']
                #playerTeam = jr['player_list'][0]['team']

                print bold + "\nServer Informations" + reset

                
                print "\n Server Name : %s\n Map : %s\n Players : %s\n Marines : %s\n Aliens : %s\n FrameRate : %s\n Cheats : %s" % (ns2ServerName, ns2Map, ns2PlayersOnline, ns2Marines, ns2Aliens, ns2FrameRate, ns2Cheats)
                print " Uptime : ", ns2UpTime / 60, "min\n"
                
                cleaner()

def doPassword ():
    try:

            inputPassword = getpass.getpass()
            
            space = chr(32)
            spacePassword = inputPassword.replace(space, "%20")
            
            password = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_password%20" + inputPassword
            pagehandlePassword = urllib2.urlopen(password, timeout=2)
            
            urlPassword = pagehandlePassword.geturl()
          
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlPassword)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
        
    else:
         print "\nDEBUG -> url : %s\n" % (urlPassword)
            
         if inputPassword == "":      
            print "no password"
  
         else:
            print "new password: %s" % (inputPassword)
            cleaner()

def doRrall ():
    try:
            rrAll = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_rrall"
            pagehandleRrall = urllib2.urlopen(rrAll, timeout=2)
            
            urlrrAll = pagehandleRrall.geturl()

            
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlrrAll)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
            
    else:
            print '\nDEBUG -> url : %s' % (urlrrAll)
            cleaner()          
        
def doChangemap ():
    try:
        print "\nMap list :\n"
        for item in listMap:
            print item
            
        inputMap = raw_input('\n> map: ')
        
        
        if inputMap in listMap:
            changemap = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_changemap%20" + inputMap
            pagehandleChangemap = urllib2.urlopen(changemap, timeout=5)
            urlChangeMap = pagehandleChangemap.geturl()
            print '\nDEBUG -> url : %s' % (urlChangeMap)
            print '\nServer is loading %s' % (inputMap)
          
        else:

            doChangemap()
          
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlChangeMap)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
                  

def doEject ():
    try:
    
            nick = raw_input('> nickname: ')
            
            space = chr(32)
            spaceNick = nick.replace(space, "%20")
                        
            eject = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_eject%20" + nick
            pagehandleEject = urllib2.urlopen(eject, timeout=2)
            
            urlEject = pagehandleEject.geturl()
            print ("DEBUG -> %s\n%s has been ejected") % (urlEject, nick)
            cleaner()
            
            
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlEject)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
    else:
           print ("DEBUG -> %s\n%s has been ejected") % (urlEject, nick)
           cleaner()

def doSlay ():
    try:
            nick = raw_input('> nickname: ')
            
            space = chr(32)
            spaceNick = nick.replace(space, "%20")
            
            slay = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_slay%20" + spaceNick
            pagehandleSlay = urllib2.urlopen(slay, timeout=2)
            urlSlay = pagehandleSlay.geturl()
          
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlSlay)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
    else:
            print ("DEBUG -> %s\n%s is dead now :<") % (urlSlay, nick)
            cleaner()
            
def doKick ():
    try:
    
            nick = raw_input('> nickname: ')
            reason = raw_input('> reason: ')
            
            space = chr(32)
            spaceReason = reason.replace(space, "%20")
              
            
            kick = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_kick%20" + nick
            say = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_say%20" + spaceReason + nick
                        
            pagehandleKick = urllib2.urlopen(kick, timeout=2)
            pagehandleKickReason = urllib2.urlopen(say, timeout=2)
            
            urlKick = pagehandleKick.geturl()
            urlReason = pagehandleKickReason.geturl()
            
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlKick)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
            
    else:
            print '\nDEBUG -> url : %s' % (urlKick)
            print '\nDEBUG -> url : %s' % (urlReason)
            print "\nPlayer '%s' kicked" % (nick )
            print 'reason: %s' % (reason)

def doBan ():
    try:
        
            banlist = "http://" + ip + ":" + port + "/?request=getbanlist"
            pagehandleBanlist = urllib2.urlopen(banlist, timeout=2)
            blist = urllib2.urlopen(banlist, timeout=2).read()
            print blist + "\n"          
            
            print json.loads(blist)
            
            nick = raw_input('> nickname: ')
            reason = raw_input('> reason: ')
            time = raw_input('> time: ')
            ban = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_ban%20" + nick + "%20" + reason + "%20" + time
            
            say = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_say%20" + nick + reason
                        
            pagehandleBan = urllib2.urlopen(ban, timeout=2)
            pagehandleBanReason = urllib2.urlopen(say, timeout=2)
            
            urlBan = pagehandleBan.geturl()
            
    
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlBan)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
            
    else:
            print '\nDEBUG -> %s' % urlBan

def doUnBan ():
    
    try:
            steamid = raw_input('> steamid: ')
            unban = "http://" + ip + ":" + port + "/?request=json&command=Send&rcon=sv_unban%20" + steamid
                                  
            pagehandleUnBan = urllib2.urlopen(unban, timeout=2)
                        
            urlUnBan = pagehandleUnBan.geturl()
    
    except urllib2.URLError, e:
            print "URLError -> %s" % (urlUnBan)
            print e.code
            exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
            print 'HTTPError'
            print 'Reason: ', e.reason
            exit("exiting ...")
            
    else:
            print '\nDEBUG -> %s' % urlUnBan

def doHelp ():

    print "\n" + bold + "Help Section" + reset + "\n\n  say : send a message on the server\n  status : activity on the server\n  password : change server password (leave blank for no password)\n  changemap : only enter the name (not the 'ns2_')\n  rr : send all to ready room\n  eject : eject commander\n  slay : kill a player\n  kick : eject from the server\n  ban : ban for x min\n  help : help section\n  quit : quit program\n\n"
    cleaner()

def doQuit ():
    exit("See you later ...")
    
    
def errhandler ():
    print "\nTry again ...\n"
    cleaner()
    
class main:
    # Authentification on the server
    auth()

    try:
        pagehandle = urllib2.urlopen(theurl, timeout=5)
        url = pagehandle.geturl()
        pagehandle.close()
    
    except urllib2.URLError, e:
        print "URLError -> %s" % (theurl)
        #print e.code
        exit("exiting ...")
            
    
    except urllib2.HTTPError, e:
        print 'HTTPError'
        print 'Reason: ', e.reason
        exit("exiting ...")
    
    else:
            os.system("clear")
          
            # set up a dictionary of actions
            takeaction = {
                "say": doSay,
                "status": doStatus,
                "password": doPassword,
                "changemap": doChangemap,
                "rr": doRrall,
                "eject": doEject,
                "slay": doSlay,
                "kick": doKick,
                "ban": doBan,
                "unban": doUnBan,
                "help": doHelp,
                "quit": doQuit
                }
        

            while True:

                print "\n" + ServerBanner1 + "\n"
                print bold + "       ###############################################\n       ### Natural Selection 2 AdminTool in Python ###\n       ###############################################\n" + reset + "\n" + bold + "\nCommands :" + reset + "\n\n * say\n * status\n * password\n * changemap\n * rr\n * eject\n * slay\n * kick\n * ban\n * unban\n * help\n * quit"
                cmd = raw_input("\n--> ")
                takeaction.get(cmd,errhandler)()<!--c2--></div><!--ec2-->

Comments

  • dePARAdePARA Join Date: 2011-04-29 Member: 96321Members, Squad Five Blue
    ALL- i can say, INteristing feature.

    But, dont release new cool features to the community. "Test" it forever and praise your server for his cool features.

    ;)

    No, really, good work pain.
  • HughHugh Cameraman San Francisco, CA Join Date: 2010-04-18 Member: 71444NS2 Developer, NS2 Playtester, Reinforced - Silver, Reinforced - Onos, WC 2013 - Shadow, Subnautica Developer, Pistachionauts
  • xDragonxDragon Join Date: 2012-04-04 Member: 149948Members, NS2 Playtester, Squad Five Gold, NS2 Map Tester, Reinforced - Shadow
    Cant help but feel like theres a hidden message here, and honestly its pretty low that you'd drag that into this unrelated thread...

    Script seems interesting and could certainly be helpful should something like #ns2scrim become popular.
  • vlncvlnc Join Date: 2010-09-07 Member: 73921Members, Squad Five Blue
    edited July 2012
    Thank for your responses, I appreciate

    Well i forgot to thanks some people for their helps and tips :

    - darkalex from #nolifenofrag
    - saderzq from #exertus
  • JuCCiJuCCi Join Date: 2011-08-08 Member: 114961Members, NS2 Map Tester
    edited July 2012
    <!--quoteo(post=1953650:date=Jul 25 2012, 01:37 PM:name=dePARA)--><div class='quotetop'>QUOTE (dePARA @ Jul 25 2012, 01:37 PM) <a href="index.php?act=findpost&pid=1953650"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec--><!--coloro:#FF00FF--><span style="color:#FF00FF"><!--/coloro-->ALL-<!--colorc--></span><!--/colorc--> i can say, <!--coloro:#FF00FF--><span style="color:#FF00FF"><!--/coloro-->IN<!--colorc--></span><!--/colorc-->teristing feature.

    But, dont release new cool features to the community. "Test" it forever and praise your server for his cool features.

    ;)

    No, really, good work pain.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Hater detected! As i said in the other thread HBZ daPARA. It's not my mod. It's being tested... And xDragon and eh? will release when they are ready.

    And sorry to the OP, just had to filter out the hate. Some people get butt hurt when they don't get what they want right away....
  • vlncvlnc Join Date: 2010-09-07 Member: 73921Members, Squad Five Blue
    edited July 2012
    Guys, please, I don't care about your little war. Just don't use this thread for that s**t.

    Use PM next time

    Thank you
  • swalkswalk Say hello to my little friend. Join Date: 2011-01-20 Member: 78384Members, Squad Five Blue
    I know you put alot of hours into this. You should be rewarded with Squad 5, imo!
  • vlncvlnc Join Date: 2010-09-07 Member: 73921Members, Squad Five Blue
    <!--quoteo(post=1953951:date=Jul 26 2012, 05:23 PM:name=swalk)--><div class='quotetop'>QUOTE (swalk @ Jul 26 2012, 05:23 PM) <a href="index.php?act=findpost&pid=1953951"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I know you put alot of hours into this. You should be rewarded with Squad 5, imo!<!--QuoteEnd--></div><!--QuoteEEnd-->

    Thanks mate !

    I'm just doing that for community and my personnal education. A reward will be just a little +.

    About the irc bot, it's still on the rail, I'll create a new thread for this.
Sign In or Register to comment.