简体   繁体   English

使用ssh连接时是否可以自动向终端输入值?

[英]Is it possible to automatically input value to the terminal when using an ssh connection?

I have the username, ip addresses and passwords for specific servers. 我有特定服务器的用户名,IP地址和密码。 I want to connect to these servers using sshpass with ssh connection. 我想使用带有ssh连接的sshpass连接到这些服务器。 It would be my first connection to these servers, so ssh will save their fingerprints. 这将是我与这些服务器的首次连接,因此ssh将保存其指纹。 I need to write code that would handle any prompt asking me if I want to continue connecting and for the password. 我需要编写代码来处理任何询问我是否要继续连接以及输入密码的提示。

The problem is that since its a new connection, it will prompt if I want to continue and I don't want to type "yes" for every server. 问题在于,由于它是新连接,它将提示我是否要继续,并且不想为每个服务器键入“是”。 I will be dealing with over 50 servers at one point and I don't want to manually type "yes" and then the password. 我将一次处理超过50台服务器,并且我不想手动输入“ yes”,然后再输入密码。

This is a (dummy) code of what I actually have in bash: 这是我在bash中实际拥有的(虚拟)代码:

ip="192.168.111.111"
user="user"
password="password"

sshpass -p $password $user@$ip "exit"

I get an error of Host key verification failed for this code. 我收到此代码的Host key verification failed的错误。

So then I tried: $user@$ip "exit" and I get a prompt asking me is I want to continue connecting (yes/no). 因此,我尝试了: $user@$ip "exit"并提示我是否要继续连接(是/否)。 I don't want to type "yes" each time and then the password for each server. 我不想每次都输入“ yes”,然后再输入每个服务器的密码。 Can have some code that can manually do this for you? 可以有一些可以手动为您执行此操作的代码吗?

Thanks. 谢谢。 Let me know if further explanation is required. 让我知道是否需要进一步说明。 Also, I want to avoid using the Expect tool. 另外,我想避免使用Expect工具。

You can disable host key checking by adding this option to ~/.ssh/config under the relevant hostnames 您可以通过在相关主机名下将此选项添加到〜/ .ssh / config中来禁用主机密钥检查。

Host xxx.xxx.xxx.xxx
    StrictHostKeyChecking no

You can use wildcards to match ranges of IPs (for example Host * could be used for a global setting). 您可以使用通配符来匹配IP范围(例如, Host *可以用于全局设置)。 With this option in your config, you should be able to use this command: 在配置中使用此选项,您应该可以使用以下命令:

sshpass -p"$password" ssh "$user"@"$ip"

If you prefer not to edit the config file, you can specify the option on the command line instead: 如果您不想编辑配置文件,则可以在命令行上指定选项:

sshpass -p"$password" ssh -o StrictHostKeyChecking=no "$user"@"$ip"

Note that normally, it is recommended to use SSH keys rather than passwords to authenticate in this kind of scenario, as this facilitates automatic processes that would otherwise require a prompt. 请注意,通常,在这种情况下,建议使用SSH密钥而不是密码进行身份验证,因为这有助于进行自动处理,否则将需要提示。 It also can be more secure, as it means that your password isn't stored in plaintext somewhere. 它也可以更安全,因为这意味着您的密码不会以明文形式存储在某处。

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

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