Annotation of embedaddon/dnsmasq/contrib/mactable/macscript, revision 1.1

1.1     ! misho       1: #!/bin/bash
        !             2: 
        !             3: STATUS_FILE="/tmp/dnsmasq-ip-mac.status"
        !             4: 
        !             5: # Script for dnsmasq lease-change hook.
        !             6: # Maintains the above file with a IP address/MAC address pairs,
        !             7: # one lease per line. Works with IPv4 and IPv6 leases, file is
        !             8: # atomically updated, so no races for users of the data.
        !             9: 
        !            10: action="$1"
        !            11: mac="$2"   # IPv4
        !            12: ip="$3"
        !            13: 
        !            14: # ensure it always exists.
        !            15: 
        !            16: if [ ! -f "$STATUS_FILE" ]; then
        !            17:   touch "$STATUS_FILE"
        !            18: fi
        !            19: 
        !            20: if [  -n "$DNSMASQ_IAID" ]; then
        !            21:     mac="$DNSMASQ_MAC"   # IPv6
        !            22: fi
        !            23: 
        !            24: # worry about an add or old action when the MAC address is not known:
        !            25: # leave any old one in place in that case.
        !            26: 
        !            27: if [ "$action" = "add" -o "$action" = "old" -o "$action" = "del" ]; then
        !            28:   if [ -n "$mac" -o "$action" = "del" ]; then
        !            29:     sed "/^${ip//./\.} / d" "$STATUS_FILE" > "$STATUS_FILE".new
        !            30:   
        !            31:     if [ "$action" = "add" -o "$action" = "old" ]; then
        !            32:        echo "$ip $mac" >> "$STATUS_FILE".new
        !            33:     fi
        !            34:     mv  "$STATUS_FILE".new "$STATUS_FILE" # atomic update.
        !            35:   fi
        !            36: fi

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>