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): def close(self):
self.socket.close() self.socket.close()
def interactive(self): def parse_command(self, command):
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()
arguments = None arguments = None
if command.split(' ', 2)[0] in self.cmd_list: if command.split(' ', 2)[0] in self.cmd_list:
if command == "quit":
break;
if "pcap-file " in command: if "pcap-file " in command:
try: try:
[cmd, filename, output] = command.split(' ', 2) [cmd, filename, output] = command.split(' ', 2)
except: except:
print "Error: arguments to command '%s' is missing" % (command) raise SuricataCommandException("Arguments to command '%s' is missing" % (command))
continue
if cmd != "pcap-file": if cmd != "pcap-file":
print "Error: invalid command '%s'" % (command) raise SuricataCommandException("Invalid command '%s'" % (command))
continue
else: else:
arguments = {} arguments = {}
arguments["filename"] = filename arguments["filename"] = filename
@ -180,11 +169,9 @@ class SuricataSC:
try: try:
[cmd, iface] = command.split(' ', 1) [cmd, iface] = command.split(' ', 1)
except: except:
print "Error: unable to split command '%s'" % (command) raise SuricataCommandException("Unable to split command '%s'" % (command))
continue
if cmd != "iface-stat": if cmd != "iface-stat":
print "Error: invalid command '%s'" % (command) raise SuricataCommandException("Invalid command '%s'" % (command))
continue
else: else:
arguments = {} arguments = {}
arguments["iface"] = iface arguments["iface"] = iface
@ -192,20 +179,33 @@ class SuricataSC:
try: try:
[cmd, variable] = command.split(' ', 1) [cmd, variable] = command.split(' ', 1)
except: except:
print "Error: unable to split command '%s'" % (command) raise SuricataCommandException("Unable to split command '%s'" % (command))
continue
if cmd != "conf-get": if cmd != "conf-get":
print "Error: invalid command '%s'" % (command) raise SuricataCommandException("Invalid command '%s'" % (command))
continue
else: else:
arguments = {} arguments = {}
arguments["variable"] = variable arguments["variable"] = variable
else: else:
cmd = command cmd = command
else: else:
print "Error: unknown command '%s'" % (command) raise SuricataCommandException("Unknown command '%s'" % (command))
continue 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) cmdret = self.send_command(cmd, arguments)
#decode json message #decode json message
if cmdret["return"] == "NOK": if cmdret["return"] == "NOK":

Loading…
Cancel
Save