77 lines
2.5 KiB
Python
77 lines
2.5 KiB
Python
|
import argparse
|
||
|
import mysql.connector
|
||
|
import configparser
|
||
|
import subprocess
|
||
|
|
||
|
def main():
|
||
|
"""
|
||
|
Entry point of the script.
|
||
|
"""
|
||
|
# Set up the argument parser
|
||
|
parser = argparse.ArgumentParser()
|
||
|
parser.add_argument('-c', '--config', help='path to the configuration file', default="config.ini")
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
# Read the MariaDB server details from the configuration file
|
||
|
config = configparser.ConfigParser()
|
||
|
config.read(args.config)
|
||
|
|
||
|
# Connect to the database
|
||
|
conn = mysql.connector.connect(
|
||
|
host=config['database']['host'],
|
||
|
user=config['database']['user'],
|
||
|
password=config['database']['password'],
|
||
|
database=config['database']['database']
|
||
|
)
|
||
|
cursor = conn.cursor()
|
||
|
|
||
|
# Create the table if it doesn't already exist
|
||
|
cursor.execute('''
|
||
|
CREATE TABLE IF NOT EXISTS onion_keys (hostname text, public_key text, secret_key text)
|
||
|
''')
|
||
|
|
||
|
# Start mkp224o
|
||
|
process = subprocess.Popen(['mkp224o', '-y', '-f', 'filter'], stdout=subprocess.PIPE)
|
||
|
|
||
|
# Run mkp224o until the user interrupts the script
|
||
|
while True:
|
||
|
try:
|
||
|
# Read the output from mkp224o in blocks of four lines
|
||
|
hostname = None
|
||
|
public_key = None
|
||
|
secret_key = None
|
||
|
while True:
|
||
|
line = process.stdout.readline().decode().strip()
|
||
|
if line == '---':
|
||
|
break
|
||
|
|
||
|
# Split the line into fields
|
||
|
fields = line.split(':', 1)
|
||
|
if len(fields) != 2:
|
||
|
continue
|
||
|
key, value = fields
|
||
|
key = key.strip()
|
||
|
value = value.strip()
|
||
|
|
||
|
# Extract the hostname, public key, and secret key from the output
|
||
|
if key == 'hostname':
|
||
|
hostname = value
|
||
|
elif key == 'hs_ed25519_public_key':
|
||
|
public_key = value
|
||
|
elif key == 'hs_ed25519_secret_key':
|
||
|
secret_key = value
|
||
|
|
||
|
if hostname:
|
||
|
# Insert the data into the database
|
||
|
cursor.execute('''
|
||
|
INSERT INTO onion_keys (hostname, public_key, secret_key) VALUES (%s, %s, %s)
|
||
|
''', (hostname, public_key, secret_key))
|
||
|
|
||
|
# Save the changes to the database
|
||
|
conn.commit()
|
||
|
except KeyboardInterrupt:
|
||
|
# If the user interrupts the script, break out of the loop
|
||
|
break
|
||
|
|
||
|
main()
|