Advertisement
afragen

mount at home

Jan 21st, 2012
3,445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.89 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # call with `mount.sh true` for debug messages
  3.  
  4. home_vols=( "/Volumes/NFS_mount" ) # if more than one mountpoint then order is important
  5. home_router_mac="0:23:46:9d:e:f7" # use code below to identify this data
  6. mounts=( `mount | grep /Volumes | awk {'print$3'}` )
  7.  
  8. router_ip=`netstat -rnf inet | grep default | awk {'print$2'}`
  9. router_mac=`netstat -rnf inet | grep -v link | grep -w ^$router_ip | awk {'print$2'}`
  10. #echo $router_mac #for current router's MAC address
  11.  
  12. #test for any parameter, true if debugging
  13. test -n "$1" && dbg=$1 || dbg=false #;dbg=true
  14.  
  15. function debug {
  16.   [ $dbg == true ] && echo $1
  17. }
  18.  
  19. function on_error {
  20.   echo "errno $1"
  21.   echo "rmdir $vol"
  22.   rmdir $vol
  23.   isMounted=false
  24. }
  25.  
  26. function un_mount {
  27.   for mount in ${mounts[@]}; do
  28.     for vol in ${home_vols[@]}; do
  29.       [ $mount == $vol ] && mount | grep nfs | grep $mount | awk {'print$3'} | xargs -t umount -f
  30.     done
  31.   done
  32.   exit $?
  33. }
  34.  
  35. function mount_disk {
  36.   echo "trying to mount $vol as $1"
  37.   mkdir $vol
  38.   ### fix the following line to mount your nfs volume
  39.   mount_nfs [-o options] server:/path $vol
  40.   ###
  41.   test $? != 0 && on_error $?
  42. }
  43.  
  44. # if nothing mounted need $vols not empty to try when at home
  45. [ -z $mounts ] && mounts=( "zero_mounts" )
  46.  
  47. debug "Running $0"
  48. debug "volumes: `ls /Volumes*`"
  49. debug "mounted volumes: `echo ${mounts[@]}`"
  50.  
  51. if [ "$router_mac" == "" ]; then
  52.   debug "not connected"
  53.   un_mount
  54. fi
  55. if ! [ "$router_mac" == $home_router_mac ]; then
  56.   debug "not at home"
  57.   un_mount
  58. fi
  59. if [ $router_mac == $home_router_mac ]; then
  60.   debug "at home"
  61.   for mount in ${mounts[@]}; do
  62.     for vol in ${home_vols[@]}; do
  63.       if [ ! $mount == $vol ] && [ ! -d $vol ]; then
  64.         mount_disk
  65.       fi
  66.     done
  67.   done
  68. fi
  69.  
  70. debug "isMounted: "$isMounted
  71. debug "volumes: `ls /Volumes*`"
  72. debug "mounted volumes: `mount | grep /Volumes | awk {'print$3'}`"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement