bash 如何检查主机是否在您的 known_host ssh 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12136998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check if a host is in your known_host ssh
提问by barp
I have the following command works in my script that adds the host to the known hosts in ssh.
我的脚本中有以下命令可以将主机添加到 ssh 中的已知主机。
VAR2=$(expect -c '
spawn ssh -o StrictHostKeyChecking=no '"$REMOTE_HOST_USER@$REMOTE_HOST_IP"'
expect "*?assword:*"
send "'"$REMOTE_HOST_PASSWD"'\r"
expect {
"Permission denied, please try again." {
exit '"$WRONG_PASSWORD"'
}
}
')
Works fine, but I need to control before the command if the host is already in known_hosts and not execute command if it is already in known_hosts. How can i check if an host is in known_hosts?
工作正常,但我需要在命令之前控制主机是否已经在 known_hosts 中,如果它已经在 known_hosts 中则不执行命令。如何检查主机是否在 known_hosts 中?
回答by complex857
Try: ssh-keygen -F <hostname>
尝试: ssh-keygen -F <hostname>
Will show the known_hostsline(s) if the hostname fingerprint is found and the command returns 0, otherwise nothing is shown and the command returns 1.
known_hosts如果找到主机名指纹并且命令返回0,将显示行,否则不显示任何内容并且命令返回1。
回答by hostmaster
According to ssh-keygen(1) man page
根据 ssh-keygen(1) 手册页
-F hostname Search for the specified hostname in a known_hosts file, listing any occurrences found. This option is useful to find hashed host names or addresses and may also be used in conjunction with the -H option to print found keys in a hashed format.
-F 主机名 在 known_hosts 文件中搜索指定的主机名,列出找到的所有事件。此选项可用于查找散列主机名或地址,也可与 -H 选项结合使用以散列格式打印找到的键。

