You are not logged in.

#1 2017-06-02 14:08:27

hamhock
Member
Registered: 2015-04-16
Posts: 94

[SOLVED] systemd service to run when my laptop IP changes

I would like to create a systemd service which runs when the IP on my laptop changes (or the netctl profile name changes).

I would prefer a systemd service rather than a bash script that monitors the IP every several seconds - just not sure if this is possible with systemd.

Last edited by hamhock (2017-06-05 18:12:33)

Offline

#2 2017-06-02 14:12:55

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 79,181

Re: [SOLVED] systemd service to run when my laptop IP changes

Offline

#3 2017-06-04 17:45:06

hamhock
Member
Registered: 2015-04-16
Posts: 94

Re: [SOLVED] systemd service to run when my laptop IP changes

seth wrote:

thank you - it's looks like what I need.

only issue i'm having is that i'm trying to assign the IP the dhcp server gives me to a variable in the script, but i'm getting the IP from the previous SSID.
/etc/netctl/hooks/test.sh

#!/bin/sh

myip="$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')"

if [ "$SSID" = "homessid" ]
then
  echo $myip > /tmp/homeip    
else
  echo $myip > /tmp/androidip
fi

when I run the code both if/else lines execute and give unexpected results:

when I change from home to android hotspot:
/tmp/homeip = correct home ip, but I don't want or need anymore.  this should not have executed
/tmp/androidip = null, but I need the ip for the Android hotspot.

when I change from android hotspot back to home:
/tmp/homeip = null, but need the ip for home LAN
/tmp/androidip = correct android ip, but I don't want or need anymore.  this should not have executed

Last edited by hamhock (2017-06-04 17:45:39)

Offline

#4 2017-06-04 20:16:52

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 79,181

Re: [SOLVED] systemd service to run when my laptop IP changes

First off, try

ip -4 -o addr show <devicename> | awk '{print substr($4, 1, length($4)-3)}'

(or sed if you want)

Second, you didn't mention "ExecUpPost", so I take you didn't make use of it and just execute a script in the hook?

Offline

#5 2017-06-04 22:19:28

hamhock
Member
Registered: 2015-04-16
Posts: 94

Re: [SOLVED] systemd service to run when my laptop IP changes

seth wrote:

First off, try

ip -4 -o addr show <devicename> | awk '{print substr($4, 1, length($4)-3)}'

(or sed if you want)

yes - that works - more elegant than what I had before.

seth wrote:

Second, you didn't mention "ExecUpPost", so I take you didn't make use of it and just execute a script in the hook?

correct. 

here is my initial attempt with "ExecUpPost" - the "myip.sh" is successfully obtaining the new IP, but the script in /etc/netctl/hooks is getting the previous IP and both echo lines are firing. 

etc/netctl/hooks/test

#!/bin/sh

ExecUpPost='/usr/local/bin/myip.sh'

 file="/tmp/myip"
 newip=$(head -n 1 $file)

if [ "$Profile" = "homessid" ]
then
  echo $newip > /tmp/homeip
else
  echo $newip > /tmp/androidip
fi

/usr/local/bin/myip.sh

#!/bin/bash

myip="$(ip -4 -o addr show wlp1s0 | awk '{print substr($4, 1, length($4)-3)}')"

echo $myip > /tmp/myip

Last edited by hamhock (2017-06-05 03:27:52)

Offline

#6 2017-06-05 06:38:30

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 79,181

Re: [SOLVED] systemd service to run when my laptop IP changes

Ok, the hook should only assing a script (though I think a function would work as well) to ExecPost and have all the payload there (if you want to operate on data valid only after the profile has been switched)

/etc/netctl/hooks/test

#!/bin/sh
ExecUpPost="/usr/local/bin/myip.sh $Profile"

/usr/local/bin/myip.sh

#!/bin/sh
if [ "$1" = "homessid" ]
then
    ip -4 -o addr show wlp1s0 | awk '{print substr($4, 1, length($4)-3)}' > /tmp/homeip
else
    ip -4 -o addr show wlp1s0 | awk '{print substr($4, 1, length($4)-3)}' > /tmp/androidip
fi

--- OR ---
/etc/netctl/hooks/test

#!/bin/sh
myIP() {
if [ "$1" = "homessid" ]
then
    ip -4 -o addr show wlp1s0 | awk '{print substr($4, 1, length($4)-3)}' > /tmp/homeip
else
    ip -4 -o addr show wlp1s0 | awk '{print substr($4, 1, length($4)-3)}' > /tmp/androidip
fi
}
ExecUpPost="myIP $Profile"

But idk whether the hooks can assign local functions.

Offline

#7 2017-06-05 15:58:49

hamhock
Member
Registered: 2015-04-16
Posts: 94

Re: [SOLVED] systemd service to run when my laptop IP changes

seth wrote:

Ok, the hook should only assing a script (though I think a function would work as well) to ExecPost and have all the payload there (if you want to operate on data valid only after the profile has been switched).

Ahh - now I get it - thank you for getting me back on track.  I adapted my code to your first example and all is working as expected now.  I'll mark as solved.

Offline

Board footer

Powered by FluxBB