Is there a way in a Ruby script to sftp to a server and run some commands.
Here is my script
Net::SFTP.start(IP_ADDRESS, 'root', :keys => ["/Users/user/.ssh/id_rsa"]) do |sftp|
sftp.mkdir! "/srv/new_dir"
sftp.dir.foreach("/srv/crm/current") do |entry|
puts entry.longname
sftp.file.open("/srv/new_dir", "w") do |f|
f.puts entry
end
end
end
Basically I am trying to sftp to the server and copy a folder into a new folder and I would like to restart apache. I tried this:
FileUtils::sudo "/etc/init.d/apache2 reload"
but that doesn’t work. Any ideas what I am doing wrong, or whether this is even possible?
Since you aren’t really doing any file transfers, I don’t think you need to be using SFTP. You could simply rewrite this script using
Net::SSH. Then you have normal access to a shell and your Apache command is simply another step in this process.