Skip to content

Instantly share code, notes, and snippets.

@gouthamvel
Created January 25, 2012 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gouthamvel/1675616 to your computer and use it in GitHub Desktop.
Save gouthamvel/1675616 to your computer and use it in GitHub Desktop.
#ruby -v # 1.9.2
#gem list|grep em-ssh # em-ssh(0.1.0)
require 'em-ssh'
# runs the command on server and gives the callback call
def do_ssh(cmd)
do_ssh_var = 'ssh'
EM::Ssh.start('localhost','goutham') do |ssh|
puts 'ssh start'
ssh.exec!(cmd) do |channel, stream, data|
puts 'callback'
yield data
puts do_ssh_var
end
end
end
# modifies command and runs it on server.
# After commands are run on server rest of code is exec in callback block
def prepare_server(cmd)
cmd += ' /home'
callback_var = 'callback var'
do_ssh(cmd) do |data|
puts "\n#{data}"
puts "pre callback_val", callback_val
yield()
puts "post callback_val", callback_val
end
end
begin
EM.run do
puts 'run'
main_var = 'main'
prepare_server('ls -l') do
puts 'main context', main_var # callback after the server is prepared
end
EM.add_timer(3) { EM.stop}
end
rescue => e
puts e.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment