简体   繁体   中英

Running bash commands from within ruby to ssh login to a machine

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 user@ec2.dns.name`

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM