Add DNS record management module for Mail-in-a-Box
This commit is contained in:
18
delete-record.py
Executable file
18
delete-record.py
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import miab.dns as miab_dns
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="Delete a DNS record in Mail-in-a-Box")
|
||||||
|
parser.add_argument("name", type=str, help="The name of the DNS record to delete")
|
||||||
|
parser.add_argument("type", type=str, help="The type of the DNS record to delete (e.g., A, CNAME, MX)")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
miab = miab_dns.MailInABox()
|
||||||
|
response = miab.delete_record(name=args.name, type=args.type)
|
||||||
|
if response == "OK":
|
||||||
|
print("Record deleted successfully.")
|
||||||
|
else:
|
||||||
|
print("Failed to delete record.")
|
||||||
13
list-records.py
Executable file
13
list-records.py
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import miab.dns as miab_dns
|
||||||
|
import json
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="List DNS records in Mail-in-a-Box")
|
||||||
|
parser.add_argument("--type", type=str, help="Filter records by type (e.g., A, CNAME, MX)")
|
||||||
|
args = parser.parse_args()
|
||||||
|
records = miab_dns.list_records(record_type=args.type)
|
||||||
|
for record in records:
|
||||||
|
print(json.dumps(record, indent=2))
|
||||||
7
miab/__init__.py
Normal file
7
miab/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Initializer for helpers package
|
||||||
|
|
||||||
|
from os import getenv
|
||||||
|
from base64 import b64encode
|
||||||
|
|
||||||
|
MIAB_HOST = getenv('MIAB_HOST', 'box.koszewscy.waw.pl')
|
||||||
|
MIAB_AUTH_HEADER = "Basic " + b64encode(f"{getenv('MIAB_USERNAME', 'admin')}:{getenv('MIAB_PASSWORD', 'password')}".encode()).decode()
|
||||||
48
miab/dns.py
Normal file
48
miab/dns.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
from . import MIAB_HOST, MIAB_AUTH_HEADER
|
||||||
|
from requests import get, put, delete
|
||||||
|
import jmespath
|
||||||
|
|
||||||
|
def list_records(record_type: str = None):
|
||||||
|
response = get(
|
||||||
|
f"https://{MIAB_HOST}/admin/dns/custom",
|
||||||
|
headers={"Authorization": MIAB_AUTH_HEADER}
|
||||||
|
)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
records = response.json()
|
||||||
|
if record_type:
|
||||||
|
jmespath_expr = f"[?rtype=='{record_type.upper()}']"
|
||||||
|
else:
|
||||||
|
jmespath_expr = "[]"
|
||||||
|
|
||||||
|
return jmespath.search(jmespath_expr + ".{name: qname, type: rtype, value: value}", records)
|
||||||
|
else:
|
||||||
|
raise Exception(f"Failed to retrieve DNS records: {response.status_code} {response.text}")
|
||||||
|
|
||||||
|
def set_record(name: str, value: str, type: str = "A"):
|
||||||
|
response = put(
|
||||||
|
f"https://{MIAB_HOST}/admin/dns/custom/{name}/{type.upper()}",
|
||||||
|
headers={
|
||||||
|
"Authorization": MIAB_AUTH_HEADER,
|
||||||
|
"Content-Type": "text/plain"
|
||||||
|
},
|
||||||
|
data=value
|
||||||
|
)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.text
|
||||||
|
else:
|
||||||
|
raise Exception(f"Failed to set DNS record: {response.status_code} {response.text}")
|
||||||
|
|
||||||
|
def delete_record(name: str, type: str):
|
||||||
|
response = delete(
|
||||||
|
f"https://{MIAB_HOST}/admin/dns/custom/{name}/{type}",
|
||||||
|
headers={
|
||||||
|
"Authorization": MIAB_AUTH_HEADER,
|
||||||
|
"Content-Type": "text/plain"
|
||||||
|
},
|
||||||
|
data=""
|
||||||
|
)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.text
|
||||||
|
else:
|
||||||
|
raise Exception(f"Failed to delete DNS record: {response.status_code} {response.text}")
|
||||||
19
set-record.py
Executable file
19
set-record.py
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import miab.dns as miab_dns
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="Set a DNS record in Mail-in-a-Box")
|
||||||
|
parser.add_argument("name", type=str, help="The name of the DNS record to set")
|
||||||
|
parser.add_argument("value", type=str, help="The value of the DNS record to set")
|
||||||
|
parser.add_argument("--type", type=str, default="A", help="The type of the DNS record (default: A)")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
miab = miab_dns.MailInABox()
|
||||||
|
response = miab.set_record(name=args.name, value=args.value, type=args.type)
|
||||||
|
if response == "OK":
|
||||||
|
print("Record set successfully.")
|
||||||
|
else:
|
||||||
|
print("Failed to set record.")
|
||||||
Reference in New Issue
Block a user