forked from jantman/misc-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinode_list_records.py
executable file
·47 lines (39 loc) · 1.64 KB
/
linode_list_records.py
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
#!/usr/bin/env python
#
# Simple script to list all records in Linode DNS via API,
# along with their Domain ID and Record ID
#
# This requires the requests and json packages.
#
##################
# Copyright 2013 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
# Free for any use provided that patches are submitted back to me.
#
# The latest version of this script can be found at:
# <https://github.com/jantman/misc-scripts/blob/master/linode_list_records.py>
##########################################################################################
import sys
import requests
import json
if len(sys.argv) < 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
print("USAGE: linode_list_records.py <API Key>")
sys.exit(1)
API_Key = sys.argv[1]
sys.stderr.write("Using API Key: %s\n" % API_Key)
URL_BASE = "https://api.linode.com/?api_key=%s" % API_Key
r = requests.get("%s&api_action=domain.list" % URL_BASE)
if r.status_code != 200:
sys.stderr.write("ERROR: API Request for domain.list failed with HTTP code %s\n" % r.status_code)
sys.exit(2)
domains = r.json()
print("domain,resource,type,DomainID,ResourceID")
for domain in domains['DATA']:
d_name = domain['DOMAIN']
d_id = domain['DOMAINID']
r = requests.get("%s&api_action=domain.resource.list&DomainID=%d" % (URL_BASE, d_id))
if r.status_code != 200:
sys.stderr.write("ERROR: API Request for domain.resource.list with DomainID %d failed with HTTP code %s\n" % (d_id, r.status_code))
sys.exit(2)
resources = r.json()
for res in resources['DATA']:
print ("%s,%s,%s,%d,%d" % (d_name, res['NAME'], res['TYPE'], d_id, res['RESOURCEID']))