SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Net::SSH and Case

    Hello everyone.

    For my first little Ruby project, I'm developing a script that will run through an array of linux/UNIX host names and perform actions on each (depending on what platform it is). However, it seems my code dies at a certain point when there is more to do.

    First, it loads a few arrays with sample data. Then, it iterates through the array of platforms (Linux, Solaris, etc), and connects to it via SSH to perform commands remotely (in this example it only connects to Linux). However, it does not seem to get past the red area if I use the puts notification. When (puts "This is a Linux box!") is run, it displays the message fine, but then nothing else is displayed, no matter if its the very last line of the script or not. However, if you comment out the red line, the script runs fine.

    Code:
    require 'net/ssh'
    
    arrPlatforms = ["Linux", "Solaris"]
    arrHostNames = ["linuxtest.com", "solaristest.com"]
    arrUserNames = ["Joe", "Joe"]
    arrUserPwds = ["mypassword1", "mypassword2"]
    
    n = 0
    
    while n < arrPlatforms.length
      platform = arrPlatforms[n]
      host_name = arrHostNames[n]
      user_name = arrUserNames[n]
      user_pwd = arrUserPwds[n]
    
      case platform
        when "Linux"
          puts "This is a Linux box!"
          #INITIATE SSH CONNECTION
          Net::SSH.start( host_name, user_name, user_pwd ) do |session|
            shell = session.shell.sync
            puts shell.send_command( "cat /proc/cpuinfo | grep 'model name' | sed 's/.*://g; s/^[ /t]*//'" ).stdout
          end
        when "Solaris"
          puts "This is a Solaris box"
      end
      n += 1
    end
    Can anyone explain why this would happen? Thanks!

  2. #2
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm only guessing, but are you sure that the connections are being closed? Also, you are missing an "else" in your case block that catches every other platform.
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  3. #3
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, so the issue had nothing to do with the code, but that I was running the code in the FreeRIDE editor, and was producing incorrect output. Maybe some bug, I don't know.

    Running this code in SciTE, for example, produces the correct output.

    Thanks for your suggestion, Tom!

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomK32 View Post
    I'm only guessing, but are you sure that the connections are being closed?
    FYI With most Net methods, if you use them with a block, the connection is automatically tidied up when the block is closed. The Net::SSH api documentation confirms this is the case with this module too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •