0

I would like to trigger a service when a change of an ip address on a specific interface occurs. Is there a target for this or some other method I am not aware of to achieve this using systemd on Linux (Kernel 3.19)?

The service would be used to send a SIGNAL to a defined process. The Linux is running on an embedded system.

Thanks!

user3482407
  • 319
  • 3
  • 18

2 Answers2

1

There is an solution in other question of StackOverflow. Just here: Detecting a change of IP address in Linux

I like this code, it's easy, you onli need a cron job with frecuency as you need (I made a little change):

#!/bin/bash
OLD_IP=`cat ip.txt`
NEW_IP=`/sbin/ifconfig  | awk -F "[: ]+'{ print $4}'`
if [ $NEW_IP != OLD_IP ]; then
    YOU_COMMAND <commands> 
    echo $NEW_IP > ip.txt
fi
exit 0
Community
  • 1
  • 1
FOP
  • 962
  • 1
  • 10
  • 21
1

Because you use Systemd you might already use systemd-networkd for managing your devices instead of relying on 3rd party code.

You could use the structured journal output to get the last 2 ADDRESS field of the current BOOD_ID.(sadly, there is no notification mechanism for address changes in systemd-networkd):

→ sudo journalctl -F ADDRESS -u systemd-networkd -n 2
192.168.178.29

So, if there is only one line output, there was no address change.

Community
  • 1
  • 1
Jürgen Hötzel
  • 18,997
  • 3
  • 42
  • 58