Python Netzwerkcheck: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „==Netzwerkcheck== <syntaxhighlight> #!/usr/bin/python #pip install pyping colorama wget dat_sys="/usr/local/etc/funktion.cfg" import os import socket, datetim…“)
(kein Unterschied)

Version vom 4. Dezember 2019, 12:02 Uhr

Netzwerkcheck

#!/usr/bin/python
#pip install pyping colorama wget 
dat_sys="/usr/local/etc/funktion.cfg"
import os
import socket, datetime
import pyping,sys
from colorama import init, Fore, Style
dat=open(dat_sys,"r")
init()
def ping_status(name, hostname):
  r = pyping.ping(hostname, timeout=100, count=1)
  if r.ret_code == 0:
      print(Style.RESET_ALL + " ping status "  +  name + " " +  Fore.GREEN + "ok")
  else:
     print(Style.RESET_ALL + " ping status "   + name + " " + Fore.RED + "failed")

def port_status(name,hostname,port):
   socket.setdefaulttimeout(1)
   start_time = datetime.datetime.now()
   try:
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     connect = sock.connect_ex((hostname, int(port)))
     if connect == 0:
        print(Style.RESET_ALL + " port status "  +  name + " " +  Fore.GREEN + "ok")
     else:
        print(Style.RESET_ALL + " port status hostname can't connect "  +  name + " " +  Fore.RED + "not ok")
     sock.close()
   except socket.gaierror:
        print(Style.RESET_ALL + " port status hostname can't resolved "  +  name + " " +  Fore.RED + "not ok")
   except socket.error:
        print(Style.RESET_ALL + " port status hostname can't connect "  +  name + " " +  Fore.RED + "not ok")
   end_time = datetime.datetime.now()


def proxy_status(name,url,proxy,port):
    x = os.system("env http_proxy="+proxy+":"+port + " " + " https_proxy="+proxy+":"+port + " "  +  "wget --spider --tries  3 -T 1 -q " + url)
    if x == 0:
       print(Style.RESET_ALL + " proxy status "  +  name + " " +  Fore.GREEN + "ok")
    else:
       print(Style.RESET_ALL + " proxy status "  +  name + " " +  Fore.RED + "not ok")

for l in dat:
    line = l.rstrip().split(";")
    if (len(line)) == 2:
        ping_status(line[0], line[1])
    if (len(line)) == 3:
        port_status(line[0], line[1], line[2])
    if (len(line)) == 4:
        proxy_status(line[0], line[1], line[2], line[3])

dat.close()