4
0

gsquery.py 3.4 KB

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