# Problems with rake and sh

**URL:** <https://www.ruby-forum.com/t/problems-with-rake-and-sh/158757>\
**Category:** Ruby\
**Created:** [January 27, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/problems-with-rake-and-sh/158757 "2009-01-27T18:27:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [January 27, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/problems-with-rake-and-sh/158757/1 "2009-01-27T18:27:15Z")

</div>

Hi,

I am using ssh to login to my Amazon EC2 server, and using a rake task  
to automate some activities on the machine. Every time I login, it is a  
new machine, so I have no host keys from the machine and SSH connections  
pass back the warning that a new key was added to my local known hosts  
file.

When I connect via the shell without using ruby, my command works great  
with no errors. Inside my rake task, using ruby, I am almost always  
having the rake task blow out with an error. How can I construct my ruby  
to shell out without choking on this ssh issue???

eg command:

sh “ssh -i /home/foo/.ssh/id\_rsa-bs-keypair  
root@#{@instance[0][:dns\_name]} ‘mkdir /mnt/data\_store && mount  
/dev/sdf /mnt/data\_store’”

This is usually resulting in something like:

ssh: connect to host [ec2-72-44-38-77.compute-1.amazonaws.com](http://ec2-72-44-38-77.compute-1.amazonaws.com) port 22:  
Connection refused  
rake aborted!

Any one know of how I can get this small snippet to work in a ruby  
task???

Thanks

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [January 28, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/problems-with-rake-and-sh/158757/2 "2009-01-28T21:04:00Z")

</div>

> Any one know of how I can get this small snippet to work in a ruby  
> task???

maybe run it within bash or sh -c or so… [?]  
-=r

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [January 28, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/problems-with-rake-and-sh/158757/3 "2009-01-28T21:07:41Z")

</div>

Roger P. wrote:

> > Any one know of how I can get this small snippet to work in a ruby  
> > task???
> 
> maybe run it within bash or sh -c or so… [?]  
> -=r

It does run fine in bash… the whole point for me is to use Ruby inside  
a rake task to do my work… and the one thing I cannot figure out is  
how to get the ssh connection to work.

If I connect once and simply exit… and then connect again, in the same  
script… with the same server instance, every further call that shells  
out works fine. It is only a problem getting the initial shell  
connection to work. I am sure there is a config change I can make to  
solve this, but so far, my only work-around has been the silly “connect  
first in a loop”…

Thanks

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [January 31, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/problems-with-rake-and-sh/158757/4 "2009-01-31T13:54:19Z")

</div>

Maybe Net::SSH could help you: [http://net-ssh.rubyforge.org/](http://net-ssh.rubyforge.org/)

Jarmo

Dave Foo wrote:

> It does run fine in bash… the whole point for me is to use Ruby inside  
> a rake task to do my work… and the one thing I cannot figure out is  
> how to get the ssh connection to work.  
> …  
> Thanks
