Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. ### Simple IP Monitor
  2. import datetime
  3. import os
  4. import time
  5. from clint.textui import colored
  6.  
  7.  
  8. def format_string(str, min_length):
  9.     while len(str) < min_length:
  10.         str += " "
  11.     return str
  12.  
  13.  
  14. class IP:
  15.     def __init__(self, str_ip, online, since):
  16.         self.str_ip = str_ip
  17.         self.online = online
  18.         self.since = since
  19.  
  20.     def format_time(self):
  21.         seconds = (datetime.datetime.now() - self.since).total_seconds()
  22.         time_str = ""
  23.  
  24.         WEEK = 604800
  25.         DAY = 216000
  26.         HOUR = 3600
  27.         MINUTE = 60
  28.  
  29.         while (seconds >= 0):
  30.             if (seconds >= WEEK):
  31.                 weeks = int(seconds / WEEK)
  32.                 seconds -= (weeks * WEEK)
  33.                 time_str += int(weeks).__str__() + " weeks "
  34.             if (seconds >= DAY):
  35.                 days = int(seconds / DAY)
  36.                 seconds -= days * DAY
  37.                 time_str += int(days).__str__() + " days "
  38.             if (seconds >= HOUR):
  39.                 hours = int(seconds / HOUR)
  40.                 seconds -= hours * HOUR
  41.                 time_str += int(hours).__str__() + " hours "
  42.             if (seconds >= MINUTE):
  43.                 minutes = int(seconds / MINUTE)
  44.                 seconds -= minutes * MINUTE
  45.                 time_str += int(minutes).__str__() + " minutes "
  46.             if (seconds <= 60):
  47.                 return time_str + int(seconds).__str__() + " seconds "
  48.  
  49.     def __str__(self):
  50.         return "%s %s for %s" % (
  51.             format_string(self.str_ip, 15),
  52.             colored.green("Online  ") if self.online else colored.red("Offline "),
  53.             self.format_time(),
  54.         )
  55.  
  56.  
  57. def get_ips(file_path):
  58.     ips_list = [line.rstrip('\n') for line in open(file_path)]
  59.  
  60.     ips_dict = {}
  61.  
  62.     for str_ip in ips_list:
  63.         ips_dict[str_ip] = IP(str_ip, False, datetime.datetime.now())
  64.  
  65.     return ips_dict
  66.  
  67.  
  68. def check_ips(ip_dict):
  69.     for ip_key in ip_dict.keys():
  70.  
  71.         ip = ip_dict[ip_key]
  72.  
  73.         response = os.system("ping -c 1 -W 1 " + str(ip.str_ip) + " >/dev/null 2>&1")
  74.  
  75.         if (response == 0):
  76.             # dont update time if already online before call
  77.             if not ip.online:
  78.                 # update time
  79.                 ip.since = datetime.datetime.now()
  80.             # set online
  81.             ip.online = True
  82.         else:
  83.             ip.online = False
  84.  
  85.  
  86. def print_ipinfo(ip_dict):
  87.     for ip_key in ip_dict:
  88.         ip = ip_dict[ip_key]
  89.         print(ip)
  90.     print()
  91.  
  92.  
  93. filepath = './iplist.txt'
  94.  
  95. ip_dict = get_ips(filepath)
  96.  
  97. while True:
  98.     check_ips(ip_dict)
  99.     print_ipinfo(ip_dict)
  100.  
  101.     time.sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement