Skip to content

Instantly share code, notes, and snippets.

@satouriko
Forked from kookxiang/update_ssh_keys.sh
Created July 5, 2018 03:11
Show Gist options
  • Save satouriko/02525de6715e5e3622143ec651ef83a4 to your computer and use it in GitHub Desktop.
Save satouriko/02525de6715e5e3622143ec651ef83a4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
TARGET_DIR='/home/kookxiang/.ssh'
GITHUB_USERNAME='kookxiang'
if ! [ -d ${TARGET_DIR} ]; then
mkdir ${TARGET_DIR}
chmod 0700 ${TARGET_DIR}
fi
if ! [ -f ${TARGET_DIR}/authorized_keys ]; then
touch ${TARGET_DIR}/authorized_keys
chmod 0600 ${TARGET_DIR}/authorized_keys
fi
curl -sS https://api.github.com/users/${GITHUB_USERNAME}/keys | grep -Po '"key": ".+?"' | sed -e 's/\"key\": \"//' -e 's/"$//' >> ${TARGET_DIR}/authorized_keys.new
if [ $? != 0 ]; then exit 1; fi
cat ${TARGET_DIR}/authorized_keys.new > ${TARGET_DIR}/authorized_keys
rm -rf ${TARGET_DIR}/authorized_keys.new
echo >> ${TARGET_DIR}/authorized_keys
if [ -f ${TARGET_DIR}/alternate_keys ]; then
cat ${TARGET_DIR}/alternate_keys >> ${TARGET_DIR}/authorized_keys
echo >> ${TARGET_DIR}/authorized_keys
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment