Files
satori/satori/dns.py
Brint O'Hearn eb115508a2 If whois information is incomplete, exclude it
Conflicts:
	satori/dns.py

Change-Id: Id136bf83e0d422c8d4b164d464aa721247f9bf26
2014-03-06 11:19:34 -06:00

78 lines
2.5 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
"""Satori DNS Discovery."""
import datetime
import logging
import socket
import dateutil.parser
import pythonwhois
from six.moves.urllib import parse as urlparse
import tldextract
from satori import errors
LOG = logging.getLogger(__name__)
def resolve_hostname(host):
"""Get IP address of hostname or URL."""
try:
if not host:
raise AttributeError("Host must be supplied.")
parsed = urlparse.urlparse(host)
except AttributeError as err:
error = "Hostname `%s` is unparseable. Error: %s" % (host, err)
LOG.exception(error)
raise errors.SatoriInvalidNetloc(error)
# Domain names are in netloc, IP addresses fall into path
hostname = parsed.netloc or parsed.path
# socket.gaierror is not trapped here
address = socket.gethostbyname(hostname)
return address
def get_registered_domain(hostname):
"""Get the root DNS domain of an FQDN."""
return tldextract.extract(hostname).registered_domain
def domain_info(domain):
"""Get as much information as possible for a given domain name."""
domain = get_registered_domain(domain)
result = pythonwhois.get_whois(domain)
registrar = []
if 'registrar' in result and len(result['registrar']) > 0:
registrar = result['registrar'][0]
nameservers = result.get('nameservers', [])
days_until_expires = None
expires = None
if 'expiration_date' in result:
if (isinstance(result['expiration_date'], list)
and len(result['expiration_date']) > 0):
expires = result['expiration_date'][0]
if not isinstance(expires, datetime.datetime):
expires = dateutil.parser.parse(expires)
days_until_expires = (expires - datetime.datetime.now()).days
return {
'name': domain,
'whois': result['raw'],
'registrar': registrar,
'nameservers': nameservers,
'days_until_expires': days_until_expires,
'expiration_date': expires,
}