如何获取.ssh / authorized_keys(2)文件的所有指纹

是否有一个简单的方法来获取在.ssh / authorized_keys ||中input的所有指纹列表 .ssh / authorized_keys2文件?

ssh-keygen -l -f .ssh/authorized_keys 

将只返回第一行/入口/公钥的指纹

awk破解:

 awk 'BEGIN { while (getline < ".ssh/authorized_keys") { if ($1!~"ssh-(r|d)sa") {continue} print "Fingerprint for "$3 system("echo " "\""$0"\"> /tmp/authorizedPublicKey.scan; \ ssh-keygen -l -f /tmp/authorizedPublicKey.scan; \ rm /tmp/authorizedPublicKey.scan" ) } }' 

但有没有更简单的方法或SSH命令我没有find?

    这是另一个黑客使用简单的bash没有临时文件:

     while read l; do [[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f /dev/stdin <<<$l; done < .ssh/authorized_keys 

    你可以在你的.bashrc轻松地创build一个函数:

     function fingerprints() { local file="${1:-$HOME/.ssh/authorized_keys}" while read l; do [[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f /dev/stdin <<<$l done < "${file}" } 

    并用以下方式调用它:

     $ fingerprints .ssh/authorized_keys 

    这是一个便携的方式来显示给定文件的所有关键指纹,在Mac和Linux上testing:

     #!/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 [email protected] (RSA) bash $ 

    基于来自ℝaphink的答案和man xargs/ dev / stdin技巧的一行代码 例子 :

     cat ~/.ssh/authorized_keys | xargs -n1 -I% bash -c 'ssh-keygen -l -f /dev/stdin <<<"%"'