Last active
January 2, 2017 09:29
-
-
Save maff/e34fc6f6d629d7c4e097c374945a7ab2 to your computer and use it in GitHub Desktop.
Update .ssh/config with "UseKeychain yes" (needed on macOS sierra)
This file contains 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
#!/usr/bin/env bash | |
# macOS sierra needs an UseKeychain yes SSH config entry in order to use the | |
# Keychain to store key passphrases. This scripts checks your .ssh/config for | |
# the existence of such a config entry and prepends the following entry if | |
# nothing is found: | |
# | |
# Host * | |
# UseKeychain yes | |
# fail on errors | |
set -e | |
set -u | |
TARGET=$HOME/.ssh/config | |
EXISTING=0 | |
# check if target contains UseKeychain config | |
function checkContent() | |
{ | |
grep -q "UseKeychain yes" $TARGET | |
} | |
if [ -f $TARGET ] | |
then | |
echo "$TARGET already exists..." | |
EXISTING=1 | |
# check if UseKeychain is already set and abort if so | |
if checkContent | |
then | |
(>&2 echo "ERROR: UseKeychain is already set in $TARGET, please check manually...") | |
exit 1 | |
fi | |
BACKUPTARGET=$TARGET-usekeychain-`date +"%Y-%m-%dT%H-%M-%SZ"`.bak | |
echo "Backing up $TARGET to $BACKUPTARGET..." | |
cp $TARGET $BACKUPTARGET | |
else | |
echo "Creating $TARGET" | |
touch $TARGET | |
fi | |
# prepend keychain config to SSH config file | |
if [[ $EXISTING -eq 1 ]]; then | |
echo -e "Host *\n UseKeychain yes\n\n$(cat $TARGET)" > $TARGET | |
else | |
echo -e "Host *\n UseKeychain yes\n$(cat $TARGET)" > $TARGET | |
fi | |
# check results | |
if checkContent | |
then | |
echo "Successfully updated $TARGET with UseKeychain config" | |
exit 0 | |
else | |
(>&2 echo "ERROR: Tried to update $TARGET with UseKeychain config, but something went wrong") | |
exit 2 | |
fi |
curl -sL https://git.io/vMqal | bash
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curl -s https://gist.githubusercontent.com/maff/e34fc6f6d629d7c4e097c374945a7ab2/raw/61f422905a82306b955ef7f45a9570039c439eb9/macos-ssh-use-keychain.sh | bash