4

I already have ddclient 3.8.2 working with OpenDNS. I'm running it on my laptop. I may travel with my laptop, and so I don't want to update the IP if I'm not home.

How do I restrict ddclient to update the IP only if I'm on my home network? This selectively has to be automated.

There are at least two ways to define whether I'm on my home network:

  • Assume I use wifi, and that my home wifi SSID name is home-ssid. Assume I can configure this name.

  • Alternatively, assume the MAC address of my router is F7:C1:A2:54:4F:71 (fake). Assume I can configure this value. This approach is intended to work for both wifi and/or wired.

If nothing else, a hack could be to use use=cmd instead of use=web in /etc/ddclient.conf. I can then potentially have the external command intelligently fail or return an unchanged IP if I'm not home. For this purpose, note that /var/cache/ddclient/ddclient.cache caches the last known IP.

Please provide a complete working solution.

1
  • If you think one of the two answers is what you want, then accept the answer by putting the green check mark on its left margin. This will help others.
    – user68186
    Jun 27, 2018 at 21:03

2 Answers 2

4

I have two custom solutions you might be able to utilize. One uses Wifi SSID only, the other uses MAC address determination.

Both versions have a prerequisite of you having a working ddclient configuration for OpenDNS.


Step 1 for both: Disable ddclient automatic executions.

I know the guide you used to set this up had you set it up as a daemon, but we are going to disable the automated ddclient update processes.

Edit /etc/default/ddclient. We're going to disable the daemon mode to start with, and disable dhclient and ipup integrations. That should make it look similar to this:

# Configuration for ddclient scripts
# generated from debconf on Tue Jun 26 12:45:45 EDT 2018
#
# /etc/default/ddclient

# Set to "true" if ddclient should be run every time DHCP client ('dhclient'
# from package isc-dhcp-client) updates the systems IP address.
run_dhclient="false"

# Set to "true" if ddclient should be run every time a new ppp connection is
# established. This might be useful, if you are using dial-on-demand.
run_ipup="false"

# Set to "true" if ddclient should run in daemon mode
# If this is changed to true, run_ipup and run_dhclient must be set to false.
run_daemon="false"

# Set the time interval between the updates of the dynamic DNS name in seconds.
# This option only takes effect if the ddclient runs in daemon mode.
daemon_interval="300"

Now, disable the ddclient service so it will not autorun. I only have the 16.04 and later syntax to disable the service, but there are other threads on "How to disable services".

sudo systemctl disable ddclient

After this is done, you can use either of the two options below and their instructions to set up customized automatic processes.

Step 2: Decide which approach you want to use: Wifi-only SSID detection, or MAC address detection. Depending on which solution you wish to go with, follow the specific section below.


Wifi Only: SSID Detection

This is the python code itself for an SSID-based detection. Put the proper Wifi SSID in for the value stored in WIFI_NETWORK_NAME.

#!/usr/bin/python3

import shlex
import subprocess as sp

# Replace 'FillMeIn' in this line with the actual network name.
WIFI_NETWORK_NAME = "FillMeIn"

proc = sp.getoutput(shlex.split('iwconfig'))

if WIFI_NETWORK_NAME in proc:
    sp.call(shlex.split('ddclient -file /etc/ddclient.conf'))

Store this code in /opt/py-selective-ddclient.py. You may have to store this in your home directory and then copy it into /opt/ with sudo.


Wifi and Ethernet: MAC Address Detection

This is the Python code for this solution; update the value for MAC_ADDRESS_ROUTER accordingly:

#!/usr/bin/python3

import shlex
import subprocess as sp

# Replace the fake MAC address below with the MAC address of your router.
# Make sure to use **uppercase letters** if you have letters present.
MAC_ADDRESS_ROUTER = "01:23:45:67:89:AB"

if MAC_ADDRESS_ROUTER in sp.getoutput(shlex.split('iwgetid -ra')).upper():
    sp.call(shlex.split('ddclient -file /etc/ddclient.conf'))

Store this code in /opt/py-selective-ddclient.py. You may have to store this in your home directory and then copy it into /opt/ with sudo.


Step 3: Automate the Python code calls.

Now, we have to automate running ddclient, or more specifically automate the Python script. The default ddclient settings have a 300 second (5 minute) delay between update checks.

First we need to make the Python script executable.

sudo chmod +x /opt/py-selective-ddclient.py

Then, start by making a file in /etc/cron.d/ with the following contents. Note that you will need to use sudo to create the file.

*/5 * * * *  root  /opt/py-selective-ddclient.py

This executes the ddclient manually, every 5 minutes. You can change the */5 part to make it execute at different intervals, but I am emulating ddclient's setup here.

Now, every 5 minutes ddclient will be called using our 'wrapper' script which does the selective determination.

0
3

These scripts require a working ddclient configuration for OpenDNS.

Here is a bash version that checks home wifi network name

#!/bin/bash 
# Read the name of the Wi-Fi network the computer is conneted to
# If not connected to wifi echo message and quit
# If home network run ddclient
# If any other network quit

# Change FillMeIn to your WiFi network name
HOMEWIFI= FillMeIn 

# Get the current WiFi network name
SSID=$(iwgetid -r)

if   [[ ! $SSID ]]; then        # Not on WiFi
    echo "Could not find any WiFi, exiting..."
elif [[ $SSID == $HOMEWIFI ]]; then
    echo "We are home! Running ddclient now."
    /usr/sbin/ddclient -file /etc/ddclient.conf
else
    echo "This WiFi is not home! Exiting..."
fi

If you want a less verbose minimal code, use the following lines:

#!/bin/bash 
# Change FillMeIn to your WiFi network name
HOMEWIFI= FillMeIn 
SSID=$(iwgetid -r)      # -r for SSID
if [[ $SSID == $HOMEWIFI ]]; then
    /usr/sbin/ddclient -file /etc/ddclient.conf
fi

Here is a bash version that checks home router's MAC address

#!/bin/bash 
# Change FillMeIn to your router's MAC address
ROUTERMAC= FillMeIn 
$MACA=$(iwgetid -ra)      # -ra for MAC address
if [[ $MACA == $ROUTERMAC ]]; then
    /usr/sbin/ddclient -file /etc/ddclient.conf
fi

You need only one of these. Store either of the three scripts in /opt/home_wifi_ddclient.sh and make the script executable:

sudo chmod +x /opt/home_wifi_ddclient.sh

To create the cron entry I suggest using crontab:

sudo crontab -e

This command will open the crontab file for the root user if one exists, or create a blank new file. Add the following line at the end of the file:

*/5 * * * *  root  /opt/home_wifi_ddclient.sh

The */5 part means the script will run every 5 minutes.

If you use nano as the text file editor, exit the editor by pressing Ctrl+X. The editor will prompt you to save the changes. Press Y and then press Enter to select the default file name.

Hope this helps

1
  • 1
    If you want to use a MAC address, change iwgetid -r to iwgetid -ra.
    – luk3yx
    Jun 27, 2018 at 0:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .