Browse Source

First commit

Alex Sidorov 1 year ago
commit
bd45b04055
4 changed files with 103 additions and 0 deletions
  1. 75 0
      pingset.py
  2. 2 0
      requarements.txt
  3. 7 0
      settings.json
  4. 19 0
      templates/template.html

+ 75 - 0
pingset.py

@@ -0,0 +1,75 @@
+import os, platform, webbrowser, 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 start(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)
+
+    start(data_from_json)

+ 2 - 0
requarements.txt

@@ -0,0 +1,2 @@
+tqdm==4.65.0
+Jinja2==3.1.2

+ 7 - 0
settings.json

@@ -0,0 +1,7 @@
+{
+    "list_ip": [ 
+        "172.16.10.231",
+        "172.16.10.233"
+    ],
+    "count": 100
+}

+ 19 - 0
templates/template.html

@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="utf-8">
+        <meta http-equiv="X-UA-Compatible" content="IE=edge">
+        <title></title>
+        <meta name="description" content="">
+        <meta name="viewport" content="width=device-width, initial-scale=1">
+        <link rel="stylesheet" href="">
+    </head>
+    <body>
+        {% for item in list_result %}
+            --------------------------------------------------
+            <h4><font color = #008080> {{ date }} </font color></h4>
+            <h3><font color = #0000FF> {{ item.time }} : ( {{item.ip}} : <font color = #8B0000> {{ item.good_ping }} / {{ count }} ) </font></h2>
+            <br />
+        {% endfor %}
+    </body>
+</html>