好好爱自己!

【转】How to easily add an SSH fingerprint to your known_hosts file in Linux

 

原文:https://www.techrepublic.com/article/how-to-easily-add-an-ssh-fingerprint-to-your-knownhosts-file-in-linux/

------------------

If your Linux shell scripts are getting tripped up by SSH fingerprints, Jack Wallen has an easy solution to avoid the problem.

Let’s say you’ve written a bash script that scans all of your Linux servers in your data center for uptime. Such a file could contain the following contents:

ssh $1 "uptime"

When you run your script, it may get foiled by an issue where it is stopped by a server that has yet to have its SSH key fingerprint added to the known_hosts file. When this happens, your script is rendered useless.

SSH key fingerprint

What is an SSH key fingerprint? Simple: It is the fingerprint of a key that is verified when you try to login to a remote computer using SSH. When you log into an SSH server for the first time, you’ll see something like that shown in Figure A.

Figure A

If you don’t accept the fingerprint, the connection will be immediately broken. So what happens when you’re working with a bash script that cannot accept input, in order to okay the addition of the remote SSH fingerprint?

Fortunately, the developers of SSH thought of this, and have added a command that allows you to easily add SSH fingerprints to the known_hosts file.

Adding the fingerprint

I’ll demonstrate adding the fingerprint from a remote serve to a local machine. Let’s say the remote server is at 192.168.1.162. To add that fingerprint, the command would be:

ssh-keyscan -H 192.168.1.162 >> ~/.ssh/known_hosts

The command will run and add the remote SSH fingerprint to the local machine, without your input (Figure B).

Figure B

So an addition to the bash script could look like:

ssh-keyscan $1 >> ~/.ssh/known_hosts

The above addition would take the argument from the command (say, for example, ./script 192.168.1.118) and add the fingerprint to ~/.ssh/known_hosts before it then moves to the next line–thereby avoiding the missing SSH fingerprint issue. Of course the above would only work properly if you have ssh key authentication setup. Otherwise, you’d have to enter the remote machine’s password.

The simple things

Sometimes it’s the simple things that trip up our bash scripts. If that key fingerprint issue has been causing you headaches with your scripts, you now have the means of avoiding the issue.

posted @   立志做一个好的程序员  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2020-06-11 【转】promise的实现
2020-06-11 【转】四种 post 请求格式的XMLHttpRequest 写法
2019-06-11 【转】高可用Redis(六):瑞士军刀之bitmap,HyperLoglog和GEO
2019-06-11 go语言日期时间格式化
2017-06-11 路由器与交换机

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示