Skip to content

Instantly share code, notes, and snippets.

@nonoesp
Last active February 18, 2021 10:34
Show Gist options
  • Save nonoesp/6e5d185583b205b25e353ce532b74bf1 to your computer and use it in GitHub Desktop.
Save nonoesp/6e5d185583b205b25e353ce532b74bf1 to your computer and use it in GitHub Desktop.
Verify whether a given private SSH key is authorized on a series of remote servers.
# Key to test
SSH_KEY_PATH=~/.ssh/id_rsa
# Add SSH key to authentication agent
ssh-add -K ${SSH_KEY_PATH}
echo "Verifying SSH access with key at ${SSH_KEY_PATH}"
for ip in \
"[email protected]" \
"[email protected]" \
; do
# 2>&1 redirects stderr to stdout
output=$(ssh -i ${SSH_KEY_PATH} \
-t "${ip}" 'exit; bash -l' 2>&1)
[[ $(echo ${output}) =~ "denied" ]] || echo "[ OK ] ${ip}"
[[ $(echo ${output}) =~ "closed" ]] || echo "[ DENIED ] ${ip}"
done
# Remove SSH key to authentication agent
ssh-add -d ${SSH_KEY_PATH}
# Print keys in authentication agent
ssh-add -l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment