[Dnsmasq-discuss] Reg: Info related to leases file

Lonnie Abelbeck lists at lonnie.abelbeck.com
Wed Sep 25 21:52:40 BST 2013


On Sep 25, 2013, at 11:20 AM, Simon Kelley wrote:
> 
> HAVE_SCRIPT isn't very big and this tiny shell script makes a file of (IP-address, MAC-address) pairs. It's trivial to alter it to split IPv4 and IPv6 into different files or include any information from the fields exposed by the script interface. The script interface is defined and I'll keep it compatible going forward.
> 
> 
> #!/bin/bash
> 
> MACFILE=/tmp/macfile
> 
> action=${1:-0}
> ip=$3
> mac=$2 # IPv4
> 
> if [ ${DNSMASQ_IAID} ] ; then
>   mac=${DNSMASQ_MAC} # IPv6
> fi
> 
> if [ ! -f ${MACFILE} ] ; then
>    touch ${MACFILE}
> fi
> 
> 
> if [ ${action} = add ] ||
>   [ ${action} = old ] ||
>   [ ${action} = del ] ; then
>    grep -v ^${ip} ${MACFILE} >${MACFILE}.new
>    if [ ${action} = add ] ||
>       [ ${action} = old ] ; then
> 	echo ${ip} ${mac} >> ${MACFILE}.new
>    fi
>    mv ${MACFILE}.new ${MACFILE}
> fi
> 
> 
> Cheers,
> 
> Simon.

Hi Simon,

May I be so bold to offer a couple tweaks to your script... :-)

1) Use 'sed' -i' instead of 'grep -v' plus a temp file

2) Include the trailing 'space' in the sed/grep match (Important)

3) Escape the dot's in a IPv4 address in the sed/grep match (unlikely to be a problem, but possible)

4) Add many quotes, (agree some are not required)

=========================
#!/bin/bash

action="$1"
mac="$2"   # IPv4
ip="$3"

STATUS_FILE="/tmp/dnsmasq-ip-mac.status"

if [ -n "$DNSMASQ_IAID" ]; then
  mac="$DNSMASQ_MAC"   # IPv6
fi

if [ "$action" = "add" -o "$action" = "old" -o "$action" = "del" ]; then
  if [ -f "$STATUS_FILE" ]; then
    sed -i "/^${ip//./\.} / d" "$STATUS_FILE"
  fi
  if [ "$action" = "add" -o "$action" = "old" ]; then
    echo "$ip $mac" >> "$STATUS_FILE"
  fi
fi
=========================

Lonnie




More information about the Dnsmasq-discuss mailing list