Secure Shell Access & Transfer with Ruby

Discussion in 'Ruby on Rails' started by pradeep, Aug 13, 2013.

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:
    SSH (Secure SHell) is a network protocol designed to be secure using various cryptographic technologies. It's very similar to telnet, but it is secure, so network a eavesdropper would be able to decipher your data that being transmitted over the network. In addition to being secure it has many features like data compression, reverse DNS checking, it can authenticate password-less using Private-Public key pairs.

    In this article we'll see how to programatically SSH into servers, and run programs or other tasks. This can be useful in a variety of scenarios, like running an update on an array of servers, copying files to servers, fetching logs from servers at the end of the day and things the like that, use your imagination.

    Installing Net::SSH gem

    Installation of the gem is very easy, for demonstration purpose I'll install another module Net::SCP synonymous to the *nix command scp.

    Issue the following commands on your shell prompt:
    # gem install net-ssh
    # gem install net-scp

    Using Net::SSH & Net::SCP

    Now that we have understood what SSH is capable of, let us dive into some code examples to get to understand how to implement it, follow the examples below, I have added comments wherever necessary:

    ## load modules
    require 'rubygems'
    require 'net/ssh'
    ## login with password
    Net::SSH.start('', 'pradeep', :password => 'ACoolPassword' ) do |ssh|
      # get all output from a remote process & print it
      output = ssh.exec!("hostname")
      p output
    ## login without password, using key
    ## use ssh-keygen command to genearate a pair of keys
    Net::SSH.start('', 'pradeep', :keys => ['/home/pradeep/test_key'] ) do |ssh|
      output = ssh.exec!("who am i")
      p output
    Now, let us look at copying files over to remote servers using SSH copy:

    require 'rubygems'
    require 'net/scp'
    ## uploading to a remote server
    Net::SCP.upload!('',  'pradeep',"/tmp/testFile", "/home/g4e/html/tmp/fileTest", :keys =>  ['/home/pradeep/test_key'])
    ## downloading from a remote server!('', 'pradeep',"/var/log/g4e/access_log","/tmp/g4e_access_log", :password => 'ACoolPassword')
    ## use a persistant connection
    Net::SCP.start('', 'pradeep', :keys => ['/home/pradeep/test_key']) do |scp|
        ## You can also run multiple downloads in parallel
        DonwLoad1 ="/var/log/g4e/access_log", "/tmp/g4e_access_log")
        DonwLoad2 ="/var/log/g4e/error_log", "/tmp/g4e_error_log")
        ## wait for downloads to complete
        [DonwLoad1, DonwLoad2].each { |d| 
    I hope you have enjoyed the article.
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice