1

In a bash script, I want to know know how to pipe in answers like for example:

ssh -l username -i /home/ubuntu/.ssh/id host.com < yes

The ssh connection would ask if I would like to continue the connection and I want to pipe in the answer yes automatically and enter me in, but my script "< yes" doesn't seem to work. Does anyone know how to properly type and pipe the response "yes" in bash under Ubuntu?

3 Answers3

7

You usually are only asked if you want to connect when ssh is performing host key checking. Instead of trying to disable by using expect or a pipe, perhaps you could just disable it in your ssh configuration.

Add StrictHostKeyChecking no to your ~/.ssh/config, and ssh will no longer ask you if you want to connect, it will just connect.

Zoredache
  • 128,755
  • 40
  • 271
  • 413
3

Use ssh -o "StrictHostKeyChecking no" -l username -i /home/ubuntu/.ssh/id host.com to avoid the question altogether.

Teddy
  • 5,134
  • 1
  • 22
  • 27
1

First of all, you should have tried this, instead of < yes:

$ echo "yes" | ssh -l username -i /home/ubuntu/.ssh/id host.com

Secondly, not even this will work because ssh doesn't read the answer from stdin, it reads it directly from the terminal, so you'll need something like Expect:

$ expect -c 'spawn dislocate ssh -l username -i /home/ciupicri/.ssh/id_rsa test3
expect {
    "(yes/no)? " {
        send "yes\n"
        expect {
            "Warn" exit
        }
    }
}'
$ dislocate

dislocate is a command that comes with Expect (at least on Fedora). Answer with y when asked connect? [y].

You could also try using pexpect, a pure Python Expect-like module. Check out its hive.py and sshls.py examples.

Cristian Ciupitu
  • 6,226
  • 2
  • 41
  • 55