Skip to content

Instantly share code, notes, and snippets.

@jvanasco
Last active June 27, 2024 21:24
Show Gist options
  • Select an option

  • Save jvanasco/253a8cd0c527cc2e4a6112602d46556f to your computer and use it in GitHub Desktop.

Select an option

Save jvanasco/253a8cd0c527cc2e4a6112602d46556f to your computer and use it in GitHub Desktop.

Revisions

  1. jvanasco revised this gist Jun 27, 2024. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions replace_domain.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    """
    This is deprecated.
    It is now maintained as part of peter_sslers:
    * https://github.com/aptise/peter_sslers/blob/main/tools/replace_domain.py
    """
    from __future__ import print_function

    import os
  2. jvanasco created this gist Sep 4, 2020.
    50 changes: 50 additions & 0 deletions replace_domain.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    from __future__ import print_function

    import os
    import sqlite3
    import sys
    import re

    _args = sys.argv
    try:
    if len(_args) != 3:
    raise ValueError("wrong number of args")
    (_subdomain_old, _subdomain_new) = _args[1:3]
    if not all((_subdomain_old, _subdomain_new)):
    raise ValueError("Missing old or new subdomain")
    # validate the domain inputs
    _regex_subdomain = re.compile("^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$")
    if not _regex_subdomain.match(_subdomain_old):
    raise ValueError("Invalid domain: old")
    if not _regex_subdomain.match(_subdomain_new):
    raise ValueError("Invalid domain: new")

    except Exception as exc:
    print("Please invoke this as `replace_domain.py {OLD_DOMAIN} {NEW_DOMAIN}`")
    raise

    _database_path = os.environ.get("ACMEDNS_DB", "acme-dns.db")
    print("Using acme-dns database at: %s" % _database_path)
    if not os.path.exists(_database_path):
    raise ValueError(
    "XXX Invalid Database Path. Please override with `ACMEDNS_DB=` environment variable"
    )

    with sqlite3.connect(_database_path) as connection:
    cursor = connection.cursor()

    cursor.execute("SELECT * FROM records WHERE subdomain=?", (_subdomain_old,))
    row = cursor.fetchone()
    if row is None:
    raise ValueError("Old Subdomain not found in acme-dns")

    print("updating the database...")
    cursor.execute(
    "UPDATE records SET subdomain=? WHERE subdomain=?",
    (_subdomain_new, _subdomain_old),
    )
    cursor.execute(
    "UPDATE txt SET subdomain=? WHERE subdomain=?", (_subdomain_new, _subdomain_old)
    )
    connection.commit()
    print("done!")