[Discuss] Network manager fun

Matthew Gillen me at mattgillen.net
Sun Aug 30 12:40:10 EDT 2020


I do some things that usually aren't done on DHCP hosts, so I needed to
know when my IP address changed or if my ISP updated their DNS servers
provided via DHCP.  Should be a simple cron job right?

Sort of. A few years ago I stopped fighting against the current and not
only gave in and started using network manager, but retired my hand
written iptables script I'd started circa 2000 (in favor or firewalld).

Network manager doesn't write out the lease to a file like the old
dhclient did though, so I had to learn some new stuff.  Turns out you
can use nmcli to get the lease info.  But at least the way I found to do
it used a connection name that was associated with the firewalld zone,
NOT the actual interface name!

So my ham-fisted cron job looks like this ('ext' is the firewalld zone
for the ISP-facing interface):
------------(sorry about the 80-char limit)

$ cat check-my-ip.sh
#!/bin/bash
OLDDNS="x.x.x.x y.y.y.y"
OLDIP="z.z.z.z"

CURRENTDNS=`nmcli -f DHCP4 con show ext | grep "DHCP4.OPTION\[6\]" | awk
'{ print $4 " " $5 }'`
CURRENTIP=`nmcli -f DHCP4 con show ext | grep "DHCP4.OPTION\[8\]" |  awk
'{ print $4 }'`

if [ "$OLDDNS" == "$CURRENTDNS" ] && [ "$OLDIP" == "$CURRENTIP" ]; then
	#echo "Ok!"
	exit 0
else
	echo "Change! Current IP: $OLDIP / $CURRENTIP ; '$OLDDNS' / '$CURRENTDNS'"
	exit 1
fi

------------------------

I looked a bit to see if NetworkManager had some built-in notification
mechanisms, but didn't see any.  If someone knows a better way I'd love
to hear it.

Thanks,
Matt


More information about the Discuss mailing list