2018-11-22 22:46:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
2022-10-25 10:58:05 +00:00
|
|
|
import json
|
2018-11-22 22:46:30 +00:00
|
|
|
import CloudFlare
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
HELP_DESCRIPTION='This is a simple utility for querying CloudFlare for DNS entries.'
|
|
|
|
HELP_EXAMPLE='Example: ./fqdns.py -d status.im -t CNAME'
|
|
|
|
|
|
|
|
def format_csv(record):
|
|
|
|
return '{};{};{};{}'.format(
|
|
|
|
record['type'],
|
|
|
|
record['name'],
|
|
|
|
record['content'],
|
|
|
|
record['proxied'],
|
|
|
|
)
|
|
|
|
|
|
|
|
def format_table(record):
|
2022-10-20 18:25:49 +00:00
|
|
|
return '{:>20} {:1} {:>6} {:30} {}'.format(
|
2019-02-15 19:27:34 +00:00
|
|
|
record['id'],
|
2019-02-16 13:38:03 +00:00
|
|
|
('P' if record['proxied'] else ''),
|
2018-11-22 22:46:30 +00:00
|
|
|
record['type'],
|
|
|
|
record['name'],
|
|
|
|
record['content'],
|
|
|
|
)
|
|
|
|
|
2022-10-25 10:58:05 +00:00
|
|
|
def format_json(record):
|
|
|
|
return json.dumps(
|
|
|
|
{k: record[k] for k in (
|
|
|
|
'id', 'proxied', 'type', 'name', 'content'
|
|
|
|
)}
|
|
|
|
)
|
|
|
|
|
2018-11-22 22:46:30 +00:00
|
|
|
def parse_opts():
|
|
|
|
parser = OptionParser(description=HELP_DESCRIPTION, epilog=HELP_EXAMPLE)
|
|
|
|
parser.add_option('-M', '--mail', dest='cf_email', default='jakub@status.im',
|
|
|
|
help='CloudFlare Account email for auth. (default: %default)')
|
|
|
|
parser.add_option('-T', '--token', dest='cf_token', default=os.environ['CF_TOKEN'],
|
|
|
|
help='CloudFlare API token for auth (env CF_TOKEN used). (default: %default)')
|
|
|
|
parser.add_option('-d', '--domain', dest='cf_domain', default='status.im',
|
|
|
|
help='Specify which domain to query for. (default: %default)')
|
2022-10-25 10:58:05 +00:00
|
|
|
parser.add_option('-t', '--type', type=str,
|
2018-11-22 22:46:30 +00:00
|
|
|
help='Type of DNS records to query for.')
|
|
|
|
parser.add_option('-c', '--csv', action='store_true',
|
|
|
|
help='Format records as a CSV file.')
|
2022-10-25 10:58:05 +00:00
|
|
|
parser.add_option('-j', '--json', action='store_true',
|
|
|
|
help='Format records as a CSV file.')
|
|
|
|
|
|
|
|
opts, args = parser.parse_args()
|
|
|
|
|
|
|
|
if opts.csv and opts.json:
|
|
|
|
parser.error('Options ---csv and --json are mutually exclusive.')
|
2018-11-22 22:46:30 +00:00
|
|
|
|
2022-10-25 10:58:05 +00:00
|
|
|
return opts, args
|
2018-11-22 22:46:30 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
(opts, args) = parse_opts()
|
|
|
|
|
|
|
|
cf = CloudFlare.CloudFlare(opts.cf_email, opts.cf_token)
|
|
|
|
|
|
|
|
zones = cf.zones.get(params={'per_page':100})
|
|
|
|
zone = next(z for z in zones if z['name'] == opts.cf_domain)
|
2018-11-22 23:59:12 +00:00
|
|
|
records = cf.zones.dns_records.get(zone['id'], params={'per_page': 1000})
|
2018-11-22 22:46:30 +00:00
|
|
|
|
|
|
|
formatter = format_table
|
|
|
|
if opts.csv:
|
|
|
|
formatter = format_csv
|
2022-10-25 10:58:05 +00:00
|
|
|
elif opts.json:
|
|
|
|
formatter = format_json
|
2018-11-22 22:46:30 +00:00
|
|
|
|
|
|
|
for r in records:
|
2022-10-20 18:25:49 +00:00
|
|
|
if opts.type and r['type'] != opts.type:
|
|
|
|
continue
|
2018-11-22 22:46:30 +00:00
|
|
|
print(formatter(r))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|