Running Scripts Using NetworkManager
For years now, I’ve had to run vpnc after connecting my school’s wireless. Truly, it never bothered me; however, at my new job, I need to run route after I connect to their wireless. For whatever reason, that got to me. I didn’t really want to run route when I wasn’t on their network (it likely wouldn’t have screwed anything up, but still…). So, I went about putting a quick bit in /etc/sysconfig/network-scripts/ifup-wireless, and it didn’t work… =\ So, after some digging, I’ve found another way using NetworkManager. Here’s what I did:
Because I hate hacking up scripts that services use, I wrote a quick script and put it in /usr/local/bin/wireless.sh:
#!/bin/bash if iwconfig|grep -c MY-WORK-ESSID then route add -net 111.111.111.111/22 gw 222.222.222.222 fi if iwconfig|grep -c msum-wireless then vpnc fi
Once that was done, I ran:
chmod +x /usr/local/bin/wireless.sh
and added the line:
/usr/local/bin/wireless.sh
to the file /etc/NetworkManager/dispatcher.d/00-netreport right before the exit.
And that’s it! It would be neat if NetworkManager added in similar functionality through the GUI, but until that day, this should work fine. Also, to note, those scripts are run as root, so be careful!
Update!
Upon upgrading from Fedora 14 to 15, the file 00-netreport was overwritten. I’ve done updates to NetworkManager, so that had nothing to do with it. In any case, if you end up upgrading your distro, there’s a chance that’ll get overwritten. In the event it does, you’ll just have to paste the line to the script again. (Another good reason to use a script!)
There may be a better way than what I’m doing above, but it works for me, so I’m not going to bother looking for another way. Hope this helps!
‹ Distributed Commands With Ruby Resolving the infamous rdesktop error: failed to open keymap en-us ›
Better option than altering the ’00-netreport’ script would be saving your script as ‘/etc/NetworkManager/dispatcher.d/01-mywireless’ istead of ‘/usr/local/bin/wireless.sh’. Then you also can tell which interface is being used and is it being opened or closed – scripts in ‘dispatcher.d’ directory are run by NetworkManager with two arguments. ‘man NetworkManager’ is very useful command
Very useful indeed! Honestly, the above was a very quick and, admittedly, hacky solution to a minor issue I encountered.
Of course, I do appreciate the right way of doing things. Thanks for the correction, loziniak!