Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to maintain a single network connection? (newbie)

Reply
Thread Tools

How to maintain a single network connection? (newbie)

 
 
Fenster Blick
Guest
Posts: n/a
 
      12-14-2007
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
John Pywtorak
Guest
Posts: n/a
 
      12-14-2007
Hi Fenster,

I suspect it is the sftp.close stanza that is causing the problem. Try
removing that, what happens?

Also, I would recommend using the block form of Net::SSH.start if you
can. That is

Net::SSH.start(...) do |session|
#do all your work here, pass the session to your methods
end

Johnny P

Fenster Blick wrote:
> 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


 
Reply With Quote
 
 
 
 
Fenster Blick
Guest
Posts: n/a
 
      12-15-2007
Thanks for the reply. Unfortunately, that didn't work.

However, I did end up solving the issue - after taking a break and
thinking about it away from the computer. The proper way to approach the
issue was to utilize a class.

I should have created an instance variable for the connection in a
separate Server class. In this way, I can keep a connection alive as
long as a Server object is alive.

I ran into difficulties when I approached the problem like a Visual
Basic script... all's better now!

John Pywtorak wrote:
> Hi Fenster,
>
> I suspect it is the sftp.close stanza that is causing the problem. Try
> removing that, what happens?
>
> Also, I would recommend using the block form of Net::SSH.start if you
> can. That is
>
> Net::SSH.start(...) do |session|
> #do all your work here, pass the session to your methods
> end
>
> Johnny P


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Custom Taglib problems - instead of a single instance per page, I have a single instance per application. chris brat Java 1 05-10-2006 11:16 AM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
BoundColumn single DataBind equiv but not in a single row? Randall Parker ASP .Net 1 12-12-2005 04:11 PM
Maintain Single Login in different Live Servers =?Utf-8?B?UHJvbm9qaXQgUm95?= ASP .Net 1 12-29-2004 05:29 PM



Advertisments