Ubuntu Networkmanager add: auto mount, set default printer and tunneling
From wiki.breedveld.net
This is a add for Network manager
if switching to another network, this script:
- auto mount/unmount sshfs or nfs shares
- automatically starts a tunnel (gstm)
- sets your prefered default printer
What you should do first:
- install sshfs
- publish you private key for sshfs
If you create an automatic tunnel with gstm, this script will find the config and stop/start gstm
The config file:
# /usr/local/bin/nm-applet-sript.cfg # # the port is only for sshfs, default this should be 22 # the domain should be the resolv.conf domain-name after switching the network # # domain printer ssh/nfs server port shares breedveld.net HP_Laserjet_5 nfs phung 0 data download capgemini.nl mono sshfs breedveld.net 21 data download agi.rws.nl DID_OCE_Color sshfs breedveld.net 8080 data download
The switch script:
#!/bin/bash
#
# Add for Networkmanager, for mounting, printers and tunnels
# 25-06-2009 RJB
# /usr/local/bin/nm-applet.script.sh
# link this as /etc/NetworkManager/dispatcher.d/nm-applet.script.sh
#
export CONFIG="/usr/local/bin/nm-applet-sript.cfg"
export LOGFILE="/var/log/nm-applet.log"
export IF=${1}
export COMMAND=${2}
export DISPLAY=:0.0
export SU_USER=$(who |grep tty7|awk '{print $1}')
if [ ! -z "$(ls /home/${SU_USER}/.gSTM/*gstm 2>/dev/null)" ]
then
export TUNNEL=gstm
fi
(
echo "Started: ${0} ${*}"
export NAME=$(grep domain /etc/resolv.conf|head -1|awk '{print $NF}')
if [ ! -z "${NAME}" ]
then
export PRINTER=$(grep "^${NAME}" ${CONFIG}|awk '{print $2}')
export MTYPE=$(grep "^${NAME}" ${CONFIG}|awk '{print $3}')
export SERVER=$(grep "^${NAME}" ${CONFIG}|awk '{print $4}')
export PORT=$(grep "^${NAME}" ${CONFIG}|awk '{print $5}')
export SHARES=$(grep "^${NAME}" ${CONFIG}|awk '{print $6" "$7" "$8" "$9}')
fi
function tunnel_down ()
{
if [ ! -z "${TUNNEL}" ]
then
echo "Killing ${TUNNEL} tunnel"
/usr/bin/pkill -9 ${TUNNEL}
fi
}
function tunnel_up ()
{
if [ ! -z "${TUNNEL}" ]
then
# su - ${SU_USER} -c "xhost +"
echo "Starting ${TUNNEL} tunnel"
su - ${SU_USER} -c "export DISPLAY=${DISPLAY}; nohup ${TUNNEL} &"
fi
}
function config_printer ()
{
if [ ! -z "${PRINTER}" ]
then
echo "Switch default printer to ${PRINTER}"
lpadmin -d ${PRINTER}
fi
}
function mount_down ()
{
ps -ef|grep "mount"|egrep -v "grep|mount_"|awk '{print $2" "$NF}'|
while read PID PRC
do
echo "Killing ${PID} ${PRC} ..."
sudo kill -9 ${PID}
done
mount|egrep "nfs|ssh|cifs"|awk '{print $3}'|
while read SHARE
do
echo "unmounting ${SHARE} ..."
sudo umount ${SHARE}
if mount|egrep "nfs|ssh|cifs" |grep "${SHARE}" >/dev/null 2>&1
then
echo "forced unmounting ${SHARE} ..."
sudo umount -f ${SHARE}
sudo umount -f ${SHARE}
fi
done
}
function mount_up ()
{
for SHARE in ${SHARES}
do
echo "${MTYPE}-mount ${SERVER}:${PORT}/${SHARE} to /home/roland/${SHARE}"
if [ ! -d "/home/${SU_USER}/${SHARE}" ]
then
su - ${SU_USER} -c mkdir /home/${SU_USER}/${SHARE}
fi
if [ "${MTYPE}" = "sshfs" ]
then
su - ${SU_USER} -c "nohup sshfs ${SU_USER}@${SERVER}:/${SHARE} /home/${SU_USER}/${SHARE} -p ${PORT} &"
fi
if [ "${MTYPE}" = "nfs" ]
then
mount -t nfs ${SERVER}:/${SHARE} /home/${SU_USER}/${SHARE} -orw,suid
fi
done
}
case ${COMMAND} in
"up")
tunnel_down
if [ "${IF}" = "eth0" -a ! -z "$(ifconfig wlan0|grep 255)" ]
then
mount_down
fi
tunnel_up
mount_up
config_printer
;;
"down")
tunnel_down
mount_down
if [ "${IF}" = "eth0" -a ! -z "$(ifconfig wlan0|grep 255)" ]
then
tunnel_up
mount_up
config_printer
fi
;;
esac
) >> ${LOGFILE} 2>&1
