Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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.

share|improve this answer
    
do you mind giving me an example? –  UbuntuUser Nov 1 '09 at 3:34

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.

share|improve this answer
1  
+1 for solving the actual problem. –  grawity Nov 1 '09 at 11:55

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.