sock_to_gzip_file.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/python
  2. #I love the python Power Glove. It's so bad!
  3. #Usage: sudo -u suricata ./sock_to_gzip_file.py --output-file="http.log.gz" --listen-sock="http.log.sock"
  4. import socket,os
  5. import gzip
  6. import sys
  7. from optparse import OptionParser
  8. if __name__ == "__main__":
  9. parser = OptionParser()
  10. #Path to the socket
  11. parser.add_option("--listen-sock", dest="lsock", type="string", help="Path to the socket we will listen on.")
  12. #Path to gzip file we will write
  13. parser.add_option("--output-file", dest="output", type="string", help="Path to file name to output gzip file we will write to.")
  14. #parse the opts
  15. (options, args) = parser.parse_args()
  16. options.usage = "example: sudo -u suricata ./sock_to_gzip_file.py --output-file=\"http.log.gz\" --listen-sock=\"http.log.sock\"\n"
  17. #Open the output file
  18. if options.output:
  19. try:
  20. f = gzip.open(options.output, 'wb')
  21. except Exception,e:
  22. print("Error: could not open output file %s:\n%s\n", options.output, e)
  23. sys.exit(-1)
  24. else:
  25. print("Error: --output-file option required and was not specified\n%s" % (options.usage))
  26. sys.exit(-1)
  27. #Open our socket and bind
  28. if options.lsock:
  29. if os.path.exists(options.lsock):
  30. try:
  31. os.remove(options.lsock)
  32. except OSError:
  33. pass
  34. try:
  35. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  36. s.bind(options.lsock)
  37. s.listen(1)
  38. conn, addr = s.accept()
  39. except Exception,e:
  40. print("Error: Failed to bind socket %s\n%s\n", options.lsock, e)
  41. sys.exit(-1)
  42. else:
  43. print("Error: --listen-sock option required and was not specified\n%s" % (options.usage))
  44. sys.exit(-1)
  45. #Read data from the socket and write to the file
  46. while 1:
  47. data = conn.recv(1024)
  48. if not data: break
  49. f.write(data)
  50. conn.close()
  51. f.close()