-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…Kuhnert
- Loading branch information
1 parent
2d07b78
commit 3e2c18c
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python3 | ||
from re import findall | ||
from subprocess import check_output | ||
|
||
|
||
def __query(domain, limit=100): | ||
"""Using the shell script to query pdns.cert.at is a hack, but python raises an error every time using subprocess | ||
functions to call whois. So this hack is avoiding calling whois directly. Ugly, but works. | ||
:param domain: The domain pdns is queried with. | ||
:type domain: str | ||
:param limit: Maximum number of results | ||
:type limit: int | ||
:returns: str -- Console output from whois call. | ||
:rtype: str | ||
""" | ||
s = check_output(['./whois.sh', '--limit {} {}'.format(limit, domain)], universal_newlines=True) | ||
return s | ||
|
||
|
||
def __process_results(results): | ||
"""Processes the result from __query to get valid json from every entry. | ||
:param results: Results from __query | ||
:type results: str | ||
:returns: python list of dictionaries containing the relevant results. | ||
:rtype: list | ||
""" | ||
result_list = [] | ||
|
||
# Splts the result and cuts first and last dataset which are comments | ||
split = results.split(sep='\n\n')[1:-1] | ||
|
||
for entry in split: | ||
entry_dict = {} | ||
for value in entry.split('\n'): | ||
if len(value) < 1: | ||
continue | ||
(desc, val) = value.split(': ') | ||
entry_dict[desc.replace('-', '')] = val.strip(' ') | ||
result_list.append(entry_dict) | ||
return result_list | ||
|
||
|
||
def query(domain: str, limit: int=100): | ||
"""Queries and returns a python dict with results. | ||
:param domain: domain that should be queried | ||
:type domain: str | ||
:param limit: number of entries to return | ||
:type limit: int | ||
:returns: query results | ||
:rtype: list | ||
""" | ||
return __process_results(__query(domain, limit)) |