#/bin/python3 import sys import tailhead import platform import subprocess import datetime, time import json def _make_gen(reader): b = reader(1024 * 1024) while b: yield b b = reader(1024*1024) def file_len(filename): """ More opti than wc on turis """ f = open(filename, 'rb') f_gen = _make_gen(f.raw.read) return sum( buf.count(b'\n') for buf in f_gen ) def multi_ping(hosts, interface): """ Returns True if host (list of str) responds to a ping request. Remember that a host may not respond to a ping (ICMP) request even if the host name is valid. """ # Option for the number of packets as a function of count = '-n' if platform.system().lower()=='windows' else '-c' source = '-S' if platform.system().lower()=='windows' else '-I' if type(hosts) is list: procs = [] for host in hosts: command = ['ping', count, '1', source, interface, host] proc = subprocess.Popen(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) procs.append(proc) procs_done = [] while True: for proc in procs: if proc.poll() is not None: procs_done.append(proc.poll()) procs.pop(procs.index(proc)) if len(procs) == 0: #print("o") break time.sleep(1) if 0 in procs_done: return 0 else: return 1 else: print("fuck you host must be a list") sys.exit() def log_write(status): with open(LOG_PATH_FILE, 'a') as f: f.write("{} {}\n".format(tools.date(), status)) def uqmi_current_settings(): command = ['uqmi', '-d', '/dev/cdc-wdm0', '--get-current-settings'] proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) datas = json.loads(proc.stdout) return datas["ipv4"]["ip"], datas["ipv4"]["gateway"], datas["ipv4"]["subnet"] def ifconfig_update(interface, ip, subnet): command = ['ifconfig', interface, ip, 'netmask', subnet, 'up'] proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def route_update(gateway): command = ['route', 'add', 'default', 'gw', gateway] proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def tail(file, lines): return tailhead.tail(open(file, 'rb'), lines) def date(): # Calculate the offset taking into account daylight saving time utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone utc_offset = datetime.timedelta(seconds=-utc_offset_sec) return datetime.datetime.now().replace(tzinfo=datetime.timezone(offset=utc_offset)).isoformat()