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.
This commit is contained in:
Jonathan Kamens 2017-05-13 16:52:33 -04:00
parent 94dc35984f
commit 5335570c6f

View file

@ -710,6 +710,7 @@ my @opt = (
[ "ip", "=s", "-ip address : set the IP address to 'address'" ], [ "ip", "=s", "-ip address : set the IP address to 'address'" ],
"", "",
[ "if", "=s", "-if interface : obtain IP address from 'interface'" ], [ "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}" ], [ "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" ], [ "web", "=s", "-web provider|url : obtain IP address from provider's IP checking page" ],
@ -2164,10 +2165,22 @@ sub get_ip {
$arg = 'ip'; $arg = 'ip';
} elsif ($use eq 'if') { } elsif ($use eq 'if') {
my $iface;
$skip = opt('if-skip', $h) || ''; $skip = opt('if-skip', $h) || '';
$reply = `ifconfig $arg 2> /dev/null`; if ($arg eq 'defaultroute') {
$reply = `ip addr list dev $arg 2> /dev/null` if $?; ($iface) = (grep(/^0\.0\.0\.0\b/, `route -n`))[0] =~ /(\S+)$/;
$reply = '' if $?; }
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') { } elsif ($use eq 'cmd') {
if ($arg) { if ($arg) {