LDShadowLord

Internet Watcher

Jul 4th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. #This scripts automatically polls the Google DNS server every 15 seconds to check that it has internet connectivity - It logs both LIVE and DEAD statuses to a formatted .csv file
  2.  
  3. #Import required libraries
  4. import logging, time, socket, os, sys
  5.  
  6. #Declare Variables
  7. ##Path to folder, leave it empty to put log files in executable folder
  8. folderpath=""
  9. count = 0
  10. ##Max number of iterations before it restarts itself - 5760 is roughly 24 hours
  11. maxCount = 5760
  12.  
  13. #Configure Logging
  14. logging.basicConfig(filename=folderpath+time.strftime("%d%m20%y-%H%M%S",time.localtime())+".csv",level=logging.INFO,datefmt="%d %b - %Y : %H:%M:%S",format="%(asctime)s , %(levelname)s , %(message)s")
  15. console = logging.StreamHandler()
  16. console.setLevel(logging.INFO)
  17. formatter = logging.Formatter("%(asctime)s , %(levelname)s , %(message)s","%d %b - %Y : %H:%M:%S")
  18. console.setFormatter(formatter)
  19. logging.getLogger('').addHandler(console)
  20.  
  21. while True:
  22.   time.sleep(10)
  23.   if count > maxCount:
  24.     logging.shutdown()
  25.     time.sleep(0.2)
  26.     os.execv(sys.executable, ['python'] + sys.argv)
  27.   else:
  28.     try:
  29.       time.sleep(5)
  30.       count = count + 1
  31.       s = socket.create_connection(("8.8.8.8", 53),5)
  32.       logging.info(str(count)+" , LIVE")
  33.     except:
  34.       logging.warn(str(count)+" , DEAD")
Add Comment
Please, Sign In to add comment