Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass variables from ruby to sh command

Tags:

sh

ruby

rake

I have a rake task that runs, quite a lot of code. At the end, I need to use sftp and ssh to do some stuff. At the moment I'm unable to automate it. Is there a way to pass to stdout?

This seems like a simple question but I can't find the answer anywhere

#some ruby code
#more ruby code
sh "sftp myuser@hots" #this opens the sftp console
sh "put file" #this doesn't get run until sftp is exited
sh "put another_file" #neither does this

#more ruby code
sh "ssh host" # opens the ssh console
sh "some_action_on_host" # this doesn't get run until ssh is exited    

I know there will be ways of doing sftp and ssh using ruby but ideally I just want to be able to pipe variables and commands into the console

like image 722
Yule Avatar asked Aug 15 '26 19:08

Yule


1 Answers

So you want to run sftp and send a series of commands to it? How about something like:

sftp = IO.popen("sftp myuser@hots", "w+")
sftp << "put file\n"
sftp << "put another file\n"
sftp.flush # make sure to include this
like image 54
Alex D Avatar answered Aug 17 '26 10:08

Alex D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!