Last active
December 20, 2018 12:10
-
-
Save smuuf/6a342404732c7bc8934501945d1f0cf2 to your computer and use it in GitHub Desktop.
Small BASH script which iterates over all hosts defined in SSH config and tries to ssh-copy-id your SSH ID to all of them.
This file contains hidden or 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
#!/bin/bash | |
cd $(dirname $0) | |
CONF_PATH=~/.ssh/config | |
function title() { | |
echo "█ $1" | |
} | |
function info() { | |
echo " $1" | |
} | |
function err() { | |
echo "! Error: $1" | |
} | |
title "SSH ID Copy Tool Thingie, Premysl Karbula, 2018" | |
if [[ ! -f "$CONF_PATH" ]]; then | |
err "No SSH config found at "$(realpath $CONF_PATH) | |
exit 1 | |
fi | |
info "Using config at "$(realpath $CONF_PATH) | |
grep --color=never -E '^Host ' $CONF_PATH | while read LINE; do | |
HOST=$(echo "$LINE" | sed -e 's/Host\s*//g') | |
if [[ "$HOST" == "*" ]]; then | |
continue | |
fi | |
title "SSH host '$HOST' ... " | |
# Dry-run to detect if key is already present. | |
# Empty string is returned if key IS present. | |
DRY_RUN=$(ssh-copy-id -o ConnectTimeout=5 -n $HOST 2>&1) | |
NOT_PRESENT=$(echo "$DRY_RUN" | grep "Would have added") | |
HAD_TIMEOUT=$(echo "$DRY_RUN" | grep "Connection timed out") | |
if [[ ! -z "$HAD_TIMEOUT" ]]; then | |
info "Timeout." | |
continue | |
fi | |
if [[ -z "$NOT_PRESENT" ]]; then | |
info "Already there." | |
continue | |
fi | |
info "Not present. Copying ..." | |
ssh-copy-id $HOST | |
done | |
title "Done." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment