import os import platform import webbrowser import json import subprocess as sp import datetime as dt from jinja2 import Environment, FileSystemLoader from tqdm import tqdm env = Environment( loader=FileSystemLoader('templates') ) def get_state_ping(ip_address): if platform.system() in 'Linux': command = "ping -w 1 -c 1 %s" else: command = "ping -n 1 %s" try: status, result = sp.getstatusoutput( command % (ip_address) ) except ValueError: return 1 return status def ip_check(ip: str, count: int) -> dict: pbar = tqdm() pbar.reset(total=count) count_good_ping = 0 time = dt.datetime.now().strftime('%H:%M:%S') for _ in range(count): if get_state_ping(ip) == 0: count_good_ping += 1 pbar.update() pbar.refresh() return { "ip": ip, "good_ping": count_good_ping, "time": time } def main(params: dict): list_result = [] for ip in params.get('list_ip'): print(F"Check ip: {ip}") list_result.append(ip_check(ip, int(params.get('count')))) template = env.get_template("template.html") render_template = template.render({ "count": params.get('count'), "list_result": list_result, "date": dt.date.today(), }) html_path = os.path.join(os.path.dirname( __file__), F"report_{dt.datetime.today().timestamp()}.html") with open(html_path, 'w') as f: f.write(render_template) webbrowser.open(F"file://{html_path}", new=2) if __name__ == "__main__": data_from_json = None if os.path.exists('settings.json'): with open('settings.json') as f: data_from_json = json.load(f) main(data_from_json)