Skip to content

Instantly share code, notes, and snippets.

@orbekk
Last active September 10, 2015 18:54
Show Gist options
  • Save orbekk/deaab56ed8221e4e4a07 to your computer and use it in GitHub Desktop.
Save orbekk/deaab56ed8221e4e4a07 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This script syncs authorized keys (found in the $authorized_keys_file below)
# to a list of remote hosts. It does not touch existing keys unless overwrite
# is set to true, but creates a special section containing the keys.
declare -r begin_marker="### BEGIN MANAGED_BY_KJ_SYNC_AUTHORIZED_KEYS.SH ###"
declare -r end_marker="### END MANAGED_BY_KJ_SYNC_AUTHORIZED_KEYS.SH ###"
declare -r overwrite=true
declare -r tmpdir=$(mktemp -d /tmp/kj_sync_authorized_keys.XXXXX)
targets=(
tesuji.6.orbekk.com
login.6.orbekk.com
minecraft.6.orbekk.com
backup.6.orbekk.com
login.pvv.ntnu.no
aji.orbekk.com
moyo.orbekk.com
)
authorized_keys_file=$HOME/dotfiles/authorized_keys
add_keys_to_file() {
filename="$1"
awk \
"/$begin_marker/"' { exit 0 } { print }' \
${filename} > ${filename}.header
awk \
"/$end_marker/"' { should_output=1 } should_output { print }' \
${filename} > ${filename}.footer
cat "${filename}.header" > ${filename}
echo "${begin_marker}" >> ${filename}
echo "# WARNING: ANY CHANGES WILL BE OVERWRITTEN" >> ${filename}
cat "$authorized_keys_file" >> ${filename}
echo "${end_marker}" >> ${filename}
cat "${filename}.footer" >> ${filename}
}
for target in ${targets[@]}; do
echo "syncing $target"
tmp="${tmpdir}/${target}"
touch ${tmp}
if [[ $overwrite != true ]]; then
ssh ${target} 'cat .ssh/authorized_keys || echo -n' > ${tmp}
fi
add_keys_to_file "${tmp}"
ssh ${target} 'mkdir -p .ssh'
cat "${tmp}" | ssh ${target} 'cat > .ssh/authorized_keys.tmp && mv .ssh/authorized_keys{.tmp,}'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment