简体   繁体   English

bash脚本来验证不要求ssh公钥

[英]bash script to verify the ssh public key isn't requested

I want to ssh to an ip from my bash script and if it asks me to add the public key to the known_hosts file, I want to print a message and exit the script. 我想从我的bash脚本中将ssh转换为ip,如果它要求我将公钥添加到known_hosts文件中,我想打印一条消息并退出该脚本。 Also, I don't want to check my known_hosts file because I'm using IDM so the key won't be in known_hosts. 另外,我不想检查我的known_hosts文件,因为我正在使用IDM,因此密钥不会在known_hosts中。 I tried this as a quick test but it seems ssh takes over because it's excepting input: 我尝试将其作为快速测试,但似乎ssh接管了它,因为它排除了输入:

    #!/bin/bash

if ssh localhost | grep -q 'authenticity'; then
   echo "matched"
fi

This is the output I currently get: 这是我当前得到的输出:

[root@host ~]# sh test.sh
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:Nw91ZidjeQLA2/pEGoLpk1lRxk22arS9/xQNTU6gck8.
ECDSA key fingerprint is MD5:d7:f0:66:9c:93:2b:2d:64:34:06:ad:c4:1d:8c:c2:a2.
Are you sure you want to continue connecting (yes/no)?

What I want is if that happen is answer "no" to the above, print "ssh key problem" and exit the script. 我想要的是,如果发生这种情况,请回答“否”,打印“ ssh key问题”并退出脚本。 If that isn't possible, maybe I can answer yes to the above then have the script check known_hosts and error if it's in there? 如果那是不可能的,也许我可以对上述回答是,然后让脚本检查known_hosts,并在其中检查错误?

You can avoid ssh asking for these things with ssh -o BatchMode=yes yourhost 您可以使用ssh -o BatchMode=yes yourhost避免ssh询问这些事情ssh -o BatchMode=yes yourhost

You'll then find -- and you can already see from your output -- that grep doesn't apply for error messages (see this post about that). 然后,您会发现-并且您已经可以从输出中看到grep不适用于错误消息(有关此信息 ,请参阅此帖子 )。

If you want to check for a successful login, you can consider doing: 如果要检查登录是否成功,可以考虑执行以下操作:

if ssh -o BatchMode=yes your.host.name true
then
  echo "Successful login"
fi

If you specifically want to check for host key verification: 如果您特别想检查主机密钥验证:

if LC_ALL=C ssh -o BatchMode=yes your.host.name true 2>&1 | grep -q "Host key verification failed"
then
  echo "Invalid host key"
fi

The 2>&1 makes sure you grep error messages, and LC_ALL=C ensures you get the default English error messages regardless of the user's current language setting. 2>&1确保您输入grep错误消息,并且LC_ALL=C确保您获得默认的英语错误消息,而不管用户当前的语言设置如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM