Last active
February 18, 2021 10:34
-
-
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.
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
# 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