forked from marty90/errant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace_run
executable file
·106 lines (77 loc) · 3.01 KB
/
trace_run
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
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
import pickle
import signal
import time
import sys
import os
import argparse
MODEL_FILE = "models.pickle"
def signal_handler(sig, frame):
StopProfile()
sys.exit(0)
def ValidProfile(Profiles,Profile):
try:
Profiles.loc[(Profile["operator"],Profile["country"],Profile["technology"], Profile["quality"])]
except:
print("Error, model not available:")
for k,v in Profile.items():
print(" {}: {}".format(k,v))
print("Available models are:")
print(Profiles.reset_index()[['Operator','Country_low','access_technology','signal_quality']].to_string(index=False))
return False
return True
def ApplyProfile(p, Interface, DryRun):
Command = "./errant -o %s -c %s -t %s -q %s -i %s" % \
(p["operator"], p["country"], p["technology"], p["quality"], Interface)
if DryRun:
Command += " -d"
before = time.time()
os.system(Command)
end = time.time()
return end-before
def StopProfile():
Command = "./errant -r"
if dry_run:
Command += " -d"
os.system(Command)
def main():
Profiles = pickle.load(open(MODEL_FILE,"rb"))
parser = argparse.ArgumentParser()
parser.add_argument('-f', dest = 'scenario_file', type=str, default='trace_example.csv', help= 'File with the trace to run')
parser.add_argument('-i', dest = 'interface', type=str, required=True, help= 'Destination interface')
parser.add_argument('-d', dest = 'dry_run', action='store_true', help= 'Make a dry run')
parser.add_argument('-n', dest = 'no_close', action='store_true', help= 'Leave last shaping policy on, when ending')
globals().update(vars(parser.parse_args()))
fin = open(scenario_file)
Scenario = []
for line in fin.read().splitlines()[1:]:
ls = line.strip().split(",")
Profile = { "operator" : ls[0].lower(),
"country" : ls[1].lower(),
"technology" : ls[2].lower(),
"quality" : ls[3].lower(),
"expire" : ls[4],
"period" : ls[5],
}
if(ValidProfile(Profiles,Profile) == False): sys.exit(-1)
Scenario.append(Profile)
fin.close()
signal.signal(signal.SIGINT, signal_handler)
for i, profile in enumerate(Scenario):
this_expire = int(profile["expire"])
if profile["period"] != '' and profile["period"] != '-':
period = int(profile["period"])
while this_expire > 0:
taken = ApplyProfile(profile,interface, dry_run)
time.sleep( min(period,this_expire)-taken )
this_expire -= period
else:
taken = ApplyProfile(profile,interface, dry_run)
time.sleep(this_expire-taken)
if not no_close:
StopProfile()
else:
print("Leaving Installed Policies")
return
if __name__ == "__main__":
main()