diff --git a/getIPaddress.py b/getIPaddress.py new file mode 100755 index 0000000..6694e27 --- /dev/null +++ b/getIPaddress.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# -*- encoding: UTF8 -*- + +# author: Pascal Gollor (https://gitbucket.pgollor.de) + + +import argparse +import requests + +def getPublicIP(ipv4=False, verbose=False): + ipstr = '6' + if (ipv4): + ipstr = '4' + r = requests.get('http://v' + ipstr + '.pgollor.de/ip.php?raw') + if (not r.ok): + raise RuntimeError() + + ip = r.text + if (verbose): + print('ip:', ip) + + return ip +# end getPublicIP + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='inwx subdomain update') + parser.add_argument('ipv', metavar='ip type', choices=['4', '6'], help='4: IPv4, 6: IPv6') + parser.add_argument('-v', '--verbose', action='store_true', help='verbose') + args = parser.parse_args() + + ipv = int(args.ipv) + + try: + getPublicIP(ipv4=ipv==4, verbose=args.verbose) + except RuntimeError as e: + print(e.args[0]) + # end try +# end if \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index df98497..4837a95 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ inwx-domrobot +requests diff --git a/update-record.py b/update-record.py index d688f0c..eb83a2e 100755 --- a/update-record.py +++ b/update-record.py @@ -7,6 +7,7 @@ from INWX.Domrobot import ApiClient from configuration import get_account_data import argparse +from getIPaddress import getPublicIP def errorCheck(ret): @@ -100,6 +101,14 @@ # get config and login username, password, shared_secret = get_account_data(True, config_file=args.config_file, config_section=args.config_section) + + # use puplic ip as content + content= args.content + if (args.public_ip and (args.mode == 'create' or args.mode == 'update')): + if (args.record_type != 'A' and args.record_type != 'AAAA'): + raise RuntimeError('Record type have to be A or AAAA') + content = getPublicIP(ipv4=(args.record_type=='A')) + # end if updateRecord( mode=args.mode, @@ -107,7 +116,7 @@ password=password, domain=domain, sub=sub, - content=args.content, + content=content, record_type=args.record_type, ttl=args.ttl, config_section=args.config_section, @@ -124,7 +133,6 @@ parser.add_argument('-r', '--record-type', metavar='type', required=True, choices=['A', 'AAAA', 'TXT', 'CNAME', 'TLSA'], help='record type (A, AAAA, TXT, CNAME, TLSA)') parser.add_argument('-m', '--mode', metavar='mode', required=True, choices=['create', 'update', 'delete'], help='operation mode (create, update, delete)\nupdate:update all existing records if one exists, or create if not existing\ndelete: delete existing record with given content, or delete all records if no content is given') parser.add_argument('-p', '--public-ip', action='store_true', required=False, help='insert public ip. Use A for ip4 an AAAA for ipv6') - parser.add_argument('-c', '--config_file', metavar='path', default='./conf.cfg', help='path to configuration file') parser.add_argument('-t', '--ttl', default=3600, type=int, help='TTL (time to live) of the nameserver record in seconds (default 3600)') parser.add_argument('-s', '--config_section', metavar='section', default='live', choices=['live', 'ote'], help='configuration section (live, ote) default: live') @@ -133,9 +141,8 @@ args = parser.parse_args() - # do some checks - if (args.mode != 'delete' and not args.content): + if (args.mode != 'delete' and not args.public_ip and not args.content): raise ValueError("Missing content for domain entry.") # end if