+from mininet.net import Mininetfrom mininet.cli import CLIfrom minicps.mcps import MiniCPSfrom topo import ScadaTopoimport sysimport timeimport shleximport subprocessimport signalautomatic = 0class Minitown(MiniCPS): """ Script to run the Minitown SCADA topology """ def __init__(self, name, net): signal.signal(signal.SIGINT, self.interrupt) signal.signal(signal.SIGTERM, self.interrupt) net.start() r0 = net.get('r0') # Pre experiment configuration, prepare routing path r0.cmd('sysctl net.ipv4.ip_forward=1') if automatic: self.automatic_start() else: CLI(net) net.stop() def interrupt(self, sig, frame): self.finish() sys.exit(0) def automatic_start(self): plc1 = net.get('plc1') plc2 = net.get('plc2') scada = net.get('scada') self.create_log_files() plc1_output = open("output/plc1.log", 'r+') plc2_output = open("output/plc2.log", 'r+') scada_output = open("output/scada.log", 'r+') physical_output = open("output/physical.log", 'r+') self.plc1_process = plc1.popen(sys.executable, "automatic_plc.py", "-n", "plc1", stderr=sys.stdout, stdout=plc1_output ) time.sleep(0.2) self.plc2_process = plc2.popen(sys.executable, "automatic_plc.py", "-n", "plc2", stderr=sys.stdout, stdout=plc2_output ) self.scada_process = scada.popen(sys.executable, "automatic_plc.py", "-n", "scada", stderr=sys.stdout, stdout=scada_output ) print "[*] Launched the PLCs and SCADA process, launching simulation..." plant = net.get('plant') simulation_cmd = shlex.split("python automatic_plant.py -s pdd -t minitown -o physical_process.csv") self.simulation = plant.popen(simulation_cmd, stderr=sys.stdout, stdout=physical_output) print "[] Simulating..." while self.simulation.poll() is None: pass self.finish() def create_log_files(self): subprocess.call("./create_log_files.sh") def end_plc_process(self, plc_process): plc_process.send_signal(signal.SIGINT) plc_process.wait() if plc_process.poll() is None: print("[*] Forcing termination of PLC process") plc_process.terminate() if plc_process.poll() is None: print("[*] Killing process") plc_process.kill() def finish(self): print "[*] Simulation finished" self.end_plc_process(self.scada_process) self.end_plc_process(self.plc2_process) self.end_plc_process(self.plc1_process) if self.simulation: self.simulation.terminate() cmd = shlex.split("./kill_cppo.sh") subprocess.call(cmd) net.stop() sys.exit(0)if __name__ == "__main__": topo = ScadaTopo() net = Mininet(topo=topo) minitown_cps = Minitown(name='minitown', net=net)
0 commit comments