Linux: SSH login without typing a password (ssh-copy-id, expect)
Key-based login with ssh-copy-id, or an expect script to automate a password.
To log in over SSH without typing a password, key-based authentication is the right approach. If you still need to automate a password login, expect can do it.
Key-based authentication
Once the key pair is generated (ssh-keygen), copy the public key to the server:
ssh-copy-id user@hostname
expect script (password)
#!/usr/bin/expect -f
spawn ssh user@hostname
expect "password:"
sleep 1
send "<PASSWORD>\r"
command1
command2
commandN
Notes
- A clear-text password in a script is a risk: prefer SSH keys, and restrict file permissions if you have no choice.
- The expected text (
password:) must match the prompt the server actually displays.