Browse Source

Added support for multiple domains

rune 1 month ago
parent
commit
cd35f0ce0b
3 changed files with 39 additions and 21 deletions
  1. 2
    1
      .gitignore
  2. 15
    2
      config-template.ini
  3. 22
    18
      py-dyndns.py

+ 2
- 1
.gitignore View File

@@ -1 +1,2 @@
1
-config.ini
1
+config.ini
2
+.idea/

+ 15
- 2
config-template.ini View File

@@ -1,7 +1,20 @@
1
+###########################################################
2
+# Config File for DynDns
3
+# The [DYNDNS] part holds information that is used
4
+# for all the domains you want to update/check
5
+#
6
+# Create a new [Section] for each domain just containg
7
+#   subdomainid = xxxxxxx
8
+# Where xxxxxxx must be replaced with the domainid
9
+###########################################################
10
+
1 11
 [DYNDNS]
2 12
 api_key = 
3 13
 baseuri = example.com
4
-subdomainid = 000000
5 14
 ip4 = http://ip4.iurl.no
6 15
 ip6 = http://ip6.iurl.no
7
-logfile = /var/log/dyndns.log
16
+logfile = /var/log/dyndns.log
17
+[SITE0]
18
+subdomainid = 000000
19
+[SITE1]
20
+subdomainid = 000000

+ 22
- 18
py-dyndns.py View File

@@ -12,19 +12,11 @@ api = config['DYNDNS']['api_key']
12 12
 ip4 = config['DYNDNS']['ip4']
13 13
 ip6 = config['DYNDNS']['ip6']
14 14
 domain_name = config['DYNDNS']['baseuri']
15
-domain_id = config['DYNDNS']['subdomainid']
15
+#domain_id = config['DYNDNS']['subdomainid']
16 16
 logging.basicConfig(filename=config['DYNDNS']['logfile'],level=logging.DEBUG)
17 17
 # Get the current local IP4
18 18
 local_ip4 = urllib.request.urlopen("http://ip4.iurl.no").read().decode('utf-8')
19 19
 
20
-req = urllib.request.Request('https://api.digitalocean.com/v2/domains/'+domain_name+'/records/' + domain_id)
21
-req.add_header('Content-Type', 'application/json')
22
-req.add_header('Authorization', 'Bearer '+api)
23
-current = urllib.request.urlopen(req)
24
-remote = current.read().decode('utf-8')
25
-remoteData = json.loads(remote)
26
-reomoteIP4 = remoteData['domain_record']['data']
27
-
28 20
 
29 21
 def updateIP(IP):
30 22
     data = {'data': IP}
@@ -32,12 +24,24 @@ def updateIP(IP):
32 24
     response = requests.put('https://api.digitalocean.com/v2/domains/'+domain_name+'/records/' + domain_id, data=json.dumps(data), headers=headers)
33 25
     return str(response)
34 26
 
35
-    
36
-if reomoteIP4 != local_ip4:
37
-    response = updateIP(local_ip4)
38
-    if '200' in response:
39
-        logging.info('Success!')
40
-    else:
41
-        logging.error('Failure! '+response)
42
-else:
43
-    logging.info('No need to update the IP! Local IP ('+local_ip4+') is the same as Remote IP ('+reomoteIP4+')')
27
+
28
+for section in config.sections():
29
+    if not config.has_option(section, 'subdomainid'):
30
+        continue
31
+    for name, value in config.items(section):
32
+        req = urllib.request.Request('https://api.digitalocean.com/v2/domains/' + domain_name + '/records/' +
33
+                                     value)
34
+        req.add_header('Content-Type', 'application/json')
35
+        req.add_header('Authorization', 'Bearer ' + api)
36
+        current = urllib.request.urlopen(req)
37
+        remote = current.read().decode('utf-8')
38
+        remoteData = json.loads(remote)
39
+        remoteIP4 = remoteData['domain_record']['data']
40
+        if remoteIP4 != local_ip4:
41
+            response = updateIP(local_ip4)
42
+            if '200' in response:
43
+                logging.info('Success! Domain %s updated with IP: %s', section,remoteIP4)
44
+            else:
45
+                logging.error('Failure! '+response)
46
+        else:
47
+            logging.info('No need to update the IP! Local IP ('+local_ip4+') for '+section+' is the same as Remote IP ('+remoteIP4+')')

Loading…
Cancel
Save