Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Simple IP Monitor
- import datetime
- import os
- import time
- from clint.textui import colored
- def format_string(str, min_length):
- while len(str) < min_length:
- str += " "
- return str
- class IP:
- def __init__(self, str_ip, online, since):
- self.str_ip = str_ip
- self.online = online
- self.since = since
- def format_time(self):
- seconds = (datetime.datetime.now() - self.since).total_seconds()
- time_str = ""
- WEEK = 604800
- DAY = 216000
- HOUR = 3600
- MINUTE = 60
- while (seconds >= 0):
- if (seconds >= WEEK):
- weeks = int(seconds / WEEK)
- seconds -= (weeks * WEEK)
- time_str += int(weeks).__str__() + " weeks "
- if (seconds >= DAY):
- days = int(seconds / DAY)
- seconds -= days * DAY
- time_str += int(days).__str__() + " days "
- if (seconds >= HOUR):
- hours = int(seconds / HOUR)
- seconds -= hours * HOUR
- time_str += int(hours).__str__() + " hours "
- if (seconds >= MINUTE):
- minutes = int(seconds / MINUTE)
- seconds -= minutes * MINUTE
- time_str += int(minutes).__str__() + " minutes "
- if (seconds <= 60):
- return time_str + int(seconds).__str__() + " seconds "
- def __str__(self):
- return "%s %s for %s" % (
- format_string(self.str_ip, 15),
- colored.green("Online ") if self.online else colored.red("Offline "),
- self.format_time(),
- )
- def get_ips(file_path):
- ips_list = [line.rstrip('\n') for line in open(file_path)]
- ips_dict = {}
- for str_ip in ips_list:
- ips_dict[str_ip] = IP(str_ip, False, datetime.datetime.now())
- return ips_dict
- def check_ips(ip_dict):
- for ip_key in ip_dict.keys():
- ip = ip_dict[ip_key]
- response = os.system("ping -c 1 -W 1 " + str(ip.str_ip) + " >/dev/null 2>&1")
- if (response == 0):
- # dont update time if already online before call
- if not ip.online:
- # update time
- ip.since = datetime.datetime.now()
- # set online
- ip.online = True
- else:
- ip.online = False
- def print_ipinfo(ip_dict):
- for ip_key in ip_dict:
- ip = ip_dict[ip_key]
- print(ip)
- print()
- filepath = './iplist.txt'
- ip_dict = get_ips(filepath)
- while True:
- check_ips(ip_dict)
- print_ipinfo(ip_dict)
- time.sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement