how to check if a host is in your known_host ssh

雨燕双飞 提交于 2020-12-02 06:22:11

问题


I have the following command works in my script that adds the host to the known hosts in 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?


回答1:


Try: ssh-keygen -F <hostname>

Will show the known_hosts line(s) if the hostname fingerprint is found and the command returns 0, otherwise nothing is shown and the command returns 1.




回答2:


According to ssh-keygen(1) man page

-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.



来源:https://stackoverflow.com/questions/12136998/how-to-check-if-a-host-is-in-your-known-host-ssh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!