Skip to content

Instantly share code, notes, and snippets.

@cernoel
Created May 17, 2022 17:25
Show Gist options
  • Save cernoel/2233608e558b3d7838ebac32b6e208cb to your computer and use it in GitHub Desktop.
Save cernoel/2233608e558b3d7838ebac32b6e208cb to your computer and use it in GitHub Desktop.
syncs /etc/hosts from mydns1 and restart dnsmasq if file has changed
#!/bin/bash
# more strict, if something goes wrong, script stops
set -e
workdir=/root/dns_syncer
newhostsfile=$workdir/newhosts
# copy file from primary
scp -q -o LogLevel=QUIET unpriv@mydns1:/etc/hosts $newhostsfile
# this of course needs a functional ssh-key / authorized_key configuration
# if file not exists
if [ ! -f $newhostsfile ]; then
exit 0;
fi;
oldhash=($(md5sum /etc/hosts | xargs))
newhash=($(md5sum $newhostsfile | xargs))
if [ "$oldhash" == "$newhash" ]; then
exit 0;
fi;
cp -f /etc/hosts /etc/hosts.old
mv -f $newhostsfile /etc/hosts
systemctl restart dnsmasq
cat /etc/hosts.old
cat /etc/hosts
echo "done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment