Browse Source

Added ability to add a new domain.

master
Rune Olsen 2 years ago
parent
commit
ae81f4796f
2 changed files with 43 additions and 1 deletions
  1. +1
    -0
      README.md
  2. +42
    -1
      py-dyndns.py

+ 1
- 0
README.md View File

@@ -13,6 +13,7 @@ Change permission of the file to be excecutable `chmod +x py-dyndns.py` and run

* To list all subdomains; ./py-dyndns.py -l
* To show current IP's for domains in config.ini; ./py-dyndns.py -c
* To add a new domain; ./py-dyndns.py -a <domainname>
* To show help; ./py-dyndns.py -h

To update all subdomains in config.ini run the app without arguments.


+ 42
- 1
py-dyndns.py View File

@@ -4,6 +4,7 @@ import configparser
import json
import logging
import argparse
import requests

# Config stuff

@@ -26,6 +27,22 @@ def updateip(ip, domain):
return str(response)


def createnewdomain(name, ip):
data = {'name': name[0],
'data': ip,
'type': "A",
'ttl': 3600
}
headers = {'Authorization': 'Bearer ' + api, "Content-Type": "application/json"}
response = requests.post('https://api.digitalocean.com/v2/domains/' + domain_name + '/records',
data=json.dumps(data), headers=headers)
print(str(response))
if str(response) == '<Response [201]>':
return response.json()
else:
return 'Fail'


def runupdate():
for section in config.sections():
if not config.has_option(section, 'subdomainid'):
@@ -76,18 +93,42 @@ def listcurrent():
remoteIP4 = remoteData['domain_record']['data']
print(section+' : '+remoteIP4)

# Commandline arguments

def addnewdomin(newdata):
currentlocalip = urllib.request.urlopen("http://ip4.iurl.no").read().decode('utf-8')
response = createnewdomain(newdata['add'][0], currentlocalip)
if response is not 'Fail':
logging.info('Success! Domain %s updated with ID: %s', newdata['add'][0], response['domain_record']['id'])
domainid = str(response['domain_record']['id'])
print(newdata['add'][0])
section = str(newdata['add'][0])
section = section.strip("[']")
print(section)
config[section] = {
'subdomainid': str(domainid)
}
with open('config.ini', 'w') as f:
config.write(f)
print('Domain ' + str(section) + ' added with ID ' + str(domainid) + 'and config.ini updated!')
else:
print('ERROR! Domain not added!')


# Commandline arguments

parser = argparse.ArgumentParser(description='DigitalOcean Dynamic DNS')
parser.add_argument('-l', '--list', help='List the available sub-domains on the domain supplied in the '
'config.ini file.', required=False, action="store_true")
parser.add_argument('-c', '--current', help='List the current IP address for the sub-domains in the config.ini file',
required=False, action="store_true")
parser.add_argument('-a', '--add', help='Add a new domain to your DigitalOcean account and to the config.ini file',
required=False, nargs=1, action='append')
args = vars(parser.parse_args())
if args['list']:
listdomains()
elif args['current']:
listcurrent()
elif args['add']:
addnewdomin(args)
else:
runupdate()

Loading…
Cancel
Save