Skip to content

Instantly share code, notes, and snippets.

@troeger
Created March 8, 2017 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troeger/266d299ecf4ba0a5229dfa62db73a371 to your computer and use it in GitHub Desktop.
Save troeger/266d299ecf4ba0a5229dfa62db73a371 to your computer and use it in GitHub Desktop.
Using the SchlundTech XML interface from Python
import schlundtech
auth={"user":"xxxxxx", "password":"yyyyy", "context":"zzz"}
all_domains=schlundtech.domain_info(auth)
for domain in all_domains:
print("\n{name} (last update: {updated})".format(**domain))
info=schlundtech.zone_info(auth, domain['name'])
for rr in info:
print("{name}\t\t{type}\t{value}".format(**rr))
#!/usr/bin/python3
import requests
import xml.etree.ElementTree as etree
HOST="https://gateway.schlundtech.de"
HEADERS = {'Content-Type': 'text/xml'}
# Base for XML request
xml_base='''
<request>
<auth>
<user>{0}</user>
<password>{1}</password>
<context>{2}</context>
</auth>
<language>de</language>
<task>
<code>{3}</code>
{4}
</task>
</request>
'''
def _build_request_xml(auth, code, additional=None):
if additional:
return xml_base.format(auth["user"], auth["password"], auth["context"], code, additional)
else:
return xml_base.format(auth["user"], auth["password"], auth["context"], code, "")
def domain_info(auth):
"""
@brief Determines the list of domains for this Schlundtech account.
@param auth Authentication information, as dictionary.
@return List of dictionaries, one for each domain.
"""
request_xml=_build_request_xml(auth, "0105", "")
result_xml=requests.post(HOST, data=request_xml, headers=HEADERS).text
tree = etree.fromstring(result_xml)
result=[]
for domain in tree.findall(".//domain"):
domain_info={child.tag:child.text for child in domain}
result.append(domain_info)
return result
def zone_info(auth, domain_name):
"""
@brief Determines all resource records for a given domain name in this Schlundtech account.
@param auth Authentication information, as dictionary.
@param name Domain name to be queried.
@return List of dictionaires, one for each resource record.
"""
request_xml=_build_request_xml(auth, "0205", "<zone><name>{0}</name></zone>".format(domain_name))
result_xml=requests.post(HOST, data=request_xml, headers=HEADERS).text
tree = etree.fromstring(result_xml)
result=[]
for rr in tree.findall(".//rr"):
domain_info={child.tag:child.text for child in rr}
result.append(domain_info)
return result
def has_spf(zone_info):
"""
@brief Determines if the resource record dictionary has an SPF entry.
@param zone_info Resource records, as returned by @zone_info.
@return True if SPF record was found, otherwise false.
"""
for entry in zone_info:
if entry['type']=='TXT' and ("v=spf1" in entry['value']):
return True
return False
def has_dmarc(zone_info):
"""
@brief Determines if the resource record dictionary has an DMARC entry.
@param zone_info Resource records, as returned by @zone_info.
@return True if DMARC record was found, otherwise false.
"""
for entry in zone_info:
if entry['name']=='_dmarc' and entry['type']=='TXT' and ("v=DMARC1" in entry['value']):
return True
return False
def add_rr(auth, domain_name, rr_name, rr_type, rr_value):
"""
@brief Add a resource record to the zone.
@return Returns status text
"""
specific_xml="<default><rr_add><name>{0}</name><type>{1}</type><value>{2}</value></rr_add></default><zone><name>{3}</name></zone>".format(rr_name, rr_type, rr_value, domain_name)
request_xml=_build_request_xml(auth, "0202001", specific_xml)
result_xml=requests.post(HOST, data=request_xml, headers=HEADERS).text
tree = etree.fromstring(result_xml)
return tree.find(".//text").text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment