openc2lib.actuators.SQLDatabase
1import sqlite3 2 3class SQLDatabase: 4 def __init__(self, db_name): 5 self.db_name = db_name 6 self.db_path = 'openc2_commands.db' 7 8 def init_db(self): 9 conn = sqlite3.connect(self.db_path) 10 c = conn.cursor() 11 c.execute('''CREATE TABLE IF NOT EXISTS commands (rule_number INTEGER PRIMARY KEY, command TEXT)''') 12 conn.commit() 13 conn.close() 14 15 def get_command_from_rule_number(self, rule_number): 16 try: 17 conn = sqlite3.connect(self.db_path) 18 c = conn.cursor() 19 c.execute('SELECT command FROM commands WHERE rule_number = ?', (rule_number,)) 20 db_result = c.fetchone() 21 conn.close() 22 return db_result 23 except: 24 return None 25 26 def delete_command_by_rule_number(self, rule_number): 27 try: 28 conn = sqlite3.connect(self.db_path) 29 c = conn.cursor() 30 c.execute('DELETE FROM commands WHERE rule_number = ?', (rule_number,)) 31 conn.commit() 32 conn.close() 33 return 0 34 except: 35 return -1 36 37 def insert_command(self, iptables_command, rule_number=None): 38 try: 39 conn = sqlite3.connect(self.db_path) 40 c = conn.cursor() 41 if rule_number is None: 42 c.execute('INSERT INTO commands (command) VALUES (?)', (iptables_command,)) 43 else: 44 c.execute('INSERT INTO commands (rule_number, command) VALUES (?, ?)', (rule_number, iptables_command)) 45 rule_number = c.lastrowid 46 conn.commit() 47 conn.close() 48 except: 49 return -1 50 51 return rule_number 52 53 def update_command_by_rule_number(self, rule_number, new_command): 54 conn = sqlite3.connect(self.db_path) 55 c = conn.cursor() 56 c.execute('UPDATE commands SET command = ? WHERE rule_number = ?', (new_command, rule_number)) 57 conn.commit() 58 conn.close()
class
SQLDatabase:
4class SQLDatabase: 5 def __init__(self, db_name): 6 self.db_name = db_name 7 self.db_path = 'openc2_commands.db' 8 9 def init_db(self): 10 conn = sqlite3.connect(self.db_path) 11 c = conn.cursor() 12 c.execute('''CREATE TABLE IF NOT EXISTS commands (rule_number INTEGER PRIMARY KEY, command TEXT)''') 13 conn.commit() 14 conn.close() 15 16 def get_command_from_rule_number(self, rule_number): 17 try: 18 conn = sqlite3.connect(self.db_path) 19 c = conn.cursor() 20 c.execute('SELECT command FROM commands WHERE rule_number = ?', (rule_number,)) 21 db_result = c.fetchone() 22 conn.close() 23 return db_result 24 except: 25 return None 26 27 def delete_command_by_rule_number(self, rule_number): 28 try: 29 conn = sqlite3.connect(self.db_path) 30 c = conn.cursor() 31 c.execute('DELETE FROM commands WHERE rule_number = ?', (rule_number,)) 32 conn.commit() 33 conn.close() 34 return 0 35 except: 36 return -1 37 38 def insert_command(self, iptables_command, rule_number=None): 39 try: 40 conn = sqlite3.connect(self.db_path) 41 c = conn.cursor() 42 if rule_number is None: 43 c.execute('INSERT INTO commands (command) VALUES (?)', (iptables_command,)) 44 else: 45 c.execute('INSERT INTO commands (rule_number, command) VALUES (?, ?)', (rule_number, iptables_command)) 46 rule_number = c.lastrowid 47 conn.commit() 48 conn.close() 49 except: 50 return -1 51 52 return rule_number 53 54 def update_command_by_rule_number(self, rule_number, new_command): 55 conn = sqlite3.connect(self.db_path) 56 c = conn.cursor() 57 c.execute('UPDATE commands SET command = ? WHERE rule_number = ?', (new_command, rule_number)) 58 conn.commit() 59 conn.close()
def
insert_command(self, iptables_command, rule_number=None):
38 def insert_command(self, iptables_command, rule_number=None): 39 try: 40 conn = sqlite3.connect(self.db_path) 41 c = conn.cursor() 42 if rule_number is None: 43 c.execute('INSERT INTO commands (command) VALUES (?)', (iptables_command,)) 44 else: 45 c.execute('INSERT INTO commands (rule_number, command) VALUES (?, ?)', (rule_number, iptables_command)) 46 rule_number = c.lastrowid 47 conn.commit() 48 conn.close() 49 except: 50 return -1 51 52 return rule_number