Mount Samba Shares Based On WiFi Networks
Posted by voice | Filed under Techie
This is probably a common occurance amongst Linux users like me, you have a share you want to mount every time your computer or laptop starts up on a WiFi network (my desktop connects via WiFi), you write a handy little script to do for you at start up only to find the script is trying to mount your Samba share on every network you connect to, needless to say this can cause security problems, espcially when you’re broadcasting passwords over a wireless network. I ended up doing some googling and found a way to modify my script so it would only try to mount the shares when the script knew it was on my home network, heres what the script looks like:
#!/bin/sh
sleep 20 #Hold here, allows for the wireless to connect.
ESSID=`/sbin/iwconfig wlan0 | grep ESSID | cut -d ‘”‘ -f 2` #Get the ESSID.
case $ESSID in
“yournetwork”)
gksudo “mount -t cifs -o credentials=/path/to/cred //ip/share /mnt/dir”
;;
*)
exit
;;
esac
Obviously you get to fill in your own details, but with this, and adding it to the start up list in Gnome’s sessions this script will automatically wait 20 seconds before checking what wifi network it is on, if its on your network then it will mount the share, if not it will just exit. There is also a handy forum post here to solve shutdown issues related to CIFS.