aboutsummaryrefslogtreecommitdiffstats
path: root/templates/usr_local_bin_gen_dhclient_conf.sh.j2
blob: 8c05f7070a12cba88842e47fc65c823801209f5f (plain)
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
37
38
#!/bin/sh

set -eu

IP=$(ifconfig wan0 | awk '/inet / {print $2}' | head -n1)
HOSTNAME=$(hostname)
CONFFILE=/etc/dhclient.conf
NEWFILE=$(mktemp)
if [ -f "${CONFFILE}" ]; then
    OLDHASH=$(md5 -q "${CONFFILE}")
else
    OLDHASH="doesnotexist"
fi

cat <<EOF > "${NEWFILE}"
# This file is generated by $0 periodically.
# Changes will be overwritten.

interface "wan0" {
    send host-name "${HOSTNAME}";
EOF

if [ ! -z "$IP" ]; then
cat <<EOF >> "${NEWFILE}"
    send dhcp-requested-address $IP;
EOF
fi

echo "}" >> "${NEWFILE}"

mv "${NEWFILE}" "${CONFFILE}"

# If changed, restart dhclient wan0
md5 -c "${OLDHASH}" "${CONFFILE}" 1>/dev/null 2>/dev/null
if [ ! "$?" = 0 ]; then
    echo "Generated ${CONFFILE} has changed, restarting dhclient on wan0 (IP: ${IP})"
    service dhclient restart wan0
fi