1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#/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()
|