Browse Source

Updates

Added:
* List all subdomains
* Show current IP(s)
* Added help
rune 1 month ago
parent
commit
440c3e4c1f
3 changed files with 85 additions and 27 deletions
  1. 2
    1
      .gitignore
  2. 13
    3
      README.md
  3. 70
    23
      py-dyndns.py

+ 2
- 1
.gitignore View File

@@ -1,2 +1,3 @@
1 1
 config.ini
2
-.idea/
2
+.idea/
3
+py-getdomains.py

+ 13
- 3
README.md View File

@@ -11,10 +11,20 @@ This is made based on Python 3 so you need Python3 to use it!
11 11
 ### Use
12 12
 Change permission of the file to be excecutable `chmod +x py-dyndns.py` and run it with `./py-dyndns.py`
13 13
 
14
+To list all subdomains; run ./py-dyndns.py -l
15
+To show current IP's for domains in config.ini; run ./py-dyndns.py -c
16
+To show help; run ./py-dyndns.py -h
17
+
18
+To update all subdomains in config.ini run the app without arguments. 
19
+
20
+The best way to use this in with cron. To run the app every second hour vrate a cron job like this:
21
+
22
+`0 1-23/2 * * * py-dyndns/py-dyndns.py >/dev/null 2>&1`
23
+
14 24
 ### Development
15 25
 * Add IPv6
16
-* Add possibility to list sub domain(s) with domainid(s)
17
-* Add help text
18
-* Add possibility to update more than one domain 
26
+* ~~Add possibility to list sub domain(s) with domainid(s)~~
27
+* ~~Add help text~~
28
+* ~~Add possibility to update more than one domain~~ 
19 29
 
20 30
 

+ 70
- 23
py-dyndns.py View File

@@ -1,9 +1,10 @@
1 1
 #!/usr/bin/python3
2 2
 import urllib.request
3
-import requests
4 3
 import configparser
5 4
 import json
6 5
 import logging
6
+import argparse
7
+
7 8
 # Config stuff
8 9
 
9 10
 config = configparser.ConfigParser()
@@ -12,35 +13,81 @@ api = config['DYNDNS']['api_key']
12 13
 ip4 = config['DYNDNS']['ip4']
13 14
 ip6 = config['DYNDNS']['ip6']
14 15
 domain_name = config['DYNDNS']['baseuri']
15
-logging.basicConfig(filename=config['DYNDNS']['logfile'],level=logging.DEBUG)
16
-# Get the current local IP4
16
+logging.basicConfig(filename=config['DYNDNS']['logfile'],level=logging.INFO)
17 17
 local_ip4 = urllib.request.urlopen("http://ip4.iurl.no").read().decode('utf-8')
18 18
 
19
+# The program
20
+
19 21
 
20
-def updateIP(ip, domain):
22
+def updateip(ip, domain):
21 23
     data = {'data': ip}
22 24
     headers = {'Authorization': 'Bearer ' + api, "Content-Type": "application/json"}
23 25
     response = requests.put('https://api.digitalocean.com/v2/domains/'+domain_name+'/records/' + domain, data=json.dumps(data), headers=headers)
24 26
     return str(response)
25 27
 
26 28
 
27
-for section in config.sections():
28
-    if not config.has_option(section, 'subdomainid'):
29
-        continue
30
-    for name, value in config.items(section):
31
-        req = urllib.request.Request('https://api.digitalocean.com/v2/domains/' + domain_name + '/records/' +
32
-                                     value)
33
-        req.add_header('Content-Type', 'application/json')
34
-        req.add_header('Authorization', 'Bearer ' + api)
35
-        current = urllib.request.urlopen(req)
36
-        remote = current.read().decode('utf-8')
37
-        remoteData = json.loads(remote)
38
-        remoteIP4 = remoteData['domain_record']['data']
39
-        if remoteIP4 != local_ip4:
40
-            response = updateIP(local_ip4, value)
41
-            if '200' in response:
42
-                logging.info('Success! Domain %s updated with IP: %s', section,remoteIP4)
29
+def runupdate():
30
+    for section in config.sections():
31
+        if not config.has_option(section, 'subdomainid'):
32
+            continue
33
+        for name, value in config.items(section):
34
+            req = urllib.request.Request('https://api.digitalocean.com/v2/domains/' + domain_name + '/records/' +
35
+                                         value)
36
+            req.add_header('Content-Type', 'application/json')
37
+            req.add_header('Authorization', 'Bearer ' + api)
38
+            current = urllib.request.urlopen(req)
39
+            remote = current.read().decode('utf-8')
40
+            remoteData = json.loads(remote)
41
+            remoteIP4 = remoteData['domain_record']['data']
42
+            if remoteIP4 != local_ip4:
43
+                response = updateip(local_ip4, value)
44
+                if '200' in response:
45
+                    logging.info('Success! Domain %s updated with IP: %s', section,remoteIP4)
46
+                else:
47
+                    logging.error('Failure! '+response)
43 48
             else:
44
-                logging.error('Failure! '+response)
45
-        else:
46
-            logging.info('No need to update the IP! Local IP ('+local_ip4+') for '+section+' is the same as Remote IP ('+remoteIP4+')')
49
+                logging.info('No need to update the IP! Local IP ('+local_ip4+') for '+section+' is the same as '
50
+                                                                                    'Remote IP ('+remoteIP4+')')
51
+
52
+
53
+def listdomains():
54
+    req = urllib.request.Request('https://api.digitalocean.com/v2/domains/' + domain_name + '/records/')
55
+    req.add_header('Content-Type', 'application/json')
56
+    req.add_header('Authorization', 'Bearer ' + api)
57
+    current = urllib.request.urlopen(req)
58
+    remote = current.read().decode('utf-8')
59
+    remoteData = json.loads(remote)
60
+    for k in remoteData["domain_records"]:
61
+        print('Name : '+k['name']+'  Id : '+str(k['id'])+' (IP: '+str(k['data'])+')')
62
+
63
+
64
+def listcurrent():
65
+    for section in config.sections():
66
+        if not config.has_option(section, 'subdomainid'):
67
+            continue
68
+        for name, value in config.items(section):
69
+            req = urllib.request.Request('https://api.digitalocean.com/v2/domains/' + domain_name + '/records/' +
70
+                                         value)
71
+            req.add_header('Content-Type', 'application/json')
72
+            req.add_header('Authorization', 'Bearer ' + api)
73
+            current = urllib.request.urlopen(req)
74
+            remote = current.read().decode('utf-8')
75
+            remoteData = json.loads(remote)
76
+            remoteIP4 = remoteData['domain_record']['data']
77
+            print(section+' : '+remoteIP4)
78
+
79
+# Commandline arguments
80
+
81
+
82
+parser = argparse.ArgumentParser(description='DigitalOcean Dynamic DNS')
83
+parser.add_argument('-l', '--list', help='List the available sub-domains on the domain supplied in the '
84
+                                         'config.ini file.', required=False, action="store_true")
85
+parser.add_argument('-c', '--current', help='List the current IP address for the sub-domains in the config.ini file',
86
+                    required=False, action="store_true")
87
+args = vars(parser.parse_args())
88
+if args['list']:
89
+    listdomains()
90
+elif args['current']:
91
+    listcurrent()
92
+else:
93
+    runupdate()

Loading…
Cancel
Save