danb1983
Posts: 5
Joined: Mon Jan 09, 2017 12:47 am

Run script on eth0 connected / disconnected

Fri May 05, 2017 1:57 pm

Hello,

I have a Raspberry Pi which is connected to the internet through a USB rocket stick. I also have it connected to a switch on local network via Ethernet (eth0 interface). This switch allows me to turn the ports up or down manually. If the rocket stick goes down, I want to be able to get access to the unit through the other network. If the eth0 port is connected, I want the rocket stick to disconnect. I do not want both interfaces up at the same time. I have a script that will do this but I cannot get it to run when the eth0 interface is connected. I have tried the script by itself and it works with no issues. I have placed the script in the /etc/network/if-up.d/ directory but it still will not run when I connect the interface. It only runs when the device boots.

Any help would be appreciated.

Thanks,

epoch1970
Posts: 4206
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Run script on eth0 connected / disconnected

Fri May 05, 2017 8:11 pm

If you are using dhcpcd, then perhaps you can have a look at "hooks", that is user scripts executed by dhcpcd every time an interfaces comes up or down.
I don't use it myself, so I won't help much here.

Otherwise, I would think ifplugd can catch the "cable (dis)connected" event and could trigger a custom script.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

pi_everalm
Posts: 33
Joined: Thu Apr 20, 2017 11:44 am

Re: Run script on eth0 connected / disconnected

Sat May 06, 2017 11:19 am

Run a cron job of your script every 5-10 minutes..? Not automated real time but probably meets most of your needs assuming your two connections don't swap around like a fiddlers elbow.

Return to “Beginners”