Guest User

Untitled

a guest
Oct 18th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.03 KB | None | 0 0
  1. """
  2.  
  3. Python Library for WHM's API1 DNS Functions
  4.  
  5.    https://documentation.cpanel.net/display/SDK/Guide+to+WHM+API+1
  6.  
  7. Author: Benton Snyder
  8. Website: http://bensnyde.me
  9. Created: 7/24/13
  10. Revised: 5/16/16
  11.  
  12. """
  13. import base64
  14. import httplib
  15. import urllib
  16. import logging
  17. import json
  18. import urllib2
  19.  
  20.  
  21. class CpanelDNS:
  22.     def __init__(self, whm_base_url, whm_username, whm_password):
  23.         """Constructor
  24.  
  25.        Parameters
  26.            whm_base_url: str whm base url (ex. whm.example.com)
  27.            whm_username: str whm root username
  28.            whm_password: str whm password
  29.        """
  30.         self.whm_base_url = whm_base_url
  31.         self.whm_headers = {'Authorization':'Basic ' + base64.b64encode(whm_username+':'+whm_password).decode('ascii')}
  32.         #print self.whm_headers
  33.  
  34.  
  35.     def _whm_api_query(self, resource, kwargs):
  36.         """WHM API Query
  37.  
  38.            Queries WHM server's JSON API with specified query string.
  39.  
  40.        Parameters
  41.            resource: str api resource uri
  42.            kwargs: dict
  43.        Returns
  44.            json
  45.        """
  46.         try:
  47.             #conn = httplib.HTTPSConnection(self.whm_base_url, 2087)
  48.             #conn.request('GET', '/json-api/%s?%s' % (resource, urllib.urlencode(kwargs)), headers=self.whm_headers)
  49.             #response = conn.getresponse()
  50.             #resp = response.read()
  51.             print "http://{0}/json-api/{1}?{2}".format(self.whm_base_url, resource, urllib.urlencode(kwargs))
  52.             request = urllib2.Request("http://{0}/json-api/{1}?{2}".format(self.whm_base_url, resource, urllib.urlencode(kwargs)), headers=self.whm_headers)
  53.             resp = urllib2.urlopen(request).read()
  54.             print resp
  55.             data = json.loads(resp)
  56.             conn.close()
  57.             return data
  58.         except httplib.HTTPException as ex:
  59.             logging.critical("HTTPException from CpanelDNS API: %s" % ex)
  60.         except ValueError as ex:
  61.             logging.critical("ValueError decoding CpanelDNS API response string: %s" % ex)
  62.         except Exception as ex:
  63.             logging.critical("Unhandled Exception while querying CpanelDNS API: %s" % ex)
  64.  
  65.  
  66.     def add_zone(self, domain, ip, trueowner=None):
  67.         """Add DNS Zone
  68.  
  69.            Creates a DNS zone.
  70.  
  71.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+adddns
  72.  
  73.        Parameters
  74.            domain: str domain name
  75.            ip: str ip address
  76.            trueowner: str cpanel username
  77.        Returns
  78.            json
  79.        """
  80.         return self._whm_api_query('adddns', {
  81.             'domain': domain,
  82.             'ip': ip,
  83.             'trueowner': trueowner
  84.         })
  85.  
  86.  
  87.     def add_zone_record(self, kwargs):
  88.         """Add Zone Record
  89.  
  90.            Adds a record to the specified DNS zone.
  91.  
  92.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+addzonerecord
  93.  
  94.        Parameters
  95.            zone:str - Name of the zone to be added, expressed like a domain name. (ex. example.com)
  96.            * May or may not be required, depending on the type of record being set
  97.            *name:str - The name of the zone record. (ex. example.com.)
  98.            *address:str - The IP address of the zone being added. (ex. 127.0.0.1)
  99.            *class:str - The class of the record. (typically IN, for "Internet")
  100.            *cname:str - The canonical name in a CNAME record.
  101.            *exchange:str - In an MX record, the name of the destination mail server.
  102.            *nsdname:str - A domain name to use for a nameserver. Example: ns1.example.com
  103.            *ptrdname:str - The domain to which the IP address will point.
  104.            *priority:int - In an MX record, this parameter specifies the priority of the destination mail server. In an SRV record, this parameter specifies the overall priority for the SRV record.
  105.            *type:str - The type of zone record being added.
  106.            *ttl:int - The record's time to live.
  107.            ** Required for SRV records
  108.            **weight:int - The weight of the record, relative to records of the same priority.
  109.            **port:int - The port number on which users can access a particular service.
  110.            **target:str - The hostname of the machine providing a specified service.
  111.        Returns
  112.            json
  113.        """
  114.         return self._whm_api_query('addzonerecord', kwargs)
  115.  
  116.  
  117.     def edit_zone_record(self, kwargs):
  118.         """Edit DNS Zone Record
  119.  
  120.            Modify a DNS zone record on the server.
  121.  
  122.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+editzonerecord
  123.  
  124.        Parameters
  125.            zone:str - Name of the zone to be added, expressed like a domain name. (ex. example.com)
  126.            Line:str - The number of the zone record line you wish to edit.
  127.            * May or may not be required, depending on the type of record being set
  128.            *address:str - The IP address of the zone being added. (ex. 127.0.0.1)
  129.            *class:str - The class of the record. (typically IN, for "Internet")
  130.            *cname:str - The canonical name in a CNAME record.
  131.            *exchange:str - In an MX record, the name of the destination mail server.
  132.            *preference:int - In an MX record, the priority of the destination mail server. (0 is highest priority)
  133.            *expire:str -  A 32-bit time value that specifies the upper limit on the time interval that can elapse before the zone is no longer authoritative.
  134.            *minimum:int - The unsigned 32-bit minimum time to live field that should be exported with any record from this zone.
  135.            *mname:int - The domain name of the nameserver serving as the original or primary source of data for this zone. Example: ns1.example.com
  136.            *name:str - Domain name. Example: example.com
  137.            *nsdname:str - A domain name to use for a nameserver. Example: ns1.example.com
  138.            *raw:str - Raw line data.
  139.            *refresh:int - 32-bit time interval which will elapse before the zone will be refreshed.
  140.            *retry:int - 32-bit time interval which will elapse before a failed refresh will be retried.
  141.            *rname:str - A domain name which specifies the mailbox of the person responsible for this zone. Example: user.example.com
  142.            *serial:int - The unsigned 32-bit version number of the original copy of the zone.
  143.            *txtdata:str - Text record data.
  144.            *type:str - The type of zone record being added.
  145.            *ttl:int - The record's time to live.
  146.        Returns
  147.            json
  148.        """
  149.         return self._whm_api_query('editzonerecord', kwargs)
  150.  
  151.  
  152.     def add_reversed_zone_record(self, zone, name, ptrdname):
  153.         """Add DNS PTR Record
  154.  
  155.            Add reverse PTR record to specified zone.
  156.  
  157.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+addzonerecord+for+Reverse+DNS
  158.  
  159.        Parameters
  160.            zone:str - The name of the reverse DNS zone file to create. This value must follow a standardized naming schema.
  161.            name:int - You will need to enter the last octet of the IP address here. If your IP address was 192.168.0.1, you would enter 1 in this parameter.
  162.            ptrdname:str - The name of the domain to which the IP address will resolve (e.g. example.com).
  163.        Returns
  164.            json
  165.        """
  166.         return self._whm_api_query('addzonerecord', {
  167.             'zone': zone,
  168.             'name': name,
  169.             'ptrdname': ptrdname,
  170.             'type': 'PTR'
  171.         })
  172.  
  173.  
  174.     def get_zone_record(self, domain, line):
  175.         """Get DNS Zone Record
  176.  
  177.            Retrieves specified record from specified zone.
  178.  
  179.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+getzonerecord
  180.  
  181.        Parameters
  182.            domain:str - The domain whose zone record you wish to view.
  183.            line:str - The line you wish to view in the zone record.
  184.        Returns
  185.            json
  186.        """
  187.         return self._whm_api_query('getzonerecord', {
  188.             'domain': domain,
  189.             'line': line
  190.         })
  191.  
  192.  
  193.     def delete_zone(self, domain):
  194.         """Delete DNS Zone
  195.  
  196.            Deletes a DNS zone.
  197.  
  198.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+killdns
  199.  
  200.        Parameters
  201.            domain:str - Domain name for the zone to be deleted.
  202.        Returns
  203.            json
  204.        """
  205.         return self._whm_api_query('killdns', {
  206.             'domain': domain
  207.         })
  208.  
  209.  
  210.     def list_zones(self, cpanel_user=None):
  211.         """List DNS zones
  212.  
  213.            Retrieves list of all domains and corresponding DNS zones associated with your server.
  214.  
  215.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+listzones
  216.  
  217.        Parameters
  218.            *cpanel_user: str cpanel account username to call from
  219.        Returns
  220.            json
  221.        """
  222.         return self._whm_api_query('listzones', {})
  223.  
  224.  
  225.     def list_zone(self, domain):
  226.         """Get DNS Zone
  227.  
  228.            Retrieves all records for specified zone.
  229.  
  230.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+dumpzone
  231.  
  232.        Parameters
  233.            domain:str - Domain for which to show the DNS zone. Example: example.com
  234.        Returns
  235.            json
  236.        """
  237.         return self._whm_api_query('dumpzone', {
  238.             'domain': domain
  239.         })
  240.  
  241.  
  242.     def get_nameserver_ip(self, nameserver):
  243.         """Get Nameserver IP
  244.  
  245.            Retrieve the IP address of a registered nameserver from the root nameservers.
  246.  
  247.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+lookupnsip
  248.  
  249.        Parameters
  250.            nameserver:str - Hostname of the nameserver whose IP address you want to obtain. Example: ns1.example.com
  251.        Returns
  252.            json
  253.        """
  254.         return self._whm_api_query('lookupnsip', {
  255.             'nameserver': nameserver
  256.         })
  257.  
  258.  
  259.     def delete_zone_record(self, zone, line):
  260.         """Delete DNS Zone Record
  261.  
  262.            Delete a DNS zone record from the server.
  263.  
  264.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+removezonerecord
  265.  
  266.        Parameters
  267.            zone:str - The domain name whose zone record you wish to remove.
  268.            line:str - The line number of the zone record you wish to remove.
  269.        Returns
  270.            json
  271.        """
  272.         return self._whm_api_query('removezonerecord', {
  273.             'zone': zone,
  274.             'line': line
  275.         })
  276.  
  277.  
  278.     def reset_zone(self, domain=None, zone=None, user=None):
  279.         """Reset Zone
  280.  
  281.            Resets specified zone and any associated subdomains to default settings.
  282.  
  283.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+resetzone
  284.  
  285.        Parameters
  286.            domain:str - The domain name whose zone should be reset.
  287.            zone:str - The DNS zone's filename.
  288.            *user:str - The user who owns the domain name whose zone should be reset.
  289.            **Only one of domain/zone is required.
  290.        Returns
  291.            json
  292.        """
  293.         if not domain and not zone:
  294.             raise Exception('reset_zone() requires one of [domain,zone]')
  295.  
  296.         return self._whm_api_query('resetzone', {
  297.             'domain': domain,
  298.             'zone': zone,
  299.             'user': user
  300.         })
  301.  
  302.  
  303.     def list_zone_mx_records(self, domain):
  304.         """Get DNS MX Records
  305.  
  306.            Retrieves MX records for specified domain.
  307.  
  308.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+listmxs
  309.  
  310.        Parameters
  311.            domain:str - The domain corresponding to the MX records that you wish to view.
  312.        Returns
  313.            json
  314.        """
  315.         return self._whm_api_query('listmxs', {
  316.             'api.version': 1,
  317.             'domain': domain
  318.         })
  319.  
  320.  
  321.     def add_zone_mx_record(self, domain, name, exchange, preference, mxclass=None, serialnum=None, ttl=None):
  322.         """Add DNS MX Record
  323.  
  324.            Add an MX record to specified domain.
  325.  
  326.                https://documentation.cpanel.net/display/SDK/WHM+API+1+Functions+-+savemxs
  327.  
  328.        Parameters
  329.            domain:str - The domain corresponding to the MX records that you wish to view.
  330.            name:str - The name of the new MX record.
  331.            exchange:str - The exchanger to which the domain will point. (e.g., example.com)
  332.            preference:int - The new MX entry's preference. Lower values indicate a higher preference.
  333.            *class:str - The MX record's class.
  334.            *serialnum:int - The serial number of the MX record.
  335.            *ttl:int - The new record's time to live.
  336.  
  337.        Returns
  338.            json
  339.        """
  340.         return self._whm_api_query('savemxs', {
  341.             'api.version': 1,
  342.             'domain': domain,
  343.             'name': name,
  344.             'exchange': exchange,
  345.             'preference': preference,
  346.             'class': mxclass,
  347.             'serialnum': serialnum,
  348.             'ttl': ttl
  349.         })
Add Comment
Please, Sign In to add comment