Created
January 25, 2012 09:26
-
-
Save gouthamvel/1675616 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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