| •1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import argparse |
| •4 |
import sys |
| 5 |
import os |
| 6 |
from namecheap import Api, ApiError |
| 7 |
|
| •8 |
cfgpath = os.path.expanduser("~/.namecheap_cfg") |
| 9 |
cfg = '''api_key = '0123456789abcdef0123456789abcdef' |
| 10 |
username = 'myusername' |
| 11 |
ip_address = '10.0.0.1' |
| 12 |
''' |
| 13 |
|
| 14 |
if os.path.isfile(cfgpath): |
| 15 |
pass |
| 16 |
else: |
| 17 |
with open(cfgpath, 'w') as f: |
| 18 |
f.write(cfg) |
| 19 |
|
| 20 |
try: |
| •21 |
sys.path.append(os.path.abspath(cfgpath)) |
| 22 |
except: |
| 23 |
pass |
| 24 |
|
| 25 |
|
| 26 |
def get_args(): |
| 27 |
parser = argparse.ArgumentParser(description="CLI tool to manage NameCheap.com domain records") |
| 28 |
|
| 29 |
parser.add_argument("--debug", action="store_true", help="If set, enables debug output") |
| 30 |
parser.add_argument("--sandbox", action="store_true", help="If set, forcing usage of Sandbox API, see README.md for details") |
| 31 |
|
| 32 |
group = parser.add_mutually_exclusive_group(required=True) |
| 33 |
group.add_argument("--add", action="store_true", help="Use to add a record") |
| 34 |
group.add_argument("--delete", action="store_true", help="Use to remove a record") |
| 35 |
group.add_argument("--list", action="store_true", help="List existing records") |
| 36 |
|
| 37 |
parser.add_argument("--domain", type=str, default="example.org", help="Domain to manage, default is \"example.org\", don't forget to override") |
| 38 |
|
| 39 |
parser.add_argument("--type", type=str, default="A", help="Record type, default is \"A\"") |
| 40 |
parser.add_argument("--name", type=str, default="test", help="Record name, default is \"test\"") |
| 41 |
parser.add_argument("--address", type=str, default="127.0.0.1", help="Address for record to point to, default is \"127.0.0.1\"") |
| 42 |
parser.add_argument("--ttl", type=int, default=300, help="Time-To-Live, in seconds, default is 300") |
| 43 |
|
| 44 |
args = parser.parse_args() |
| 45 |
|
| 46 |
return args |
| 47 |
|
| 48 |
|
| 49 |
def list_records(): |
| 50 |
return api.domains_dns_getHosts(domain) |
| 51 |
|
| 52 |
|
| 53 |
def record_delete(hostname, address, record_type="A", ttl=300): |
| 54 |
record = { |
| 55 |
"Type": record_type, |
| 56 |
"Name": hostname, |
| 57 |
"Address": address, |
| 58 |
"TTL": str(ttl) |
| 59 |
} |
| 60 |
api.domains_dns_delHost(domain, record) |
| 61 |
|
| 62 |
|
| 63 |
def record_add(record_type, hostname, address, ttl=300): |
| 64 |
record = { |
| 65 |
"Type": record_type, |
| 66 |
"Name": hostname, |
| 67 |
"Address": address, |
| 68 |
"TTL": str(ttl) |
| 69 |
} |
| 70 |
api.domains_dns_addHost(domain, record) |
| 71 |
|
| 72 |
args = get_args() |
| 73 |
|
| 74 |
domain = args.domain |
| 75 |
print("domain: %s" % domain) |
| 76 |
|
| 77 |
api = Api(username, api_key, username, ip_address, sandbox=args.sandbox, debug=args.debug) |
| 78 |
|
| 79 |
if args.add: |
| 80 |
record_add( |
| 81 |
args.type, |
| 82 |
args.name, |
| 83 |
args.address, |
| 84 |
args.ttl |
| 85 |
) |
| 86 |
elif args.delete: |
| 87 |
record_delete( |
| 88 |
args.name, |
| 89 |
args.address, |
| 90 |
args.type |
| 91 |
) |
| 92 |
elif args.list: |
| 93 |
for line in list_records(): |
| 94 |
print("\t%s \t%s\t%s -> %s" % (line["Type"], line["TTL"], line["Name"], line["Address"])) |