Mount different network drive for different accounts

georgel
New Contributor III

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
1 ACCEPTED SOLUTION

georgel
New Contributor III

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

View solution in original post

2 REPLIES 2

georgel
New Contributor III

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

Santosh_BR
New Contributor III

Good to know!! Sharing would help others :)