Skip to content
Snippets Groups Projects

Multiple domain spf setter

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Kick Victor

    Update multiple domains to add an spf record.

    Install dependencies pip install -r requirements.txt

    Generate api key https://eu.api.ovh.com/createToken/ and update exclude_domains & spf_value

    Edited
    main.py 2.89 KiB
    from typing import List
    
    import ovh
    
    exclude_domains = []
    spf_value = "\"v=spf1 ip4:xx.xx.xx.xx ip4:xx.xx.xx.xx -all\""
    """
    To create OVH api credentials go there https://eu.api.ovh.com/createToken/
    // It needs the following Endpoints :
    // - GET /domain/zone
    // - GET /domain/zone/*/record
    // - GET /domain/zone/*/record/*
    // - PUT /domain/zone/*/record/*
    // - DELETE /domain/zone/*/record/*
    // - POST /domain/zone/*/record
    """
    
    
    class SPFClient:
        TXT_RECORD = 1
        SPF_RECORD = 2
    
        def __init__(self, application_key, application_secret, consumer_key):
            self.client = ovh.Client(
                endpoint='ovh-eu',
                application_key=application_key,
                application_secret=application_secret,
                consumer_key=consumer_key,
            )
    
        def get_zones(self) -> List[str]:
            zones = self.client.get("/domain/zone")
            return [i for i in zones if i not in exclude_domains]
    
        # Return record + record type
        def get_spf(self, zone):
            records = self.client.get('/domain/zone/%s/record?fieldType=TXT' % zone)
            for record in records:
                r = self.get_record(zone, record)
                if r["target"].startswith("\"v=spf1"):
                    return record, SPFClient.TXT_RECORD
            records = self.client.get('/domain/zone/%s/record?fieldType=SPF' % zone)
            for record in records:
                r = self.get_record(zone, record)
                if r["target"].startswith("\"v=spf1"):
                    return record, SPFClient.SPF_RECORD
            return None, 0
    
        def get_record(self, zone: str, record: str):
            return self.client.get('/domain/zone/%s/record/%s' % (zone, record))
    
        def set_record(self, zone, value):
            return self.client.post('/domain/zone/%s/record' % zone, target=value, fieldType="SPF", ttl=60)
    
        def set_spf(self, zone: str, spf: str):
            record, record_type = self.get_spf(zone)
            if not record:
                print("Setting spf %s for record %s" % (spf, zone))
                self.set_record(zone, spf)
            else:
                print("Updating spf %s for record %s" % (spf, zone))
                if record_type == SPFClient.TXT_RECORD:
                    print("Previous record was TXT, updating it to SPF")
                    self.delete_record(zone, str(record))
                    self.set_record(zone, spf)
                else:
                    self.update_record(zone, spf_value, str(record))
            return
    
        def update_record(self, zone: str, value: str, record_id: str):
            return self.client.put('/domain/zone/%s/record/%s' % (zone, record_id), target=value)
    
        def delete_record(self, zone: str, record_id: str):
            return self.client.delete('/domain/zone/%s/record/%s' % (zone, record_id))
    
        def set_spf_all(self, spf: str):
            for zo in self.get_zones():
                self.set_spf(zo, spf)
    
    
    client = SPFClient(application_key="", application_secret="",
                       consumer_key="")
    client.set_spf_all(spf_value)
    requirements.txt 27 B
    ovh==1.0.0
    requests==2.25.1
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment