File: macscript

package info (click to toggle)
dnsmasq 2.92-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,120 kB
  • sloc: ansic: 37,139; perl: 643; sh: 632; makefile: 210; python: 136; xml: 53
file content (36 lines) | stat: -rwxr-xr-x 952 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash

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

# Script for dnsmasq lease-change hook.
# Maintains the above file with an IP address/MAC address pairs,
# one lease per line. Works with IPv4 and IPv6 leases, file is
# atomically updated, so no races for users of the data.

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

# ensure it always exists.

if [ ! -f "$STATUS_FILE" ]; then
  touch "$STATUS_FILE"
fi

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

# worry about an add or old action when the MAC address is not known:
# leave any old one in place in that case.

if [ "$action" = "add" -o "$action" = "old" -o "$action" = "del" ]; then
  if [ -n "$mac" -o "$action" = "del" ]; then
    sed "/^${ip//./\.} / d" "$STATUS_FILE" > "$STATUS_FILE".new
  
    if [ "$action" = "add" -o "$action" = "old" ]; then
       echo "$ip $mac" >> "$STATUS_FILE".new
    fi
    mv  "$STATUS_FILE".new "$STATUS_FILE" # atomic update.
  fi
fi