HeatWare.net header image
HeatWare.net
TwitterRSSEmail
  • Home
  • Windows
    • Windows 8
    • Windows 7
    • Windows Vista
    • Windows XP
  • Linux/Unix
  • Mac
  • Mobile
    • Android
    • iOS
    • Phones (Help / Resources)
  • Software / Programming
    • Free Software
    • Programming – General
    • PHP
    • Ruby/Rails
    • Quality Assurance (QA)
    • Software – General
    • Software Help
    • Databases
  • Cool Websites
  • Other
    • Deals & Bargains
    • News
    • Video Games
    • Hardware
    • Electronics
  • About

0 comments / January 20, 2011 / sood / Programming – Ruby/Rails

How to SSH to a server using Ruby – Part II

Welcome to Part II of how to SSH to a server using Ruby.  In Part I of this article, we gave you a simple example of how to SSH to a server and run a command. The hostname, username, password, and the command to execute were hardcoded in the example.

In this article, we will expand on the first example by allowing you to specify the server connection information and the command to execute as command-line parameters.  This will allow you to use the same script to connect to a variety of servers and execute any command without modifying the script over and over.

Ruby Syntax

require 'rubygems'
require 'net/ssh'
require 'optparse'

opts = OptionParser.new
opts.on("-h HOSTNAME", "--hostname NAME", String, "Hostname of Server") { |v| @hostname = v }
opts.on("-u SSH USERNAME", "--username SSH USERNAME", String, "SSH Username of Server") { |v| @username = v }
opts.on("-p SSH PASSWORD", "--password SSH PASSWORD", String, "SSH Password of Server") { |v| @password = v }
opts.on("-c SHELL_COMMAND", "--command SHELL_COMMAND", String, "Shell Command to Execute") { |v| @cmd = v }
begin
  opts.parse!(ARGV)
rescue OptionParser::ParseError => e
  puts e
end
raise OptionParser::MissingArgument, "Hostname [-h]" if @hostname.nil?
raise OptionParser::MissingArgument, "SSH Username [-u]" if @username.nil?
raise OptionParser::MissingArgument, "SSH Password [-p]" if @password.nil?
raise OptionParser::MissingArgument, "Command to Execute [-c]" if @cmd.nil?

 begin
    ssh = Net::SSH.start(@hostname, @username, :password => @password)
    res = ssh.exec!(@cmd)
    ssh.close
    puts res
  rescue
    puts "Unable to connect to #{@hostname} using #{@username}/#{@password}"
  end

Example of how to execute the script

ruby script_name.rb -h localhost -u root -p password -c "ls -al"

As you can see, the hostname, username, password, and command are all parameters passed in to the script. All of these parameters are required, so if you forget one, it will spit out an error. Give it a try!

Part I – How to SSH using Ruby, a simple example

Part II – How to SSH using Ruby, with command-line arguments

Related Posts

  • How to SSH to a server using Ruby - Part I
  • What is the login name and password in VMware Server 2.0?
  • How to install Ruby 1.8.7 on CentOS 5.5 Linux
  • Using Linux ‘head’ command to preview a file
  • Scripting the Linux 'passwd' command easily without interactive prompt

ruby

0 comments… add one
Cancel reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Why You Should Consider Diving Apps
  • How to Buy a Smartphone that Fits Your Budget
  • How to Overcome Frustrating PDF Stress
  • Convert PDF to Word: Easy, Reliable and Quality Conversion
  • PDF to Excel App ­ – A New Way of Handling Tricky Documents

Recent Comments

  • Valarie Walter on Basic Troubleshooting Steps for your Cell Phone
  • John Mists on A Brief History of Android OS
  • syarif on PostgreSQL: How to reload config settings without restarting database
  • Raghu on How to SSH to a server using Ruby – Part I
  • francisco clemente on Basic Troubleshooting Steps for your Cell Phone

Tags

ACSLS Android Cell Phones Cool Software Cool Websites Databases Facebook File System Free Software G2x Galaxy S5 Hot Deals iOS iPhone ISO LG Linux Linux/Unix Mac Mobile mysql OpenStack OS X PHP Postgres PostgreSQL Printers Programming ruby Samsung Galaxy S6 Shell Smartphones Sun T-Mobile Tips Tips & Tricks Ubuntu Unix Virtualization VMWare Windows Windows 7 Windows 8 Windows Vista Windows XP

Latest Tweet

Follow @HeatwaredotNet

SP
@HeatwaredotNet

  • Why You Should Consider Diving Apps https://t.co/Is41cdUv2I #diving-apps
    about 5 years ago

All Categories

Other Links

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2015 — HeatWare