gsquery.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Game Server Query
  4. # Author: Anonymous & Daniel Gibbs
  5. # # Website: http://gameservermanagers.com
  6. # Version: 030515
  7. import optparse
  8. import socket
  9. import sys
  10. class GameServer:
  11. def __init__( self, options, arguments ):
  12. self.option = options
  13. self.argument = arguments
  14. #
  15. self.server_response_timeout = 5
  16. self.default_buffer_length = 1024
  17. #
  18. if self.option.engine == 'source':
  19. self.query_prompt_string = '\xFF\xFF\xFF\xFFTSource Engine Query\0'
  20. if self.option.engine == 'goldsource':
  21. self.query_prompt_string = '\xFF\xFF\xFF\xFFTSource Engine Query\0'
  22. if self.option.engine == 'spark':
  23. self.query_prompt_string = '\xFF\xFF\xFF\xFFTSource Engine Query\0'
  24. if self.option.engine == 'realvirtuality':
  25. self.query_prompt_string = '\xFF\xFF\xFF\xFFTSource Engine Query\0'
  26. if self.option.engine == 'unity3d':
  27. self.query_prompt_string = '\xFF\xFF\xFF\xFFTSource Engine Query\0'
  28. elif self.option.engine == 'unreal':
  29. self.query_prompt_string = '\x5C\x69\x6E\x66\x6F\x5C'
  30. elif self.option.engine == 'unreal2':
  31. self.query_prompt_string = '\x79\x00\x00\x00\x00'
  32. elif self.option.engine == 'avalanche':
  33. self.query_prompt_string = '\xFE\xFD\x09\x10\x20\x30\x40'
  34. self.connected = False
  35. self.response = None
  36. self.sanity_checks()
  37. def fatal_error( self, error_message, error_code=1 ):
  38. sys.stderr.write( 'ERROR: ' + str(error_message) + '\n' )
  39. sys.exit( error_code )
  40. def exit_success( self, success_message='' ):
  41. sys.stdout.write( 'OK: ' + str(success_message) + '\n' )
  42. sys.exit( 0 )
  43. def responding( self ):
  44. # Connect.
  45. connection = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
  46. connection.settimeout( self.server_response_timeout )
  47. try:
  48. self.connected = connection.connect( ( self.option.address, int(self.option.port) ) )
  49. except socket.timeout:
  50. self.fatal_error( 'Request timed out', 1 )
  51. except:
  52. self.fatal_error( 'Unable to connect', 1 )
  53. # Send.
  54. connection.send( self.query_prompt_string )
  55. # Receive.
  56. try:
  57. self.response = connection.recv( self.default_buffer_length )
  58. except socket.error:
  59. self.fatal_error( 'Unable to receive', 2 )
  60. connection.close()
  61. # Response.
  62. if self.response == None:
  63. self.fatal_error( 'No response', 3 )
  64. if len( self.response ) < 10 :
  65. sys.exit( 'Short response.', 3 )
  66. else:
  67. self.exit_success( str( self.response ) )
  68. def sanity_checks( self ):
  69. if not self.option.address:
  70. self.fatal_error( 'No IPv4 address supplied.', 4 )
  71. if not self.option.port:
  72. self.fatal_error( 'No port supplied.', 4 )
  73. if __name__ == '__main__':
  74. parser = optparse.OptionParser(
  75. usage = 'usage: python %prog [options]',
  76. version = '%prog 0.0.1'
  77. )
  78. parser.add_option(
  79. '-a', '--address',
  80. action = 'store',
  81. dest = 'address',
  82. default = False,
  83. help = 'The IPv4 address of the server.'
  84. )
  85. parser.add_option(
  86. '-p', '--port',
  87. action = 'store',
  88. dest = 'port',
  89. default = False,
  90. help = 'The IPv4 port of the server.'
  91. )
  92. parser.add_option(
  93. '-e', '--engine',
  94. action = 'store',
  95. dest = 'engine',
  96. default = False,
  97. help = 'Engine type: avalanche, goldsource, realvirtuality, spark, source, unity3d, unreal, unreal2.'
  98. )
  99. parser.add_option(
  100. '-v', '--verbose',
  101. action = 'store_true',
  102. dest = 'verbose',
  103. default = False,
  104. help = 'Display verbose output.'
  105. )
  106. parser.add_option(
  107. '-d', '--debug',
  108. action = 'store_true',
  109. dest = 'debug',
  110. default = False,
  111. help = 'Display debugging output.'
  112. )
  113. options, arguments = parser.parse_args()
  114. #
  115. server = GameServer( options, arguments )
  116. server.responding()