Net::SSH & passing session

K

kilim

Hello,

I can make a connection using the examples provided in the
documentation.

But if I try to make it more 'clean', as shown bellow, I get the
session returned, but I can't seem to do anything with it.

Can you help me with this ?

Thanks !

This is the code:


require 'net/ssh'

class MySSH
def initialize(host, port, user, password)
@host = host
@port = port
@user = user
@password = password
end

def myConnection
begin
session = Net::SSH::Session.new(@host, @port, @user, @password )
rescue SocketError
raise(IOError, 'Could not connect to host')
rescue Net::SSH::AuthenticationFailed
raise(IOError, 'Authentication Failed')
end
return session
end
end # of class

a = MySSH.new( "localhost", 22, "aaa", "bbb" )
puts "from inspect #{a.inspect}"

# here you see that session is being returned
puts "the value of a.myConnection is: #{a.myConnection}"

# just to double check
if a.myConnection.open?
puts "yep, connection is OPEN"
end

# Now this stuff below don't get 'seen' at all
# and that is the real problem
# How do I use the session once I pass it ?
# -----------------------------------------
a.myConnection.open_channel do |channel|
puts "open now, closing"
channel.close
end
a.myConnection.loop
 
J

Jamis Buck

Hello,

I can make a connection using the examples provided in the
documentation.

But if I try to make it more 'clean', as shown bellow, I get the
session returned, but I can't seem to do anything with it.

Can you help me with this ?

Thanks !

This is the code:


require 'net/ssh'

class MySSH
def initialize(host, port, user, password)
@host = host
@port = port
@user = user
@password = password
end

def myConnection
begin
session = Net::SSH::Session.new(@host, @port, @user, @password )
rescue SocketError
raise(IOError, 'Could not connect to host')
rescue Net::SSH::AuthenticationFailed
raise(IOError, 'Authentication Failed')
end
return session
end
end # of class

a = MySSH.new( "localhost", 22, "aaa", "bbb" )
puts "from inspect #{a.inspect}"

# here you see that session is being returned
puts "the value of a.myConnection is: #{a.myConnection}"

# just to double check
if a.myConnection.open?
puts "yep, connection is OPEN"
end

# Now this stuff below don't get 'seen' at all
# and that is the real problem
# How do I use the session once I pass it ?
# -----------------------------------------
a.myConnection.open_channel do |channel|
puts "open now, closing"
channel.close
end
a.myConnection.loop

The problem is that your #myConnection method isn't caching the
session, so every time you call it, you're opening a new session.
This means that the channel you try to open gets discarded, and the
loop applies to a completely different connection. Try this:

def myConnection
begin
@session ||= Net::SSH::Session.new(@host, @port, @user,
@password )
rescue SocketError
raise(IOError, 'Could not connect to host')
rescue Net::SSH::AuthenticationFailed
raise(IOError, 'Authentication Failed')
end
end


- Jamis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top