pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin private pastebin - collaborative debugging tool What's a private pastebin?

Posted by frymaster on Fri 22nd Oct 16:42
download | new post

  1. #!/usr/bin/python
  2.  
  3. #Wrapper around initctl functions.  This way only a small subset of functionality is allowed.
  4. #  This way sudo access can be granted to this script for game admins
  5.  
  6. import optparse,csv,sys,os,subprocess
  7.  
  8. def doStuff(server,command):
  9.         validCommands = "start","stop","status"
  10.         if os.getuid() == 0:
  11.                 if command=="list":
  12.                         reader = csv.reader(open("/storage/games/scripts/gameservers.csv"),quoting=csv.QUOTE_NONE)
  13.                         statusOutput = subprocess.Popen(["/sbin/initctl","list"],stdout=subprocess.PIPE).communicate()[0].split("\n")
  14.                         output=[]
  15.                         serverID = []
  16.                         for row in reader:
  17.                                 if len(row)>1:
  18.                                         serverID.append(row[0])
  19.                         for line in statusOutput:
  20.                                 for id in serverID:
  21.                                         if line[:len(id)]==id:
  22.                                                 output.append(line)
  23.                         print "\n".join(output)
  24.                         return output
  25.                 elif command in validCommands:
  26.                         reader = csv.reader(open("/storage/games/scripts/gameservers.csv"),quoting=csv.QUOTE_NONE)
  27.                         for row in reader:
  28.                                 if len(row)>1:
  29.                                         if row[0].lower()==server:
  30.                                                 if command=="status":
  31.                                                         os.execl("/sbin/initctl",command,server)
  32.                                                         sys.exit(0)
  33.                                                 else:
  34.                                                         os.execl("/sbin/initctl",command,server,"--no-wait")
  35.                                                         sys.exit(0)
  36.  
  37.  
  38.                         print server,"is not a valid server name"
  39.                         sys.exit(1)
  40.                 else:
  41.                         print command,"is not a valid command"
  42.                         sys.exit(1)
  43.         else:
  44.                 print "You must be root to run this program"
  45.                 sys.exit(1)
  46.  
  47.  
  48. def main():
  49.         """Runs program and handles command line options"""
  50.         p = optparse.OptionParser(description=' Controls game servers and lists their status',
  51.                 prog='serverControl',
  52.                 version='%prog 0.1',
  53.                 usage='\t%prog SERVER-NAME command')
  54.  
  55.         options, arguments = p.parse_args()
  56.         if len(arguments)==2:
  57.                 return doStuff(arguments[0].lower(),arguments[1].lower())
  58.         else:
  59.                 p.print_help()
  60.  
  61.  
  62.  
  63. if __name__ == '__main__':
  64.         main()

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Use syntax highlighting

To highlight particular lines, prefix each line with @@


Remember my settings