I want to login server by SSH without inputting password, so I make use of a file. But it can't work

c52594xx@bsa0483:~$ ls login.txt

**c52594xx@bsa0483:~$ ssh bsa10881.fra1 <<login.txt
> ^C
c52594xx@bsa0483:~$ ssh bsa10881.fra1 << login.txt
> ^C
c52594xx@bsa0483:~$ ssh c52594xx@bsa10881.fra1 <<login.txt
> ^C

c52594xx@bsa0483:~$ cat /etc/issue
Ubuntu 9.10 \n \l**
  • Wrong syntax: <xyz redirects input from file named xyz; <<xyz redirects input not from any file but from a here document aka heredoc which consists of subsequent shell input until a line consisting of xyz, hence the modified > prompt ($PS2). See wikipedia or the man page. But I concur with telcoM even if you did redirect stdin it wouldn't work; passing stdin to the remote system is a vital part of ssh's functionality. – dave_thompson_085 Nov 22 '18 at 7:39

Like several other programs, SSH does not take password input from standard input but explicitly from /dev/tty, so the password input is not (easily) redirectable.

This is because SSH has always had key authentication, which is the preferred form of non-interactive authentication with SSH.

For example, you could create a SSH key pair on you bsa0483 host with ssh-keygen -t rsa and then install the corresponding public key to bsa10881 with either running ssh-copy-id bsa10881 and entering your password (one-time only) or just copying ~/.ssh/id_rsa.pub from bsa0483 to ~/.ssh/authorized_keys on bsa10881.

By default, ssh-keygen asks you to encrypt your SSH private key with a passphrase. If you do this (and nothing else), you will be asked for the passphrase each time you're using your key. But for automation purposes, you have the option of not setting a passphrase: this allows logins without inputting a password or a key passphrase, and is not less secure than embedding a password into a script.

(It may be slightly more secure, as ssh-keygen and the SSH client will take care that the private key is properly protected, and the SSH client will output a big warning or stop using the key completely if it finds the key is readable by someone else.)

But the best combination of security and convenience might come from using a SSH authentication agent, although it requires a bit more one-time set-up. You could log into the system that has your SSH key pair (e.g. bsa0483), start up the authentication agent (perhaps in a login script?) and use ssh-add to feed it your SSH key (and enter the key passphrase one time). Now, as long as the agent is running, you can make as many outgoing SSH connections from that host with the key authentication as you want, without being asked for the key passphrase.

Between trusted hosts, you even have the option of using agent connection forwarding, so if you have your authentication agent agent on host A, and have copied your public key to hosts B, C and D, you can SSH from A to B and then onward from B to C or D in any order, still without being asked for a password or passphrase.

But if you absolutely must use password authentication with SSH without inputting the password manually, install the sshpass tool: apt-get install sshpass (hopefully available in your very old Ubuntu). More details: https://stackoverflow.com/q/12202587/9197000

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.