0

I have a use case where I want to login via ssh to an ec2 instance from a ruby script. The reason why I am trying to do this is, we manage many instances and its painful to get the dns of each instance and then ssh into it with the pem file. Here is the gist of my code:

# bunch of logic to get the DNS name of the instance

`ssh -i GFFGFG.pem [email protected]`

When I do this it opens that in a seperate process, what I want is my ruby program and end and I should login to the instance ec2.dns.name via ssh. Is there a way to latch on the std in/out of the separate process? I tried using IO.popen but that too did not give the intended result. Any help is much appreciated.

4
  • Any particular reason you need to execute it via command line? Can't you use Net::SSH library? Commented Apr 14, 2014 at 20:21
  • @BroiSatse I have a use case where I have to ssh login into multiple different instances all the time, so I want to automate the process of getting the dns name for these instances which is not constant, the isntances keep changing. So I need to ssh via command line and then do bunch of different stuff there. Thank you. Commented Apr 14, 2014 at 20:42
  • 1
    I still don't see the reason why you need to use command line for that. Net::SSH library should do all you want within the ruby process and definitively will give you better control over those connections. Have a read: net-ssh.github.io/net-ssh Commented Apr 14, 2014 at 20:46
  • 1
    There is even module Net::SSH::Multi to handle multiple parallel connections, seems you might be interested in it. Commented Apr 14, 2014 at 20:50

1 Answer 1

1

It sounds to me like you're looking for Kernel.exec

Replaces the current process by running the given external command

http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-exec

This ends your script and has SSH take over. No Ruby code after the call to exec will run.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.