Creation of DNS Dig tool
* Creation of module dns_dig * Added dig and dig_search functions * dig uses dnspython package to query the given url and return the target from the answer. * dig_search will recursively call dig to look for a search string amount the urls that dig finds. * Updated pip-requires to add dnspython Change-Id: I6393c52f4f8b903dff100569d3be59f970d56f95
This commit is contained in:
parent
ab0bbfcd34
commit
bc36d20bb2
45
cloudcafe/common/tools/dns_dig.py
Normal file
45
cloudcafe/common/tools/dns_dig.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""
|
||||
Copyright 2016 Rackspace
|
||||
|
||||
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.
|
||||
"""
|
||||
import re
|
||||
from dns import resolver
|
||||
|
||||
|
||||
def dig(url, dig_type=None):
|
||||
"""
|
||||
Use dns.resolver to query the provided url and return the target from the
|
||||
answer. Resolver will raise an exception if no Answer is returned from the
|
||||
query.
|
||||
"""
|
||||
for rdata in resolver.query(url, dig_type):
|
||||
return rdata.target
|
||||
|
||||
|
||||
def dig_search(url, search_string=None):
|
||||
"""
|
||||
Recursively digs URL's until one with the specified search string is
|
||||
found. Otherwise return None.
|
||||
"""
|
||||
url = str(url)
|
||||
|
||||
if re.search(search_string, url):
|
||||
# Found search string in url, return it
|
||||
return url
|
||||
elif not url:
|
||||
return
|
||||
else:
|
||||
# Dig on the url
|
||||
url = dig(url, dig_type="cname")
|
||||
return dig_search(url, search_string)
|
@ -2,3 +2,4 @@ IPy
|
||||
netaddr
|
||||
XenAPI
|
||||
dateutils
|
||||
dnspython
|
||||
|
Loading…
x
Reference in New Issue
Block a user