From 5335570c6f67a5a2bb29f1f16b66d503e1d72a6e Mon Sep 17 00:00:00 2001 From: Jonathan Kamens Date: Sat, 13 May 2017 16:52:33 -0400 Subject: [PATCH] Support obtaining interface name from default route Allow the user to specify "ddclient" to the "if" option to tell ddclient to run `route -n` and look at the default route in its output to determine which interface to query for the IP address. --- ddclient | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ddclient b/ddclient index 6053c1c..83582ca 100755 --- a/ddclient +++ b/ddclient @@ -710,6 +710,7 @@ my @opt = ( [ "ip", "=s", "-ip address : set the IP address to 'address'" ], "", [ "if", "=s", "-if interface : obtain IP address from 'interface'" ], + " Specify 'defaultroute' to obtain interface name from default route.", [ "if-skip", "=s", "-if-skip pattern : skip any IP addresses before 'pattern' in the output of ifconfig {if}" ], "", [ "web", "=s", "-web provider|url : obtain IP address from provider's IP checking page" ], @@ -2164,10 +2165,22 @@ sub get_ip { $arg = 'ip'; } elsif ($use eq 'if') { + my $iface; $skip = opt('if-skip', $h) || ''; - $reply = `ifconfig $arg 2> /dev/null`; - $reply = `ip addr list dev $arg 2> /dev/null` if $?; - $reply = '' if $?; + if ($arg eq 'defaultroute') { + ($iface) = (grep(/^0\.0\.0\.0\b/, `route -n`))[0] =~ /(\S+)$/; + } + else { + $iface = $arg; + } + if (! $iface) { + $reply = ''; + } + else { + $reply = `ifconfig $iface 2> /dev/null`; + $reply = `ip addr list dev $iface 2> /dev/null` if $?; + $reply = '' if $?; + } } elsif ($use eq 'cmd') { if ($arg) {