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 Answers 3

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.

1
  • 1
    +1 for solving the actual problem. Nov 1, 2009 at 11:55
3

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

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.

1
  • do you mind giving me an example?
    – UbuntuUser
    Nov 1, 2009 at 3:34

You must log in to answer this question.