Skip to content

Instantly share code, notes, and snippets.

@yunginnanet
Created August 14, 2024 01:36
Show Gist options
  • Save yunginnanet/ac5aadfbffc2961a3df137282dd0096e to your computer and use it in GitHub Desktop.
Save yunginnanet/ac5aadfbffc2961a3df137282dd0096e to your computer and use it in GitHub Desktop.
import sqlite3
import os
import shutil
def merge_databases(db1, db2):
if not os.path.exists(db1):
print(db1 + " does not exist, copying " + db2 + "...")
try:
shutil.copyfile(db2, db1)
except Exception as e:
print("FATAL: " + e.__str__())
exit(1)
print("Merging " + db2 + " into " + db1)
con3 = sqlite3.connect(db2)
c = con3.execute("ATTACH '" + db1 + "' as dba")
try:
c.execute("BEGIN")
for row in con3.execute("SELECT * FROM dba.sqlite_master WHERE type='table'"):
try:
combine = "INSERT OR IGNORE INTO dba."+ row[1] + " SELECT * FROM " + row[1]
print(combine)
c.execute(combine)
except Exception as e:
print(e)
exit(1)
except sqlite3.Error as e:
print("FAILED: " + e.__str__())
exit(1)
finally:
c.close()
print("fin, closing")
con3.commit()
con3.close()
def read_files(directory):
fname = []
print("Reading files from " + directory)
for f in os.listdir(directory):
c_name = os.path.join(directory, f)
_, file_extension = os.path.splitext(c_name)
if (file_extension == '.kismet'):
print("found kismet file: " + c_name)
if os.path.exists(c_name.replace('.kismet','.kismet-jounal')):
print("skipping in-use kismet database: " + c_name)
continue
fname.append(c_name)
return fname
def batch_merge(target, directory):
db_files = read_files(directory)
for db_file in db_files[1:]:
merge_databases(target, db_file)
if __name__ == '__main__':
batch_merge('./merged.sqlite3', '/media/data')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment