Skip to content

Instantly share code, notes, and snippets.

@zhaoyao
Last active December 30, 2015 09:29
Show Gist options
  • Save zhaoyao/7809610 to your computer and use it in GitHub Desktop.
Save zhaoyao/7809610 to your computer and use it in GitHub Desktop.
integrate sshpass and keychain
U=$(echo "$1" | cut -d@ -f1 | cut -d: -f1)
HOSTNAME=$(echo "$1" | cut -d@ -f2 | cut -d: -f1)
PORT=$(echo "$1" | cut -d: -f2)
if [[ $U = $1 ]]; then #no user specified
U=$(USER)
fi
if [[ $PORT = $1 ]]; then #no port specified
PORT="22"
fi
PASSWD=$(security find-generic-password -l $HOSTNAME -g 2>&1 1>/dev/null | cut -d'"' -f2)
if [[ ! `echo $PASSWD | grep 'The specified item could not be found in the keychain.'` = '' ]]; then
echo "Could not found password for host $HOSTNAME in the keychain"
echo -n "Password:"
read -s password
security add-generic-password -a $U -l $HOSTNAME -s ssh -w $password
PASSWD=$password
echo "Password for $HOSTNAME succesfully added to keychain."
fi
sshpass -p$PASSWD ssh $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment