I need to connect to a remote machine with SSH password authentication because public-key authentication is not available. Tools such as sshpass or expect can provide the password automatically, but is there a way to do the same using only Bash? In particular, can the password be piped to SSH through standard input?
3 Answers
Not directly. SSH reads the password from the controlling terminal rather than standard input, so something like `echo 'secret' | ssh user@host` will not supply it. Tools such as sshpass or expect can control the terminal interaction, but they add security risks and are generally discouraged.
The better solution is to use an SSH key pair. Generate a key, install the public key on the remote account, and use ssh-agent or a keychain to cache the private-key passphrase. Then you only enter the passphrase once instead of automating a plain-text login password.
If password authentication is unavoidable, expect or sshpass can automate it, but neither is truly Bash-only. Avoid putting the password directly in command arguments or scripts, since it can leak through shell history, process listings, backups, or file permissions. For exposed SSH services, prefer key authentication, a VPN, or another restricted access method.

That makes sense for repeated access. The main limitation here is that I do not currently have permission to install a public key on the remote machine.