expect makes it easy to log in to SSH automatically. Ubuntu doesn't include expect by default, so install it first.
sudo apt-get install expect

Here's the script:

1
2
3
4
5
6
7
#! /usr/bin/expect
# auto ssh login with expect
set timeout 600
spawn ssh -p 65422 user@host
expect “*passoword:"
send “your_pwd\r"
interact

#! /usr/bin/expect
The path where expect is installed. This line says the script should run with expect, which works similarly to bash

set timeout 600
Sets the timeout, in seconds

spawn ssh -p 65422 user@host
spawn only works inside expect. If expect isn't installed, or if you're running it in bash, you'll get a "not found" error

expect "*passoword:"
This is also an expect command. It checks whether the last output contained the string password. If so, it returns immediately. Otherwise, it waits until the timeout

send "your_pwd\r"
Interacts with the prompt, equivalent to typing the password. Check the \r if something goes wrong

interact
Keeps the session interactive so it stays open in the terminal.

If you run sh script.sh directly, you'll get a file-not-found error. Make the script executable first: chmod +x script.sh, then run ./script.sh.