有没有简单的方法可以获取在 .ssh/authorized_keys || .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 命令?
答案1
下面是另一个使用普通 bash 而不使用临时文件的技巧:
while read l; do
[[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f /dev/stdin <<<$l;
done < .ssh/authorized_keys
您可以轻松地使其成为您的一个功能.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}"
}
并使用以下命令调用它:
$ fingerprints .ssh/authorized_keys
答案2
基于/dev/标准输入来自的诡计ℝaphink 的回答和man xargs → 示例:
egrep '^[^#]' ~/.ssh/authorized_keys | xargs -n1 -I% bash -c 'ssh-keygen -l -f /dev/stdin <<<"%"'
答案3
这是一种显示给定文件的所有密钥指纹的便携方法,已在 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 [email protected] (RSA)
bash $
答案4
如果有人需要在 Windows / PowerShell 中执行此操作:
gc authorized_keys | foreach {$_ |ssh-keygen -l -f -}
或者使用没有别名的完整版本来查找您的用户配置文件目录:
(Get-Content ((Get-Content env:/userprofile)+"/.ssh/authorized_keys")) | foreach {$_ |ssh-keygen -l -f -}