def __init__(self): Cmd.__init__(self) self.prompt = "hephaestus> " self.intro = ( "\n" + ruler("*") + "WELCOME to hephaestus\n" + ruler("*") + "hephaestus is the stateless firewall configuration parser for JunOS.\nGet help with 'help (<cmd>)' or '?', or issue 'userguide' to get started.\n" + ruler("*") + "\n" ) self.myconfig = Baseconfig() self.configinterpreter = Interpreter_config() self.configinterpreter.myconfig = self.myconfig self.scaninterpreter = Interpreter_scan() self.scaninterpreter.myconfig = self.myconfig self.updateinterpreter = Interpreter_update() self.updateinterpreter.myconfig = self.myconfig self.queryinterpreter = Interpreter_query() self.queryinterpreter.myconfig = self.myconfig self.resultsinterpreter = Interpreter_results() self.resultsinterpreter.myconfig = self.myconfig
class Interpreter(Cmd): """Interpreter class based on the 'cmd' library.""" def __init__(self): Cmd.__init__(self) self.prompt = "hephaestus> " self.intro = ( "\n" + ruler("*") + "WELCOME to hephaestus\n" + ruler("*") + "hephaestus is the stateless firewall configuration parser for JunOS.\nGet help with 'help (<cmd>)' or '?', or issue 'userguide' to get started.\n" + ruler("*") + "\n" ) self.myconfig = Baseconfig() self.configinterpreter = Interpreter_config() self.configinterpreter.myconfig = self.myconfig self.scaninterpreter = Interpreter_scan() self.scaninterpreter.myconfig = self.myconfig self.updateinterpreter = Interpreter_update() self.updateinterpreter.myconfig = self.myconfig self.queryinterpreter = Interpreter_query() self.queryinterpreter.myconfig = self.myconfig self.resultsinterpreter = Interpreter_results() self.resultsinterpreter.myconfig = self.myconfig def setconfig(self, cfg): self.myconfig = cfg def emptyline(self): pass def do_shell(self, line): "Run a shell command" print("\nRunning shell command: " + line + "\n") output = os.popen(line).read() print(output) self.last_output = output def do_exit(self, line): return True def help_exit(self): print("\nExit this program.\n") def help_help(self): print("\nOverride worked...\n") def help_shell(self): print("\nOverride worked...\n") # interesting functions def do_userguide(self, line): """ A punchy, pithy user guide will go here soon. For now: Basics: 1. Enter Configuration mode, load a file, show contents, exit config > load > show > exit 2. Enter Scan mode, autoscan the network scan > autoscan 3. Enter Update mode, update local cache of router configs update > update 4. Enter Query mode, run query query > query 5. Enter Results mode, view results results > summary results > diagram results > detail """ self.help_userguide() def help_userguide(self): """Print help on do_userguide() method""" print print(inspect.getdoc(self.do_userguide)) print def do_config(self, line): self.configinterpreter.cmdloop() def help_config(self): print( "\nBefore you can scan a network or run any queries, you must first enter configuration to enter the necessary AS/network information.\n" ) def do_scan(self, line): self.scaninterpreter.cmdloop() def help_scan(self): print("\nScan the configured network and identify any routers present.\n") def do_update(self, line): self.updateinterpreter.cmdloop() def help_update(self): print("\nFetch configuration from the identified routers.\n") def do_query(self, line): self.queryinterpreter.cmdloop() def help_query(self): print( "\nRun a query against the network state to determine if a given flow will be accepted, rejected or discarded.\n" ) def do_results(self, line): self.resultsinterpreter.cmdloop() def help_results(self): print("\nView results in more detail.\n")