So I need to login as a lot of different users to a server using ssh and I'm tired of copying the password from the files, which contain the passwords for the users, each time. I can't change it to not use passwords, it's not mine, it's for a game.

I've tried using :

$ cat /path/to/myfile | ssh user@localhost

I get a message saying pseudo-terminal will not be allocated because stdin is not a terminal.

I'm not really sure how it should work.

  • I wouldn't like to use sshpass either, I should have mentioned that. Is there a way to make it think it was issued by keyboard interactive user?
    – daniel
    Commented Aug 16, 2015 at 11:25
  • 2
    I don't think openssh supports doing that (it's trying to read from the terminal, not from stdin I believe). Perhaps you could make it work trying to write to the correct terminal device, but probably you'd be better off using expect or something like that if you don't want to use sshpass which is the tool created for doing the task you described Commented Aug 16, 2015 at 12:29
  • 1
    SSH pipes stdin to the remote server, echo hi | ssh host 'cat' so at minimum you'll need to experiment with the -f flag to SSH but really expect or sshpass or hacking away at the SSH source code are the viable options here.
    – thrig
    Commented Aug 16, 2015 at 15:23

1 Answer 1


SSH keys

First of all, the best solution for you would be to create new ssh keypair and use this key to login to all your servers (or some, based on preferences). If you don't know how, you can find it many times here on stackexchange, but shortcut:

ssh-keygen; ssh-copy-id your-host

Basically you should set passphrase for your key, so you will log in only once with the one password.

SSH pass

But if you really insist on using your passwords from files, you can do it quite simply as described in comments:

sshpass -f /path/to/myfile ssh user@localhost

But note that this is not really preferred method of using ssh with passwords stored in plaintext.


You can do the same thing using expect script like this:

spawn  ssh [email protected]
expect "password:"
send "your_password\n";
  • For connecting to server with pubkey you don't need to change anything on server except storing the key there. But anyway, if it is "regulation", it is pretty stupid. I guess I read too fast. Next time you add something to your question, please edit it, not mention explicit requests in comments. Sure you can use expect if it is way for you. I edited the answer.
    – Jakuje
    Commented Aug 16, 2015 at 16:31
  • @daniel your answer is to use spawn/expect Commented Oct 20, 2015 at 1:04
  • 9
    @daniel, you were rude to Jakuje and that is a problem for the whole community. This has a very different culture than a traditional forum. You might decide that you don't like their answer, but by being rude, you discourage others from giving better answers. And then people like me come here and don't have an answer. Then if I ask the question again, it will get marked as a duplicate and no one will answer that one either. So effectively, you stop all Google searchers from getting a solution (since SE/SO is always top hits) with one snark comment. This is why we say, "be nice." Commented Oct 12, 2018 at 20:25
  • 1
    @BrunoBronosky, Rudeness is not a problem for community, and it never was. The problem is that a lot of low qualified people had got in. People, who show very little attention to details using the platform for self-promotion only. This approach is wasting a lot of time of professionals who are looking for the real answers. Commented Nov 15, 2018 at 6:32

Not the answer you're looking for? Browse other questions tagged .