简体   繁体   中英

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. I want to connect to these servers using sshpass with ssh connection. It would be my first connection to these servers, so ssh will save their fingerprints. 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.

This is a (dummy) code of what I actually have in 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.

So then I tried: $user@$ip "exit" and I get a prompt asking me is I want to continue connecting (yes/no). I don't want to type "yes" each time and then the password for each server. 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.

You can disable host key checking by adding this option to ~/.ssh/config under the relevant hostnames

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). 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. It also can be more secure, as it means that your password isn't stored in plaintext somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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