#!/bin/sh
  #         
  # license: Standard BSD2CLAUSE (BSD 2-clause Simplified License),
  # Please read from the web.
  #         
            
  #         
  # Script to force the daily updating of the dynamic DNS IP address at 
  # your dynamic DNS hosting provider.
  #
  # NOTE: Most DNS hosting providers that provide dynamic DNS services use a
  # variation of the default browser URL format used here. A simple user
  # substitution to the "fetch" command for the browser URL format used by your
  # dynamic DNS hosting provider will allow this script to work for you.
  #
  # See man dynip for details.
  #
  # On purpose the new IP address is not included in the standard browser
  # style URL format because the dynamic DNS hosting providers update process
  # will use the IP address of the sending update request as the value to
  # update their records to.

            
  prev_ip_file="/usr/local/etc/dynip"
            
  # Find the NIC device name of the NIC connected to the public internet.
  # The default interface obtained from route command is the one connected
  # to the public internet.
  #         
  nic_devicename=$(route get -inet default 2> /dev/null | \
    grep -o "interface.*" | cut -d ' ' -f 2)
            
            
  # Get the IP address assigned to that NIC device name.
  current_ip=$(ifconfig $nic_devicename inet | \
    grep -m 1 -o inet.* | cut -d ' ' -f 2)
            
            
  #         
  # Update dynamic DNS hosting provider with new IP address.
  #
  #   Insert best match example from man dynip here.

  #################### start insert here ##########################

  echo "/usr/local/etc/periodic/daily/dynip_daily"
  echo "Has not been customized yet, terminated"
  exit 99

  ####################  end  insert here ##########################


  # For testing, all messages go to screen
  #fetch -q -o - ${url}

  # For Production usage
  fetch --user-agent="dynip Freebsd" -q -o /var/log/dynip.log ${url}
            
  if [ $? -ne 0 ]; then
    log_msg="Error: /usr/bin/fetch command failed."
    echo "$log_msg"
            
    # Send email to host root to inform about update error.
    subject="DYNIP Status report: Update error."
    echo "$log_msg" | /usr/bin/mail -s "${subject}" root@$(hostname)
    exit 3  
  fi
            
  echo "${current_ip}" > ${prev_ip_file}
  log_msg="Daily Success: Dynamic DNS service updated:"
  log_msg="${log_msg} IP address set to ${current_ip}"
  #echo "$log_msg"
            
  # Send email to host root to inform about Daily Dynamic DNS service updated.
  subject="Daily DYNIP Status report: Dynamic DNS service updated."
  echo "$log_msg" | /usr/bin/mail -s "${subject}" root@$(hostname)
  exit 0


