diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-07-10 10:36:10 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-07-10 10:36:10 +0300 |
| commit | ffd25cdb04f6a40bc9b95afd507042717b2f7fc5 (patch) | |
| tree | ee519bceb4089e9498f67673e54a59e0a96ae309 | |
| parent | 8b9455adfd32708719d3534e21dff3a041efc9ee (diff) | |
Generate specific dhclient.conf to coerce the ISP to offer the same IP
| -rw-r--r-- | tasks/general.yml | 15 | ||||
| -rw-r--r-- | tasks/network.yml | 8 | ||||
| -rw-r--r-- | templates/etc_crontab.j2 | 24 | ||||
| -rw-r--r-- | templates/usr_local_bin_gen_dhclient_conf.sh.j2 | 38 |
4 files changed, 85 insertions, 0 deletions
diff --git a/tasks/general.yml b/tasks/general.yml index e9c1ef3..902d6c4 100644 --- a/tasks/general.yml +++ b/tasks/general.yml @@ -43,3 +43,18 @@ service: name: auditd state: started + +- name: Set up crontab + template: + src: etc_crontab.j2 + dest: /etc/crontab + owner: root + group: wheel + mode: "0644" + register: etc_crontab + +- name: Restart cron + service: + name: cron + state: restarted + when: etc_crontab.changed diff --git a/tasks/network.yml b/tasks/network.yml index 45f44e6..dbfa43c 100644 --- a/tasks/network.yml +++ b/tasks/network.yml @@ -142,3 +142,11 @@ name: do_dyndns state: restarted when: not do_dyndns_start.changed and (do_dyndns_sh.changed or do_dyndns_rc.changed) + +- name: Install gen_dhclient_conf.sh + template: + src: usr_local_bin_gen_dhclient_conf.sh.j2 + dest: /usr/local/bin/gen_dhclient_conf.sh + owner: root + group: wheel + mode: "0755" diff --git a/templates/etc_crontab.j2 b/templates/etc_crontab.j2 new file mode 100644 index 0000000..cdd34a4 --- /dev/null +++ b/templates/etc_crontab.j2 @@ -0,0 +1,24 @@ +# /etc/crontab - root's crontab for FreeBSD +# +# +SHELL=/bin/sh +PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin +# +#minute hour mday month wday who command +# +# Save some entropy so that /dev/random can re-seed on boot. +*/11 * * * * operator /usr/libexec/save-entropy +# +# Rotate log files every hour, if necessary. +0 * * * * root newsyslog +# +# Perform daily/weekly/monthly maintenance. +1 3 * * * root periodic daily +15 4 * * 6 root periodic weekly +30 5 1 * * root periodic monthly +# +# Adjust the time zone if the CMOS clock keeps local time, as opposed to +# UTC time. See adjkerntz(8) for details. +1,31 0-5 * * * root adjkerntz -a +# Generate /etc/dhclient.conf +*/15 * * * * root sh -c 'gen_dhclient_conf.sh | logger -t gen_dhclient_conf' diff --git a/templates/usr_local_bin_gen_dhclient_conf.sh.j2 b/templates/usr_local_bin_gen_dhclient_conf.sh.j2 new file mode 100644 index 0000000..8c05f70 --- /dev/null +++ b/templates/usr_local_bin_gen_dhclient_conf.sh.j2 @@ -0,0 +1,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 |
