You are currently on IBM Systems Media’s archival website. Click here to view our new website.

AIX > Tips & Techniques > Systems Management

Simplify Advanced Shell Scripting With Ruby

To learn more about this topic, read the following articles: 
On-Demand Systems Management
AIX Shell Scripting Made Simple
AIX Shell Scripting Made Easy

Ruby is just for experienced programmers. right? False! Originating in the mid-1990s, Ruby is a fantastic language, made popular recently by the Web application framework—Ruby on Rails. It’s a massively flexible and easy language to learn. You can find Ruby for AIX in the AIX Toolbox at the Bull Freeware. The script we’ll be creating will do the following:


  1. Go through a large list of servers.
  2. SSH them and determine whether the server is AIX.
  3. If so, it will go through all disks, returning their type.
  4. It will do all of this completely in parallel in batch at 25 sub-processes at a time.

In the script below, I will explain, line-by-line, how it works.


Any great script will begin with a shebang line. Ruby is no different.

require 'timeout'
require 'optparse'

Any require basically means it’s running another file. Think of it as importing a library. In this case we’re importing two “libraries.” The timeout library will be used in a mechanism to quit out of a block code based on a set time. This is important because we're working with SSH.

# Parallel variables
limit = 25
processes = []

# Server variable
servers = ""

Here we’re declaring some variables. The first two, limit and processes, define our parallel processing. The limit is the number of subshells, and the processes, an empty array we’re creating. The servers statement is a placeholder variable. It’s empty, so we can use it later. In Ruby, you don’t need to explicitly define variables; but this allows us to use it throughout the script because of variable scoping.

# Functions #
def ping_server(server)
  result =`ping -q -c 1 #{server} > /dev/null 2>&1`
  if ($?.exitstatus != 0)
    return "not_ping"

Defining in methods (which are like functions) is simple, use: def function(variable). In this method, we’re passing it a server, pinging the server and returning yay or nay.

def get_os(server)
    Timeout::timeout(10) do
      os = %x[ssh -q -o BatchMode=yes #{server} 
"uname 2>/dev/null"].chomp if ! os.nil? return os else return "no_connection" end end rescue Timeout::Error return "no_connection" end end

In this method, we’re passing it a server, SSHing out to that server and executing an uname on it. Doing this will determine whether the server is AIX or Linux. You can see within this code block, we’re using our timeout mechanism. So anything inside that code block has 10 seconds to finish, or it will error out. Some import parts:

  • Anything inside %x[] is executed on the shell outside of Ruby. What we’re trying to do is shell out and do our SSH.
  • Anything that is return x returns that outside of our method.
  • Anything inside begin code blocks is setup for exception handling. It’s how we determine errors and how to handle them.
  • If logic statements are similar in bash, in Ruby, you simply use if, and end. There’s no need for then.
# Option parser #
options = {} do |o|
        o.on('-s servers') { |servers| servers = servers }
        o.on('-h') { puts o; exit }

This is an option parses mechanism. It controls what we input on the command line. Like so: ./ruby_script.rb -f option_one -b option_two. The only option we care about is the -s. This is our list of servers. So we execute the script like so:

./which_storage.rb -s "server1 server2 server3 server4" 

if servers.nil? || servers.empty?
  puts "Specify a server." 

Here, we’re ensuring we have all the needed variables. Basically, we need to make sure we have our server variable. Note that nil means it returned empty or nothing, and puts is similar to echo in bash.

Andrew Wojnarek is a systems engineer with ATS Group and an IBM Champion.



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

Disk I/O and the Network

Increase performance with more tips for AIX 5.3, 6.1 and 7


Paging, Memory and I/O Delays

How to tune AIX versions 5.3, 6.1 and 7 for increased performance

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters