def startup(config): print(tform("Starting program", 'HEADER')) print("Fetching template") template = get_template(config) print(tform("Program running", 'OKGREEN')) return template
def startup(config): print(tform("Starting program", 'HEADER')) print("Fetching configuration") sysconf = get_config(config) #print(json.dumps(sysconf, indent=2, sort_keys=True)) print(tform("Program running", 'OKGREEN')) return sysconf
def shutdown(logs): print(tform("Halting program", 'WARNING')) print(" Do not quit, storing logs") store_log('tank_sim.log', logs) print(" Program ended")
def get_template(filename): try: with open(filename, 'r') as infile: return infile.read() except: print("\r" + tform("Incorrect template file.", "FAIL")) print("Template must adhere to Jinja2 format") sys.exit()
def get_config(filename): try: with open(filename, 'r') as infile: return json.load(infile) except: print("\r" + tform("Incorrect configuration file.", "FAIL")) print("System configuration must be JSON format") sys.exit()
def reportTanks(self, cycle): if cycle == None: output = ["END CONDITIONS"] else: output = ["Cycle: " + str(cycle)] for o in self.objects: if (o.fault): output.append("> " + tform("FAULT: Tank is dry", 'FAIL') + (" " * 8)) else: if o.value <= (o.capacity * 0.35): output.append("> " + tform( "tank level: {0:.2f}".format(o.value), 'WARNING') + (" " * 5)) else: output.append("> tank level: {0:.2f}".format(o.value)) return "{: <15} {: <25} {: <25} {: <25} {: <25} {: <25}".format( *output)
def main(): if (len(sys.argv) < 3): print("\r" + tform("Incorrect usage.", "FAIL")) print("eg: sim.py <system_configuration> <simulated_cycles>") sys.exit() config = str(sys.argv[1]) cycles = int(sys.argv[2]) sysconf = startup(config) system = JSON2Obj(sysconf) try: print("Running simulation for " + str(cycles) + " cycles.") logs = runSimulation(system, cycles) print(tform("Simulation completed successfully", 'OKGREEN')) except (KeyboardInterrupt): print("\r" + tform("Simultaion interrupted by user", "FAIL")) shutdown(logs)
def main(): if (len(sys.argv) < 3): print("\r" + tform("Incorrect usage.", "FAIL")) print("eg: sim.py <system_configuration> <output_directory>") sys.exit() config = str(sys.argv[1]) out_dir = str(sys.argv[2]) sysconf = startup(config) system = JSON2Obj(sysconf) #print(system.getSimNodes()) shutdown("no logs")
def update(self): update = ["SYSTEM UPDATE"] updated = False formatter = "{: <15}" for sensor in self.sensors: sensor.source.update() if (sensor.read() < (sensor.source.capacity * 0.3)): sensor.source.inc( (random.randint(0, 100) + (random.randint(0, 100)))) update.append("> " + tform("LEVEL: Adding water", 'OKBLUE') + (" " * 4)) updated = True else: update.append(">") formatter += " {: <25}" return (updated, formatter.format(*update))
def JSON2Obj(d): print(tform("Building control system", 'HEADER')) system = control_system() for key, value in d.items(): s = d[key] for key, value in s.items(): current = key si = s[key] for l in si: if current == "objects": system.addObject(industial_object(l["label"], l["value"])) if current == "sensors": obj = system.getObject(l["object"]) system.addSensor(industrial_sensor(obj, l["label"])) if current == "actuators": pass if current == "plcs": pass return system
def main(): if (len(sys.argv) < 2): print("\r" + tform("Incorrect usage.", "FAIL")) print("eg: sim.py <script_template>") sys.exit() templateName = str(sys.argv[1]) pyPlate = startup(templateName) args = {'cycleCount': 100000, 'tankSize': 1000} try: print("- Applying template") t = Template(pyPlate) script = t.render(args) print("- ...") print("- Success") except: print("! Templating failed") store_script("test.py", script) shutdown("logs")
def shutdown(logs): print(tform("Halting program", 'WARNING')) #store_log('tank_sim.log', logs) print(tform("Program ended", 'OKGREEN'))
def inSubnet(ip, subnet): if ipaddress.ip_address(ip) in ipaddress.ip_network(subnet): return True else: return False def maskIP(ip, mask): ip_integer = ip2int(ip) mask_integer = ip2int(mask) return int2ip(ip_integer & mask_integer) if len(sys.argv) != 4: print('Usage is: ' + tform( '$python blind-ip.py <original pcap file> <rule file> <output pcap file>', "WARNING")) print('Example usage: ' + tform('$python blind-ip.py original.pcap rules.ip /tmp/blind.pcap', "WARNING")) sys.exit(1) pcap = sys.argv[1] rulefile = sys.argv[2] outfile = sys.argv[3] rules = [] with open(rulefile, 'r') as f: for line in f: rules.append(line.split())