File: namecheap-api-cli

package info (click to toggle)
namecheap 0.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 244 kB
  • sloc: python: 587; makefile: 6
file content (94 lines) | stat: -rwxr-xr-x 2,788 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3

import argparse
import sys
import os
from namecheap import Api, ApiError

cfgpath = os.path.expanduser("~/.namecheap_cfg")
cfg = '''api_key = '0123456789abcdef0123456789abcdef'
      username = 'myusername'
      ip_address = '10.0.0.1'
      '''

if os.path.isfile(cfgpath):
    pass
else:
    with open(cfgpath, 'w') as f:
        f.write(cfg)

try:
    sys.path.append(os.path.abspath(cfgpath))
except:
    pass


def get_args():
    parser = argparse.ArgumentParser(description="CLI tool to manage NameCheap.com domain records")

    parser.add_argument("--debug", action="store_true", help="If set, enables debug output")
    parser.add_argument("--sandbox", action="store_true", help="If set, forcing usage of Sandbox API, see README.md for details")

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("--add", action="store_true", help="Use to add a record")
    group.add_argument("--delete", action="store_true", help="Use to remove a record")
    group.add_argument("--list", action="store_true", help="List existing records")

    parser.add_argument("--domain", type=str, default="example.org", help="Domain to manage, default is \"example.org\", don't forget to override")

    parser.add_argument("--type", type=str, default="A", help="Record type, default is \"A\"")
    parser.add_argument("--name", type=str, default="test", help="Record name, default is \"test\"")
    parser.add_argument("--address", type=str, default="127.0.0.1", help="Address for record to point to, default is \"127.0.0.1\"")
    parser.add_argument("--ttl", type=int, default=300, help="Time-To-Live, in seconds, default is 300")

    args = parser.parse_args()

    return args


def list_records():
    return api.domains_dns_getHosts(domain)


def record_delete(hostname, address, record_type="A", ttl=300):
    record = {
        "Type": record_type,
        "Name": hostname,
        "Address": address,
        "TTL": str(ttl)
    }
    api.domains_dns_delHost(domain, record)


def record_add(record_type, hostname, address, ttl=300):
    record = {
        "Type": record_type,
        "Name": hostname,
        "Address": address,
        "TTL": str(ttl)
    }
    api.domains_dns_addHost(domain, record)

args = get_args()

domain = args.domain
print("domain: %s" % domain)

api = Api(username, api_key, username, ip_address, sandbox=args.sandbox, debug=args.debug)

if args.add:
    record_add(
        args.type,
        args.name,
        args.address,
        args.ttl
    )
elif args.delete:
    record_delete(
        args.name,
        args.address,
        args.type
    )
elif args.list:
    for line in list_records():
        print("\t%s \t%s\t%s -> %s" % (line["Type"], line["TTL"], line["Name"], line["Address"]))