Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'How to pull a password from Keychain to use in a script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
How to pull a password from Keychain to use in a script
Authored by: szedula on Jul 24, '13 07:26:08PM

In csh or tcsh, this could be done as:

set sys="192.168.1.2"
set pw = `security find-generic-password -ga $USER |& grep password | awk '{print $2}' | sed -e s/\"//g`
if (! -d $HOME/Data) mkdir $HOME/Data
mount -t afp afp://${USER}:$pw@$sys/Data $HOME/Data

Things I noted while playing with this:
+ The mounted volume also shows up on the Desktop, but I guess that is because my Finder preferences include "Connected server" under "Show these items on the desktop", "df" reports the mount is in my home directory not in "/Volumes" as desired
+ "$HOME/Data" becomes an alias that eventually goes back to being a folder after unmounting.
+ Avoids something like "/Volumes/Data" and "/Volumes/Data-1" if I mount a shared volume with the same name as a volume on my system. But doesn't solve the confusion in Applescripts. Still can't tell the difference between the two mounted volumes with the same name. Both are treated as something like: of disk "Data"
+ Learned the "|&" in csh or tcsh combines "stdout" and "stderr"

Tested with: Mac OS X 10.6.8



[ Reply to This | # ]
How to pull a password from Keychain to use in a script
Authored by: sr105 on Jul 25, '13 04:48:01PM

Your command could be simplified to:
security ... | sed -En '/^password: / s,^password: "(.*)"$,\1,p'

# No if() required:
mkdir -p $HOME/Data

Update: or as martin suggested, use -w with security.

Edited on Jul 25, '13 04:51:37PM by sr105



[ Reply to This | # ]