As mentioned in another answer,
ssh-keyscan is ideal, but if for some reason you can't do that: You can disable password authentication by either setting the option
in a configuration file (e.g.
.ssh/config), or passing the option
-o PasswordAuthentication on the command line, and thus prevent ssh from asking you for a password. Needless to say, the server sees this as a failed login attempt, unless you have another authentication method configured that does let you log in.
As you've already noted, changing the value of the
StrictHostKeyChecking option from
ask, the default, to
yes if you want better security) is necessary to avoid the prompt about whether you want to add the host key to