Skip to content

Instantly share code, notes, and snippets.

@kytvi2p
Last active October 25, 2015 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kytvi2p/2cb5bd5d5809581921e7 to your computer and use it in GitHub Desktop.
Save kytvi2p/2cb5bd5d5809581921e7 to your computer and use it in GitHub Desktop.
Print fingerprints of the keys in $HOME/.ssh/authorized_keys
#!/bin/sh
# script to print fingerprints of the keys in authorized_keys
set -eu
IFS='
'
if ! which ssh-keygen > /dev/null 2>&1; then
echo "OpenSSH is needed to run this script" >&2
exit 1
fi
print_fingerprints() {
if ssh-keygen -l -E md5 -f "$keyfile" > /dev/null 2>&1; then
# New OpenSSH, print both SHA256 and MD5 fingerprints
ssh-keygen -l -E md5 -f "$1"
ssh-keygen -l -E sha256 -f "$1"
else
# Old OpenSSH, print just the MD% fingerprints
ssh-keygen -l -f "$1"
fi
}
TMPFILE=$(mktemp -d -p /dev/shm -t key.XXXXX)
trap 'rm -rf $TMPFILE;exit' 0 1 2 15
for line in $(grep -v '^\([[:space:]]*$\|#\)' $HOME/.ssh/authorized_keys \
| sort -u)
do
keyfile="$TMPFILE/$(echo $line | awk '{print $3}' | sed 's/\//∕/g')"
echo $line > $keyfile
print_fingerprints $keyfile |sed "s/$(echo $TMPFILE \
| sed 's/\([/]\)/\\\1/g')//;s/\///"
done | sort -k1,1n -k3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment