Bonjour à tous,
Ayant une ip dynamique et un domaine chez Gandi, j'ai installé gandip
https://pypi.org/project/gandip/#description ( script python 3.6 pour actualiser la zone dns de Gandi(v5) aux changements d'ip).
Ai la version debian stretch python 3.5 + python3-pip installés.
Statut de développement du script gandip: production/stable.
Ai procédé à l'installation avec un
pip3 install --user gandip
Installation réussie sous $HOME/.local/
Lorsque je lance le script, je me retrouve avec une erreur de syntax :
Traceback (most recent call last):
File "/home/olivier/.local/bin/gandip", line 7, in <module>
from gandip import main
File "/home/olivier/.local/lib/python3.5/site-packages/gandip.py", line 66
f"{self.url}/domains/{fqdn}/records/{name}/{rrset_type}",
^
SyntaxError: invalid syntax
Etant totalement novice en python, quelqu'un pourrait 'il m'aider à résoudre ce problème, j'ai vraiment besoin de ce script.
Merci d'avance.
Je joints le contenu de gandip.py
"""
Copyright (c) 2017 Arnaud Levaufre <arnaud@levaufre.name>
"""
import urllib.request
import logging
import argparse
import json
import sys
IP_GETTER_URL = "http://5.39.16.10/tout/ip/"
GANDI_API_URL = "https://dns.api.gandi.net/api/v5"
LOG_FORMAT = "[%(asctime)s][%(name)s][%(levelname)s] %(message)s"
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
logger = logging.getLogger(__name__)
class GandiAPI:
def __init__(self, url, key):
self.url = url
self.key = key
def update_records(self, fqdn, records, current_ip, ttl=10800):
all_records = self.get_record_list(fqdn)
filtered_records = list(filter(
lambda x: x['rrset_name'] in records and x['rrset_type'] == 'A' and current_ip not in x['rrset_values'], all_records
))
if filtered_records:
for record in filtered_records:
logger.info(
"Updating record %s for domain %s with new ip %s",
record['rrset_name'],
fqdn,
current_ip
)
name = record["rrset_name"]
rrset_type = record["rrset_type"]
request = urllib.request.Request(
f"{self.url}/domains/{fqdn}/records/{name}/{rrset_type}",
method="PUT",
headers = {
"Content-Type": "application/json",
"X-Api-Key": self.key
},
data=json.dumps({
"rrset_ttl": ttl,
"rrset_values": [current_ip],
}).encode()
)
with urllib.request.urlopen(request) as response:
logger.debug(json.loads(response.read().decode()))
else:
logger.info("Everything is up to date")
return True
def get_record_list(self, fqdn):
request = urllib.request.Request(f"{self.url}/domains/{fqdn}/records")
request.add_header("X-Api-Key", self.key)
with urllib.request.urlopen(request) as response:
return json.loads(response.read().decode())
def get_current_ip(provider_url=IP_GETTER_URL):
request = urllib.request.urlopen(provider_url)
return request.read().decode()
def main():
parser = argparse.ArgumentParser(
description=""""
Keep your gandi DNS records up to date with your current IP
"""
)
parser.add_argument('key', type=str, help="Gandi API key")
parser.add_argument('zone', type=str, help="Zone to update")
parser.add_argument('record', type=str, nargs='+', help="Records to update")
parser.add_argument("--ip-getter", type=str, default=IP_GETTER_URL, help="""
Web page that give your current ip. It should only return the ip as
text. Defaults to {}
""".format(IP_GETTER_URL))
parser.add_argument("--ttl", type=int, default=10800, help="Set a custom ttl (in second)")
args = parser.parse_args()
logger.info('Gandi record update started.')
current_ip = get_current_ip(args.ip_getter)
api = GandiAPI(GANDI_API_URL, args.key)
api.update_records(args.zone, args.record, current_ip, ttl=args.ttl)
if __name__ == "__main__":
main()
Dernière modification par loularsen (12-08-2018 18:49:53)
Au beau milieu de nulle part ......