Add `ip_info` and `netloc_info`
`domain_info` makes the assumption that only a domain is being passed to it. This patch adds an `ip_info` and a `netloc_info` to handle ip addresses or domain. `ip_info` expects an IP only and will error if the string that is passed to it is not a valid IP address. `netloc_info` will attempt to determine if the string that is passed to it is an IP or a domain, and call the appropriate function. Change-Id: I34baf3b2ee94bd866531f94d3073fe83098f1046 Implements: blueprint ip-and-netloc-info
This commit is contained in:
parent
1b82964f41
commit
199bba3095
|
@ -55,6 +55,19 @@ def get_registered_domain(hostname):
|
|||
return tldextract.extract(hostname).registered_domain
|
||||
|
||||
|
||||
def ip_info(ip):
|
||||
"""Get as much information as possible for a given ip address."""
|
||||
if not utils.is_valid_ip_address(ip):
|
||||
error = "`%s` is an invalid IP address." % ip
|
||||
raise errors.SatoriInvalidIP(error)
|
||||
|
||||
result = pythonwhois.get_whois(ip)
|
||||
|
||||
return {
|
||||
'whois': result['raw']
|
||||
}
|
||||
|
||||
|
||||
def domain_info(domain):
|
||||
"""Get as much information as possible for a given domain name."""
|
||||
registered_domain = get_registered_domain(domain)
|
||||
|
@ -87,3 +100,11 @@ def domain_info(domain):
|
|||
'days_until_expires': days_until_expires,
|
||||
'expiration_date': expires,
|
||||
}
|
||||
|
||||
|
||||
def netloc_info(netloc):
|
||||
"""Determine if netloc is an IP or domain name."""
|
||||
if utils.is_valid_ip_address(netloc):
|
||||
ip_info(netloc)
|
||||
else:
|
||||
domain_info(netloc)
|
||||
|
|
|
@ -31,6 +31,11 @@ class SatoriInvalidDomain(SatoriException):
|
|||
"""Invalid Domain provided."""
|
||||
|
||||
|
||||
class SatoriInvalidIP(SatoriException):
|
||||
|
||||
"""Invalid IP provided."""
|
||||
|
||||
|
||||
class SatoriShellException(SatoriException):
|
||||
|
||||
"""Invalid shell parameters."""
|
||||
|
|
|
@ -276,5 +276,19 @@ class TestDNS(utils.TestCase):
|
|||
"192.168.0.1"
|
||||
)
|
||||
|
||||
def test_ip_info_raises_invalid_ip_error(self):
|
||||
self.assertRaises(
|
||||
errors.SatoriInvalidIP,
|
||||
dns.ip_info,
|
||||
"example.com"
|
||||
)
|
||||
|
||||
def test_ip_info_raises_invalid_ip_error_bad_ip(self):
|
||||
self.assertRaises(
|
||||
errors.SatoriInvalidIP,
|
||||
dns.ip_info,
|
||||
"1.2.3"
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in New Issue