I need to find a way to control some paramenters of my routers while using VPNC (an IPSec VPN CLient). When it either connects to the VPN or disconnects I need some actions to be taken (turn on WPS led, routes, fix dns, etc), a simple bash script apparently does the trick but I need to have it running at startup. So I basically have to monitor the state of the tunnel interface and take action when this state changes:
#!/bin/sh
state=0
while :
do
operstate=$(cat /sys/devices/virtual/net/vpn-tun0/operstate)
operstatewan=$(cat /sys/devices/platform/ag71xx.0/net/eth0/operstate)
if [[ -e /sys/class/net/vpn-tun0 ]] && [[ "$operstate" == "unknown" ]] && [[ "$operstatewan" == "up" ]];
then
if [ $state -eq 0 ]
then
echo 1 > /sys/devices/platform/leds-gpio/leds/tp-link:blue:qss/brightness
logger vpn state up
route add -net 172.16.0.0 netmask 255.240.0.0 dev vpn-tun0
logger vpn route configured
state=1
fi
else
if [ $state -eq 1 ]
then
echo 0 > /sys/devices/platform/leds-gpio/leds/tp-link:blue:qss/brightness
logger vpn state is down dnsmasq will be restarted
/etc/init.d/dnsmasq restart
state=0
fi
fi
sleep 5
done
What is the best way to turn it into a daemon, I guess using init.d is the way, but I ended up bricking one of my routers trying to achieve that so before I brick another device I need some help. I tryed the following without good results.
Why do you talk specifically about "bash" in the topic title, while your script is written for "sh" and is actually run by "ash" in busybox. Or have you really installed the bash shell and defined that it is used?
You might achieve the daemonising also by launching the script in a detached shell from /etc/rc.local (that is run at the end of the boot process)
e.g. add this to /etc/rc.local:
( script_vpn.sh )&
Ps. make sure to specify the correct directory or place the script on a directory that is in path.
Inacurate title now corrected. I like your suggestion. I'll try that. I would also like to mention that this script_vpn.sh is older version and possibly not working quite well. I'll correct that when topic is solved.
I recall that VPNC calls a script called "vpnc-script" on connect and disconnect, so you could also try to add your commands there. In LEDE it seems to be located in "/lib/netif" if you've installed the "vpnc-scripts" package.
You can also use /etc/init.d/ to kick off these scripts. You can, in theory, put the script itself directly into init.d or simply create a new script that simply launches the existing script. init.d lets you define the start order and gives you the ability to define start, stop, and restart behaviors. And you can then enable/disable the script by using: /etc/init.d/<script_name> enable (or disable)
or use the LuCI startup page.
Thanks, I'll take a look at it and it looks like the proper way to go. If hotplug uses operstate files as references we might find some problems though. First wan operstate is up in some devices even if there is no cable attached to the interface (as far as I know it has to do with hardware architecture), vpnc creates a tunnel where the operstate is "unknown" when it's up. Sure it can be overcome.