这是一种可移植的方式,用于显示给定文件的所有关键指纹,并在Mac和Linux上进行了测试:
#!/bin/bash
fingerprint_keys()
{
if (( $# != 1 )); then
echo "Usage: ${FUNCNAME} <authorized keys file>" >&2
return 1
fi
local file="$1"
if [ ! -r "$file" ]; then
echo "${FUNCNAME}: File '${file}' does not exist or isn't readable." >&2
return 1
fi
# Must be declared /before/ assignment, because of bash weirdness, in
# order to get exit code in $?.
local TMPFILE
TEMPFILE=$(mktemp -q -t "$0.XXXXXXXXXX")
if (( $? != 0 )); then
echo "${FUNCNAME}: Can't create temporary file." >&2
return 1
fi
while read line; do
# Make sure lone isn't a comment or blank.
if [[ -n "$line" ]] && [ "${line###}" == "$line" ]; then
# Insert key into temporary file (ignoring noclobber).
echo "$line" >| "$TEMPFILE"
# Fingerprint time.
ssh-keygen -l -f "$TEMPFILE"
# OVerwrite the file ASAP (ignoring noclobber) to not leave keys
# sitting in temp files.
>| "$TEMPFILE"
fi
done < "$file"
rm -f "$TEMPFILE"
if (( $? != 0 )); then
echo "${FUNCNAME}: Failed to remove temporary file." >&2
return 1
fi
}
用法示例:
bash $ fingerprint_keys ~/.ssh/authorized_keys
2048 xx:xx:xx:xx:xx:xx:xx:xx:bb:xx:xx:xx:xx:xx:xx:xx x@x.local (RSA)
bash $
authorized_keys
文件中的options字段,该文件ssh-keygen
位于。我正在寻找一种可靠的方法来解析它,但是这个答案涵盖了我能想到的最好的方法。