Last active
February 9, 2018 15:05
-
-
Save cocoabox/29304ab08792483b5234bdc5d5b15ce5 to your computer and use it in GitHub Desktop.
mount AFP share with keychain password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# e.g. mount_afp.sh 192.168.xx.xx files cocoa | |
# | |
SERVER="$1" | |
SHARE_NAME="$2" | |
USER_NAME="$3" | |
MOUNT_DIR="/Volumes/$SHARE_NAME" | |
if [ -z "$1" -o -z "$2" ]; then | |
echo "Usage: $0 <SERVER> <SHARE_NAME> [USER]\n" | |
exit 1 | |
fi | |
if [ -d "$MOUNT_DIR" ]; then | |
mount | grep --colour=always "$MOUNT_DIR" | |
if [ $? -ne 0 ] ; then | |
echo "ERROR: $MOUNT_DIR already exists" >&2 | |
exit 1 | |
else | |
exit 0 | |
fi | |
fi | |
mkdir "/Volumes/$2" | |
# | |
# get password from keychain, then dequote | |
# | |
PASS_QUOTED=`security find-internet-password -gs "$1" 2>&1 | sed -n 's/password: \(.*\)/\1/p'` | |
PASS=`echo "$PASS_QUOTED" | sed -e 's/^"//' -e 's/"$//'` | |
PASS_PARAM="" | |
if [ -z "$PASS" ]; then | |
echo "NOTICE: no password found for $1" >&2 | |
else | |
PASS_PARAM=":$PASS" | |
fi | |
# | |
# | |
# | |
if [ -z "$USER_NAME" ]; then | |
USER_NAME=`whoami` | |
echo "NOTICE: using local user name: $USER_NAME" >&2 | |
fi | |
echo "mounting afp://$USER_NAME@$SERVER ==> $MOUNT_DIR" >&2 | |
mount_afp "afp://${USER_NAME}${PASS_PARAM}@${SERVER}/$SHARE_NAME" "$MOUNT_DIR" | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment