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

chi*_*.de 46 ssh bash awk ssh-keygen ssh-keys

有没有一种简单的方法来获取 .ssh/authorized_keys 中输入的所有指纹的列表?.ssh/authorized_keys2 文件?

ssh-keygen -l -f .ssh/authorized_keys 
Run Code Online (Sandbox Code Playgroud)

只会返回第一行/条目/公钥的指纹

用 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"
        )
    }
}'
Run Code Online (Sandbox Code Playgroud)

但是有没有更简单的方法或我没有找到的 ssh 命令?

rap*_*ink 54

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

while read l; do
  [[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f /dev/stdin <<<$l;
done < .ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)

您可以轻松地使其成为您的功能.bashrc

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}"
}
Run Code Online (Sandbox Code Playgroud)

并调用它:

$ fingerprints .ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)

  • 如果键以选项为前缀,这将不起作用。 (2认同)

aka*_*vel 10

基于?aphink 的答案man xargs → 示例中/dev/stdin技巧的单行

egrep '^[^#]' ~/.ssh/authorized_keys | xargs -n1 -I% bash -c 'ssh-keygen -l -f /dev/stdin <<<"%"'
Run Code Online (Sandbox Code Playgroud)


Wil*_*ill 9

这是一种显示给定文件的所有关键指纹的便携式方法,在 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
}
Run Code Online (Sandbox Code Playgroud)

示例用法:

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 $ 
Run Code Online (Sandbox Code Playgroud)

  • 这使它更安全,对吗?欢迎您进行编辑,但为什么要投反对票?我没有提出它是比你更好的解决方案......我觉得一个安全的临时文件更好,并且脚本编写需要更多的安全性。此外,上面的版本是 noclobber-safe。 (3认同)

小智 5

ssh-keygen -l -f - <authorized_keys
Run Code Online (Sandbox Code Playgroud)

为您生成一个不错的列表:

# ssh-keygen -l -f - <authorized_keys
2048 SHA256:GzZ7.................................RqTEag foo (RSA)
2048 SHA256:/y0.......................................4 bar (RSA)
2048 SHA256:p.........................................k bleech (RSA)
Run Code Online (Sandbox Code Playgroud)