Files
SnarfCode/scripts/run_db_update.py

99 lines
4.3 KiB
Python

import paramiko
import sys
sys.stdout.reconfigure(encoding='utf-8')
key_path = r"C:\Users\Snarf\.ssh\id_ed25519_snarfnet"
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("140.44.4.71", username="root", key_filename=key_path, timeout=10)
remote_script = """import sqlite3, json, datetime
conn = sqlite3.connect("/var/lib/docker/volumes/nginx-proxy-manager_nginx-data/_data/database.sqlite")
cur = conn.cursor()
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
updates = [
(3, json.dumps(["pihole.snarfnet.net"]), "140.44.4.34", 443, "https"),
(43, json.dumps(["pihole2.snarfnet.net"]), "140.44.4.62", 443, "https"),
(51, json.dumps(["pihole3.snarfnet.net"]), "140.44.4.62", 443, "https"),
(52, json.dumps(["dns1.snarfnet.net"]), "140.44.4.34", 443, "https"),
(53, json.dumps(["dns2.snarfnet.net"]), "140.44.4.62", 443, "https"),
]
for pid, dnames, host, port, scheme in updates:
cur.execute('''UPDATE proxy_host
SET domain_names = ?, forward_host = ?, forward_port = ?, forward_scheme = ?,
certificate_id = 1, ssl_forced = 1, allow_websocket_upgrade = 1, http2_support = 1,
hsts_enabled = 1, hsts_subdomains = 1, advanced_config = "", modified_on = ?
WHERE id = ?''', (dnames, host, port, scheme, now, pid))
print(f"Updated proxy host {pid} -> {host}:{port} ({scheme})")
meta_json = json.dumps({"nginx_online": True, "nginx_err": None})
cur.execute("SELECT id FROM proxy_host WHERE id = 54")
if not cur.fetchone():
cur.execute('''INSERT INTO proxy_host (
id, created_on, modified_on, owner_user_id, is_deleted,
domain_names, forward_host, forward_port, access_list_id,
certificate_id, ssl_forced, caching_enabled, block_exploits,
advanced_config, meta, allow_websocket_upgrade, http2_support,
forward_scheme, enabled, locations, hsts_enabled, hsts_subdomains,
trust_forwarded_proto
) VALUES (
54, ?, ?, 1, 0,
?, "140.44.4.34", 443, 0,
1, 1, 0, 1,
"", ?, 1, 1,
"https", 1, "[]", 1, 1,
0
)''', (now, now, json.dumps(["technitium.snarfnet.net"]), meta_json))
print("Inserted proxy host 54: technitium.snarfnet.net -> 140.44.4.34:443")
else:
cur.execute('''UPDATE proxy_host
SET domain_names = ?, forward_host = "140.44.4.34", forward_port = 443, forward_scheme = "https",
certificate_id = 1, ssl_forced = 1, allow_websocket_upgrade = 1, http2_support = 1,
hsts_enabled = 1, hsts_subdomains = 1, modified_on = ?
WHERE id = 54''', (json.dumps(["technitium.snarfnet.net"]), now))
print("Updated proxy host 54: technitium.snarfnet.net")
cur.execute("SELECT id FROM proxy_host WHERE id = 55")
if not cur.fetchone():
cur.execute('''INSERT INTO proxy_host (
id, created_on, modified_on, owner_user_id, is_deleted,
domain_names, forward_host, forward_port, access_list_id,
certificate_id, ssl_forced, caching_enabled, block_exploits,
advanced_config, meta, allow_websocket_upgrade, http2_support,
forward_scheme, enabled, locations, hsts_enabled, hsts_subdomains,
trust_forwarded_proto
) VALUES (
55, ?, ?, 1, 0,
?, "140.44.4.34", 443, 0,
1, 1, 0, 1,
"", ?, 1, 1,
"https", 1, "[]", 1, 1,
0
)''', (now, now, json.dumps(["pihole1.snarfnet.net"]), meta_json))
print("Inserted proxy host 55: pihole1.snarfnet.net -> 140.44.4.34:443")
else:
cur.execute('''UPDATE proxy_host
SET domain_names = ?, forward_host = "140.44.4.34", forward_port = 443, forward_scheme = "https",
certificate_id = 1, ssl_forced = 1, allow_websocket_upgrade = 1, http2_support = 1,
hsts_enabled = 1, hsts_subdomains = 1, modified_on = ?
WHERE id = 55''', (json.dumps(["pihole1.snarfnet.net"]), now))
print("Updated proxy host 55: pihole1.snarfnet.net")
conn.commit()
conn.close()
"""
# Write script to /tmp/update_npm_db.py
cmd = "cat << 'EOF' > /tmp/update_npm_db.py\n" + remote_script + "\nEOF\npython3 /tmp/update_npm_db.py\n"
stdin, stdout, stderr = c.exec_command(cmd)
print("Output:\n", stdout.read().decode('utf-8'))
err = stderr.read().decode('utf-8')
if err: print("Err:\n", err)
c.close()