I wanted to make my laptop running Ubuntu automatically mount a Samba share whenever it connected to my wireless home network. I quickly discovered the
/etc/network/if-down.d folders which contain scripts that are run whenever a network interface is started or stopped.
I devised the following simple scripts to mount and dismount the share when needed:
#!/bin/bash if [ "$IFACE" = "<INTERFACE>" ] then if iwconfig|grep -c <SSID> then sleep 10s mount -t smbfs <SHARE> <MOUNT-FOLDER> -o username=<USERNAME>,password=<PASSWORD> fi fi
#!/bin/bash if [ "$IFACE" = "<INTERFACE>" ] then if cat /etc/mtab|grep -c <MOUNT-FOLDER> then umount -t smbfs <MOUNT-FOLDER> fi fi
In order for it to work with your local network settings, you have to replace the following values:
<INTERFACE>: Your wireless network interface. Normally
eth2or the like. Can be found by running the
<SSID>: The SSID of your wireless network.
<SHARE>: The Samba share that you want to mount, e.g.
<MOUNT-FOLDER>: The path to an existing empty folder in which to mount the share.
<PASSWORD>: The username and password to use when connecting to the share.
Note the 10 second sleep command (
sleep 10s) run when we want to mount. This is due to the fact that
mount cannot mount a network share immediately after connecting to the network. This bugged me for quite some time, but is easily solved by waiting 10 seconds before trying to mount the share.