How can I run a shell script when some WiFi is connected? I have a really simple shell script, that just adds a route to my internal development machine. This is useful, as that machine has some LXC containers inside, so I can reach all my containers from my cell phone (to test websites from mobile browser, or Asterisk SIP accounts from CSipSimple)

#!/system/bin/sh
su -c "ip route add 10.0.3.0/24 via 192.168.0.200 dev wlan0"


To use this rule, I've created a new Local connection on ConnectBot, with the script name as login command. This works great.

However, I'm too lazy. I just want to know if there is some way to apply this script, as soon as I connect to my home WiFi automatically. Maybe something to execute a script on every network change, as I could check from same script if I'm connected to my home WiFi using dumpsys and some grep magic.
Matthew already pointed to tasker for this task (uh) – an app I already use for years. So let me add the steps to achieve this. Obviously, a pre-condition is the device is rooted – but the su command in the question tells me it is. So here we go:


Condition: State › Net › WiFi Connected
As parameter, include the SSID of your WiFi network, so it doesn't fire on alien networks :)
Task: Script › Run Shell
Here you add the name of the script to run (full path), preceded by an exclamation mark (to indicate it must be run as root)


This should do it. Not tested myself, as I have no routes to add :)