Skip to main content
Solved

Mount different network drive for different accounts

  • May 8, 2018
  • 2 replies
  • 43 views

Forum|alt.badge.img+5

I have no scripting knowledge, found this script below which works for me for mounting the same network share for every user that logs in.

But for our lab we have multiple local accounts and each connects to a different share folder. Is it possible to create a script that will look up the logged in user and mount a network folder based who the logged in user is??

#!/bin/sh

protocol="$4" # This is the protocol to connect with (afp | smb)
    echo "Protocol: $4"
serverName="$5"   # This is the address of the server, e.g. my.fileserver.com
    echo "Server: $5"
shareName="$6"    # This is the name of the share to mount
    echo "Sharename: $6"

# Mount the drive 
    mount_script=`/usr/bin/osascript  > /dev/null << EOT
    tell application "Finder" 
    activate
    mount volume "$protocol://${serverName}/${shareName}"
    end tell
EOT`

exit 0

Best answer by georgel

Never mind, I managed to make a script that works for me.
Here is what i used

#!/bin/sh

# Get the username of the currently logged in user
loggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')

# USER1
if [ "$loggedInUser" = "USER1" ]; then
protocol="$4" # This is the protocol to connect with (afp | smb)
    echo "Protocol: smb"
serverName="$5"   # This is the address of the server, e.g. my.fileserver.com
    echo "Server: SERVER"
shareName="$6"    # This is the name of the share to mount
    echo "Sharename: SHARENAME_1"

# Mount the drive 
    mount_script=`/usr/bin/osascript  > /dev/null << EOT
    tell application "Finder" 
    activate
    mount volume "smb://SERVER/SHARENAME_1"
    end tell
EOT`
fi

# USER2
if [ "$loggedInUser" = "USER2" ]; then
protocol="$4" # This is the protocol to connect with (afp | smb)
    echo "Protocol: smb"
serverName="$5"   # This is the address of the server, e.g. my.fileserver.com
    echo "Server: SERVER_2"
shareName="$6"    # This is the name of the share to mount
    echo "Sharename: SHARENAME_2"

# Mount the drive 
    mount_script=`/usr/bin/osascript  > /dev/null << EOT
    tell application "Finder" 
    activate
    mount volume "smb://SERVER_2/SHARENAME_2"
    end tell
EOT`
fi

2 replies

Forum|alt.badge.img+5
  • Author
  • New Contributor
  • Answer
  • May 9, 2018

Never mind, I managed to make a script that works for me.
Here is what i used

#!/bin/sh

# Get the username of the currently logged in user
loggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')

# USER1
if [ "$loggedInUser" = "USER1" ]; then
protocol="$4" # This is the protocol to connect with (afp | smb)
    echo "Protocol: smb"
serverName="$5"   # This is the address of the server, e.g. my.fileserver.com
    echo "Server: SERVER"
shareName="$6"    # This is the name of the share to mount
    echo "Sharename: SHARENAME_1"

# Mount the drive 
    mount_script=`/usr/bin/osascript  > /dev/null << EOT
    tell application "Finder" 
    activate
    mount volume "smb://SERVER/SHARENAME_1"
    end tell
EOT`
fi

# USER2
if [ "$loggedInUser" = "USER2" ]; then
protocol="$4" # This is the protocol to connect with (afp | smb)
    echo "Protocol: smb"
serverName="$5"   # This is the address of the server, e.g. my.fileserver.com
    echo "Server: SERVER_2"
shareName="$6"    # This is the name of the share to mount
    echo "Sharename: SHARENAME_2"

# Mount the drive 
    mount_script=`/usr/bin/osascript  > /dev/null << EOT
    tell application "Finder" 
    activate
    mount volume "smb://SERVER_2/SHARENAME_2"
    end tell
EOT`
fi

Forum|alt.badge.img+3
  • New Contributor
  • May 9, 2018

Good to know!! Sharing would help others :)