Skip to content

Instantly share code, notes, and snippets.

@gnufied
Created September 30, 2008 20:29
Show Gist options
  • Save gnufied/13937 to your computer and use it in GitHub Desktop.
Save gnufied/13937 to your computer and use it in GitHub Desktop.
require "rubygems"
require "net/ssh"
module Kernel
def with_rsh(host,username,options = {},&block)
a = RemoteShell.new(host,username,options)
a.start(&block)
end
end
class RemoteShell
attr_accessor :host,:username
attr_accessor :session,:commands,:options
def initialize host,username,options = {}
@host = host
@username = username
@options = options
@commands = []
end
def start &block
Net::SSH.start(host,username,options) do |ssh|
@session = ssh
instance_eval(&block)
end
end
def rcd new_dir,&block
old_dir = rexec("pwd")
puts "Old directory was : #{old_dir}"
rexec("cd #{new_dir}")
new_dir = rexec("pwd")
puts "New direcrory is : #{new_dir}"
instance_eval(&block)
rexec("cd #{old_dir}")
t = rexec("pwd")
puts "Back to old direcrory : #{t}"
end
def rexec cmd
foo = @session.exec!(cmd)
foo
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment