Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.project
.pydevproject
37 changes: 23 additions & 14 deletions dnsdb_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os
import sys
import time
import urllib2
import requests
from cStringIO import StringIO

try:
Expand All @@ -37,11 +37,12 @@
locale.setlocale(locale.LC_ALL, '')

class DnsdbClient(object):
def __init__(self, server, apikey, limit=None, json=False):
def __init__(self, server, apikey, limit=None, json=False, verify=False):
self.server = server
self.apikey = apikey
self.limit = limit
self.json = json
self.verify = verify

def query_rrset(self, oname, rrtype=None, bailiwick=None):
if bailiwick:
Expand All @@ -68,23 +69,23 @@ def query_rdata_ip(self, rdata_ip):
def _query(self, path):
res = []
url = '%s/lookup/%s' % (self.server, path)
headers = {'Accept': 'application/json', 'X-Api-Key': self.apikey}
if self.limit:
url += '?limit=%d' % self.limit
req = urllib2.Request(url)
req.add_header('Accept', 'application/json')
req.add_header('X-Api-Key', self.apikey)
try:
http = urllib2.urlopen(req)
while True:
line = http.readline()
params = '?limit=%d' % self.limit
else:
params = ""
response = requests.get(url, params=params, headers=headers, verify=self.verify)
if response.status_code != 200:
sys.stderr.write(str(response.status_code) + " " + response.reason + "\n")
return res
else:
for line in response.iter_lines():
if not line:
break
if self.json:
res.append(line)
else:
res.append(json.loads(line))
except urllib2.HTTPError, e:
sys.stderr.write(str(e) + '\n')
return res

def sec_to_text(ts):
Expand Down Expand Up @@ -219,8 +220,16 @@ def main():
if not 'APIKEY' in cfg:
sys.stderr.write('dnsdb_query: APIKEY not defined in config file\n')
sys.exit(1)

client = DnsdbClient(cfg['DNSDB_SERVER'], cfg['APIKEY'], options.limit, options.json)
if not 'VERIFY' in cfg:
cfg['VERIFY'] = False
elif cfg['VERIFY'].lower() in ['true', '1', 't', 'y', 'yes']:
cfg['VERIFY'] = True
if 'CA_BUNDLE' in cfg:
cfg['VERIFY'] = cfg['CA_BUNDLE']
else:
cfg['VERIFY'] = False

client = DnsdbClient(cfg['DNSDB_SERVER'], cfg['APIKEY'], options.limit, options.json, cfg['VERIFY'])
if options.rrset:
res_list = client.query_rrset(*options.rrset.split('/'))
fmt_func = rrset_to_text
Expand Down