How to use a password from the OSX keychain in your shell scripts
#!/bin/sh
# Man Page
# http://ss64.com/osx/security-password-mgt.html
# Helpful links
# http://hints.macworld.com/article.php?story=20130722033452283
# http://blog.macromates.com/2006/keychain-access-from-shell/
get_keychain_pw () {
keychain_pw_account="wpe-sftp"
# you can get the current user's password by settings the account to $USER
# example
# keychain_pw_account=$USER
security 2>&1 >/dev/null find-generic-password -ga $keychain_pw_account \
| ruby -e 'print $1 if STDIN.gets =~ /^password: "(.*)"$/'
}
# You can assign it to a variable like so:
SFTP_PASS=`get_keychain_pw`
echo $SFTP_PASS
# or use it directly like so:
echo get_keychain_pw
# ideally you'll want to use this with something that doesn't echo the password to the screen, which defeats the purpose of putting a password into your keychain.
#For accessing things other than your main account, this command doesn't seem to work with 10.11.5 right now
get_keychain_pw () {
security find-generic-password -wl naslord
}
NAS_PASS=`get_keychain_pw`
echo $NAS_PASS