ruby - Using Net::SSH in an `initialize` block does not work -


i want connect remote server , run commands there. writing following ruby script , works fine.

@hostname = "server_name" @username = "user" @password = "pass" @cmd = "ls -alt"  begin   @ssh = net::ssh.start(@hostname, @username, :password => @password)   puts "#{@ssh}"   res = @ssh.exec!(@cmd)   @ssh.close   puts res rescue   puts "unable connect #{@hostname} using #{@username}/#{@password}" end 

but when try put same code in initialize block not working. below code that:

def initialize(hostname, user, password)   @hostname = "#{hostname}"   @username = "#{user}"   @password = "#{password}"    begin     puts "entered begin"     @ssh = net::ssh.start(@hostname, @username, :password => @password)     puts "#{@ssh}"     res = @ssh.exec!(@cmd)     puts "#{res}"     # @ssh.close     puts res   rescue => e     puts e     puts "#{@ssh} unable connect #{@hostname} using #{@username}/#{@password}"   end end 

printing @ssh gives different results in first , second chunk of code.

can 1 me in figuring out what's going wrong?

problem did not initialize @cmd in initialize block. why not working expected. fixed as recommended @ptierno. below working code:

def initialize(hostname, user, password)   @hostname = "#{hostname}"   @username = "#{user}"   @password = "#{password}"   @cmd = "ls -alt"    begin     puts "entered begin"     @ssh = net::ssh.start(@hostname, @username, :password => @password)     puts "#{@ssh}"     res = @ssh.exec!(@cmd)     puts "#{res}"     # @ssh.close     puts res   rescue => e     puts e     puts "#{@ssh} unable connect #{@hostname} using #{@username}/#{@password}"   end end 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -