]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
suricatasc: factorize command parsing
authorEric Leblond <eric@regit.org>
Sat, 6 Sep 2014 09:37:20 +0000 (11:37 +0200)
committerEric Leblond <eric@regit.org>
Wed, 17 Sep 2014 09:43:36 +0000 (11:43 +0200)
scripts/suricatasc/src/suricatasc.py

index 6f1d3269868a394a0bbe643479c82458e8d1faa8..d3babe188ea75dde952830a6194a0aa25d9b6ed5 100644 (file)
@@ -151,6 +151,46 @@ class SuricataSC:
     def close(self):
         self.socket.close()
 
+    def parse_command(self, command):
+        arguments = None
+        if command.split(' ', 2)[0] in self.cmd_list:
+            if "pcap-file " in command:
+                try:
+                    [cmd, filename, output] = command.split(' ', 2)
+                except:
+                    raise SuricataCommandException("Arguments to command '%s' is missing" % (command))
+                if cmd != "pcap-file":
+                    raise SuricataCommandException("Invalid command '%s'" % (command))
+                else:
+                    arguments = {}
+                    arguments["filename"] = filename
+                    arguments["output-dir"] = output
+            elif "iface-stat" in command:
+                try:
+                    [cmd, iface] = command.split(' ', 1)
+                except:
+                    raise SuricataCommandException("Unable to split command '%s'" % (command))
+                if cmd != "iface-stat":
+                    raise SuricataCommandException("Invalid command '%s'" % (command))
+                else:
+                    arguments = {}
+                    arguments["iface"] = iface
+            elif "conf-get" in command:
+                try:
+                    [cmd, variable] = command.split(' ', 1)
+                except:
+                    raise SuricataCommandException("Unable to split command '%s'" % (command))
+                if cmd != "conf-get":
+                    raise SuricataCommandException("Invalid command '%s'" % (command))
+                else:
+                    arguments = {}
+                    arguments["variable"] = variable
+            else:
+                cmd = command
+        else:
+            raise SuricataCommandException("Unknown command '%s'" % (command))
+        return (cmd, arguments)
+
     def interactive(self):
         print "Command list: " + ", ".join(self.cmd_list)
         try:
@@ -159,53 +199,13 @@ class SuricataSC:
             readline.parse_and_bind('tab: complete')
             while True:
                 command = raw_input(">>> ").strip()
-                arguments = None
-                if command.split(' ', 2)[0] in self.cmd_list:
-                    if command == "quit":
-                        break;
-                    if "pcap-file " in command:
-                        try:
-                            [cmd, filename, output] = command.split(' ', 2)
-                        except:
-                            print "Error: arguments to command '%s' is missing" % (command)
-                            continue
-                        if cmd != "pcap-file":
-                            print "Error: invalid command '%s'" % (command)
-                            continue
-                        else:
-                            arguments = {}
-                            arguments["filename"] = filename
-                            arguments["output-dir"] = output
-                    elif "iface-stat" in command:
-                        try:
-                            [cmd, iface] = command.split(' ', 1)
-                        except:
-                            print "Error: unable to split command '%s'" % (command)
-                            continue
-                        if cmd != "iface-stat":
-                            print "Error: invalid command '%s'" % (command)
-                            continue
-                        else:
-                            arguments = {}
-                            arguments["iface"] = iface
-                    elif "conf-get" in command:
-                        try:
-                            [cmd, variable] = command.split(' ', 1)
-                        except:
-                            print "Error: unable to split command '%s'" % (command)
-                            continue
-                        if cmd != "conf-get":
-                            print "Error: invalid command '%s'" % (command)
-                            continue
-                        else:
-                            arguments = {}
-                            arguments["variable"] = variable
-                    else:
-                        cmd = command
-                else:
-                    print "Error: unknown command '%s'" % (command)
+                if command == "quit":
+                    break;
+                try:
+                    (cmd, arguments) = self.parse_command(command)
+                except SuricataCommandException, err:
+                    print err
                     continue
-
                 cmdret = self.send_command(cmd, arguments)
                 #decode json message
                 if cmdret["return"] == "NOK":