Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Ruby on Rails (http://www.go4expert.com/articles/ruby/)
-   -   Secure Shell Access & Transfer with Ruby (http://www.go4expert.com/articles/secure-shell-access-transfer-ruby-t29768/)

pradeep 13Aug2013 09:36

Secure Shell Access & Transfer with Ruby
 
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:
Code:

# 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:

Code: Ruby

## load modules
require 'rubygems'
require 'net/ssh'

## login with password
Net::SSH.start('go4expert.com', 'pradeep', :password => 'ACoolPassword' ) do |ssh|
  # get all output from a remote process & print it
  output = ssh.exec!("hostname")

  p output
end

## login without password, using key
## use ssh-keygen command to genearate a pair of keys
Net::SSH.start('go4expert.com', 'pradeep', :keys => ['/home/pradeep/test_key'] ) do |ssh|
  output = ssh.exec!("who am i")
  p output
end


Now, let us look at copying files over to remote servers using SSH copy:

Code: Ruby

require 'rubygems'
require 'net/scp'

## uploading to a remote server
Net::SCP.upload!('go4expert.com',  'pradeep',"/tmp/testFile", "/home/g4e/html/tmp/fileTest", :keys =>  ['/home/pradeep/test_key'])

## downloading from a remote server
Net::SCP.download!('go4expert.com', 'pradeep',"/var/log/g4e/access_log","/tmp/g4e_access_log", :password => 'ACoolPassword')

## use a persistant connection
Net::SCP.start('go4expert.com', 'pradeep', :keys => ['/home/pradeep/test_key']) do |scp|
    ## You can also run multiple downloads in parallel
    DonwLoad1 = scp.download("/var/log/g4e/access_log", "/tmp/g4e_access_log")
    DonwLoad2 = scp.download("/var/log/g4e/error_log", "/tmp/g4e_error_log")
    ## wait for downloads to complete
    [DonwLoad1, DonwLoad2].each { |d|
        d.wait
    }
end


I hope you have enjoyed the article.


All times are GMT +5.5. The time now is 00:47.