F
Fenster Blick
Hi, I am a beginner to Ruby, having come over from the Java side of
things. I am writing a simple script to familiarize myself with the
Net-SSH and Net-SFTP libraries. I want to split my script into several
different functions to improve readability and organization.
However, I'm having difficulty doing this. When I create an SSH
connection, the connection automatically dies after I exit the function.
How can I keep this connection alive? I tried storing the connection as
a global variable but it did not work - the connection still closed.
Here is a portion of my script. When I run it, I call "testAll". It
fails on the first line of sftpTest1A:
----------------------------------------
$session = nil
def sftpTest1
puts "Testing sftp..."
$session= Net::SSH.start( '127.0.0.1', :username=>'user',
assword=>'password' )
sftp = $session.sftp.connect
handle = sftp.opendir( "." )
items = sftp.readdir(handle)
puts items
sftp.close_handle( handle )
sftp.close
#At this point, session should still be open, correct?
end
def sftpTest1A
shell = $session.shell.open #FAILS at this line!
shell.pwd
print shell.stdout while shell.stdout?
$stderr.puts "-- stderr: --"
$stderr.print shell.stderr while shell.stderr?
$session.close
end
def testAll
sftpTest1
sftpTest1A
end
things. I am writing a simple script to familiarize myself with the
Net-SSH and Net-SFTP libraries. I want to split my script into several
different functions to improve readability and organization.
However, I'm having difficulty doing this. When I create an SSH
connection, the connection automatically dies after I exit the function.
How can I keep this connection alive? I tried storing the connection as
a global variable but it did not work - the connection still closed.
Here is a portion of my script. When I run it, I call "testAll". It
fails on the first line of sftpTest1A:
----------------------------------------
$session = nil
def sftpTest1
puts "Testing sftp..."
$session= Net::SSH.start( '127.0.0.1', :username=>'user',
assword=>'password' )
sftp = $session.sftp.connect
handle = sftp.opendir( "." )
items = sftp.readdir(handle)
puts items
sftp.close_handle( handle )
sftp.close
#At this point, session should still be open, correct?
end
def sftpTest1A
shell = $session.shell.open #FAILS at this line!
shell.pwd
print shell.stdout while shell.stdout?
$stderr.puts "-- stderr: --"
$stderr.print shell.stderr while shell.stderr?
$session.close
end
def testAll
sftpTest1
sftpTest1A
end