Skip to content

Instantly share code, notes, and snippets.

@davidsommer
Last active May 12, 2020 12:57
Show Gist options
  • Save davidsommer/0abc907de678d7b57c8bc6a6fcef1578 to your computer and use it in GitHub Desktop.
Save davidsommer/0abc907de678d7b57c8bc6a6fcef1578 to your computer and use it in GitHub Desktop.
Connect Script to connect to SSH with a Password fetched from OSX Keychain
#!/bin/bash
if [ "$1" == "-h" ]; then
echo " To run this script, you need the following things:"
echo " * Install sshpass "
echo " brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb"
echo " * generate a generic password in your keychain"
echo " Usage: add-generic-password [-a account] [-s service] [-w password] [options...] [-A|-T appPath] [keychain]"
echo " -a Specify account name (required)"
echo " -c Specify item creator (optional four-character code)"
echo " -C Specify item type (optional four-character code)"
echo " -D Specify kind (default is 'application password')"
echo " -G Specify generic attribute (optional)"
echo " -j Specify comment string (optional)"
echo " -l Specify label (if omitted, service name is used as default label)"
echo " -s Specify service name (required)"
echo " -p Specify password to be added (legacy option, equivalent to -w)"
echo " -w Specify password to be added"
echo " -X Specify password data to be added as a hexadecimal string"
echo " -A Allow any application to access this item without warning (insecure, not recommended!)"
echo " -T Specify an application which may access this item (multiple -T options are allowed)"
echo " -U Update item if it already exists (if omitted, the item cannot already exist)"
echo ""
echo " By default, the application which creates an item is trusted to access its data without warning."
echo " You can remove this default access by explicitly specifying an empty app pathname: -T """
echo " If no keychain is specified, the password is added to the default keychain."
echo " Use of the -p or -w options is insecure. Specify -w as the last option to be prompted."
exit 0
fi
# fetch password from keychain
echo "retrieving password from keychain..."
PASSWORD=`security find-generic-password -s "KEYCHAIN PASSWORD NAME" -a "KEYCHAIN PASSWORD NAM" -w`
# connect
echo "connection"
sshpass -p $PASSWORD ssh -p 24822 USERNAME@IP_ADRESS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment