don't hard-code the path to the python interpreter
[dyn-nsupdate.git] / client-scripts / dyn-ns-client
index af263b76aca5111812a19a38ffbcc4f6b9714c23..383c1150941dad94ce7f273b815f99738f2bf977 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python3
+#!/usr/bin/env python3
 # Copyright (c) 2014, Ralf Jung <post@ralfj.de>
 # All rights reserved.
 # 
@@ -23,7 +23,7 @@
 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #==============================================================================
 
-import urllib.request, socket, sys, argparse, os, configparser, itertools
+import urllib.request, socket, sys, argparse, os, configparser, itertools, subprocess, re
 
 def readConfig(fname, defSection = 'DEFAULT'):
     config = configparser.ConfigParser()
@@ -54,7 +54,7 @@ def getMyIP(family, config, methods = {}, verbose = False):
         if verbose:
             print("Server",server,"says my",family,"is",ip)
         return ip
-    elif method in 'methods':
+    elif method in methods:
         return methods[method]()
     else:
         raise Exception("Unsupported "+family+" detection method: "+method)
@@ -65,7 +65,19 @@ def getMyIPv4(config, verbose = False):
 
 def getMyIPv6(config, verbose = False):
     '''Returns our current IPv6 address, detected as given by the configuration'''
-    return getMyIP("IPv6", config, verbose=verbose)
+    def local():
+        out = subprocess.check_output(["ip", "addr"])
+        for line in out.decode('utf-8').split('\n'):
+            m = re.search('inet6 ([a-fA-F0-9:]+)/64 ([a-zA-Z0-9 ]*)', line)
+            if m is not None:
+                ip = m.group(1)
+                flags = m.group(2).split()
+                if not 'temporary' in flags and not 'deprecated' in flags:
+                    if verbose:
+                        print("Local IPv6 detected to be",ip)
+                    return ip
+        raise Exception("Unable to detect correct local IPv6 address")
+    return getMyIP("IPv6", config, methods={'local': local}, verbose=verbose)
 
 def getCurIP(domain, family):
     '''Return the current IP of the given <domain>. <family> can be socket.AF_INET or socket.AF_INET6.'''
@@ -93,7 +105,7 @@ def updateDomain(server, domain, ipv4, ipv6, password, verbose):
     curIPv4 = getCurIPv4(domain)
     curIPv6 = getCurIPv6(domain)
     if verbose:
-        print("Current status of domain {0} is: IPv4 address '{1}', IPv6 address '{2}'".format(domain, curIPv4, curIPv6))
+        print("Current status of domain {} is: IPv4 address '{}', IPv6 address '{}'".format(domain, curIPv4, curIPv6))
     
     # check if there's something to do
     needUpdate = (ipv4 is not None and curIPv4 != ipv4) or (ipv6 is not None and curIPv6 != ipv6)