suricatasc: factorize command parsing

pull/1122/head
Eric Leblond 12 years ago
parent 61a9739f44
commit 1b9cc03653

@ -151,27 +151,16 @@ class SuricataSC:
def close(self):
self.socket.close()
def interactive(self):
print "Command list: " + ", ".join(self.cmd_list)
try:
readline.set_completer(SuricataCompleter(self.cmd_list))
readline.set_completer_delims(";")
readline.parse_and_bind('tab: complete')
while True:
command = raw_input(">>> ").strip()
def parse_command(self, command):
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
raise SuricataCommandException("Arguments to command '%s' is missing" % (command))
if cmd != "pcap-file":
print "Error: invalid command '%s'" % (command)
continue
raise SuricataCommandException("Invalid command '%s'" % (command))
else:
arguments = {}
arguments["filename"] = filename
@ -180,11 +169,9 @@ class SuricataSC:
try:
[cmd, iface] = command.split(' ', 1)
except:
print "Error: unable to split command '%s'" % (command)
continue
raise SuricataCommandException("Unable to split command '%s'" % (command))
if cmd != "iface-stat":
print "Error: invalid command '%s'" % (command)
continue
raise SuricataCommandException("Invalid command '%s'" % (command))
else:
arguments = {}
arguments["iface"] = iface
@ -192,20 +179,33 @@ class SuricataSC:
try:
[cmd, variable] = command.split(' ', 1)
except:
print "Error: unable to split command '%s'" % (command)
continue
raise SuricataCommandException("Unable to split command '%s'" % (command))
if cmd != "conf-get":
print "Error: invalid command '%s'" % (command)
continue
raise SuricataCommandException("Invalid command '%s'" % (command))
else:
arguments = {}
arguments["variable"] = variable
else:
cmd = command
else:
print "Error: unknown command '%s'" % (command)
continue
raise SuricataCommandException("Unknown command '%s'" % (command))
return (cmd, arguments)
def interactive(self):
print "Command list: " + ", ".join(self.cmd_list)
try:
readline.set_completer(SuricataCompleter(self.cmd_list))
readline.set_completer_delims(";")
readline.parse_and_bind('tab: complete')
while True:
command = raw_input(">>> ").strip()
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":

Loading…
Cancel
Save