DeaD_EyE

srcds_scanner

Mar 10th, 2018
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. This program scans for srcds servers from port 27000 - 50000
  3. in your local network.
  4.  
  5. A prepared broadcast is sent to the port range.
  6. Afterwards the script prints out ip:port of servers.
  7. """
  8.  
  9. import binascii
  10. import socket
  11. import time
  12. import logging
  13. import sys
  14.  
  15.  
  16. REQUEST = "ff ff ff ff 54 53 6f 75 72 63 65 20 45 6e 67 69 6e 65 20 51 75 65 72 79 00".replace(' ', '')
  17. logging.basicConfig()
  18. logger = logging.getLogger('scanner')
  19.    
  20.  
  21. def create_socket():
  22.     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  23.     sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  24.     sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  25.     return sock
  26.  
  27.  
  28. def send_broadcasts(sock, start, end):
  29.     request = request = binascii.a2b_hex(REQUEST)
  30.     for port in range(start, end + 1):
  31.         sock.sendto(request, ('255.255.255.255', port))
  32.  
  33.  
  34. def receive_with_timeout(sock, timeout):
  35.     sock.setblocking(False)
  36.     t_start = time.time()
  37.     while True:
  38.         if time.time() - t_start > timeout:
  39.             break
  40.         try:
  41.             response, addr = sock.recvfrom(1024)
  42.         except BlockingIOError:
  43.             continue
  44.         else:
  45.             print('{}:{}'.format(*addr))
  46.  
  47.  
  48. def main():
  49.     logger.debug('Creating socket')
  50.     sock = create_socket()
  51.     logger.debug('Sending Broadcasts')
  52.     send_broadcasts(sock, 27000, 50001)
  53.     logger.debug('Receiving responses')
  54.     receive_with_timeout(sock, 5)
  55.  
  56.  
  57. if __name__ == '__main__':
  58.     if len(sys.argv) == 2 and sys.argv[1] == '-v':
  59.         level = logging.DEBUG
  60.     else:
  61.         level = logging.INFO
  62.     logger.setLevel(level)
  63.     main()
RAW Paste Data