You are trying to skip a host key checking by setting StrictHostKeyChecking
to no
.
But you have to do that before the checking, i.e. before the session.connect()
.
Anyway, you should never do this, unless you do not care about security. The host key checking is there to protect you from man-in-the-middle attacks.
Instead, set up an expected host key to let JSch verify it.
For example:
-
Call
JSch.setKnownHosts
providing a path to a.ssh/known_hosts
-like file.To generate the
.ssh/known_hosts
-like file, you can use anssh-keyscan
command from OpenSSH. If you are connecting from a *nix server, you should have the command available, just runssh-keyscan example.com > known_hosts
It will have a format like:
example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0hVqZOvZ7yWgie9OHdTORJVI5fJJoH1yEGamAd5G3werH0z7e9ybtq1mGUeRkJtea7bzru0ISR0EZ9HIONoGYrDmI7S+BiwpDBUKjva4mAsvzzvsy6Ogy/apkxm6Kbcml8u4wjxaOw3NKzKqeBvR3pc+nQVA+SJUZq8D2XBRd4EDUFXeLzwqwen9G7gSLGB1hJkSuRtGRfOHbLUuCKNR8RV82i3JvlSnAwb3MwN0m3WGdlJA8J+5YAg4e6JgSKrsCObZK7W1R6iuyuH1zA+dtAHyDyYVHB4FnYZPL0hgz2PSb9c+iDEiFcT/lT4/dQ+kRW6DYn66lS8peS8zCJ9CSQ==
And reference the generated
known_hosts
file in your JSch code.If you are on Windows, you can get a Windows build of
ssh-keyscan
from Win32-OpenSSH project or Git for Windows. -
Call
JSch.getHostKeyRepository().add()
to provide the expected host key (e.g. hard-coded, as your other credentials).See Creating JSch HostKey instance from a public key in .pub format.