Ruby scripts for daily unix system administration

Hi, I’m a newbie looking for any example of writing ruby script to do my
daily sysadmin chores. If anyone here knows their existence, please forward
me with the link. Thanks a bunch.

Take a look at

http://www.rubygarden.org/ruby?SysAdmin

Please post suggestions for others. People will happily write some
good ones and add them to the collection.

Gavin

···

On Wednesday, February 19, 2003, 5:10:15 PM, Useko wrote:

Hi, I’m a newbie looking for any example of writing ruby script to do my
daily sysadmin chores. If anyone here knows their existence, please forward
me with the link. Thanks a bunch.

Actually, I’d like to see them too …

···

On Wed, 19 Feb 2003, Useko Netsumi wrote:

Hi, I’m a newbie looking for any example of writing ruby script to do my
daily sysadmin chores. If anyone here knows their existence, please forward
me with the link. Thanks a bunch.

Hi. This is a smbfs.sh replacement for FreeBSD’s /usr/local/etc/rc.d. It
mounts smbfs mounts found in /etc/fstab.

smbfs.sh (459 Bytes)

···

On Wed, Feb 19, 2003 at 03:10:15PM +0900, Useko Netsumi wrote:

Hi, I’m a newbie looking for any example of writing ruby script to do my
daily sysadmin chores. If anyone here knows their existence, please forward
me with the link. Thanks a bunch.


Yours truly, WBR, Paul Argentoff.

In article b2v6hr$1gua53$1@ID-159205.news.dfncis.de,
usenets@nyc.rr.com says…

Hi, I’m a newbie looking for any example of writing ruby script to do my
daily sysadmin chores. If anyone here knows their existence, please forward
me with the link. Thanks a bunch.

I’ve currently written a function which walks recursively through an
directory performing different callbacks for every file and every
directory. These callbacks may perform any task you want, for example
backups. Maybe somedays I’ll have enough time to create a complete
backup solution fron this. Currently this function is used for
performing testcases.

This script is just another version of a common task and far from
perfect, but I think, for a beginner it is a good start.

Michael B.

---------------------------- SOS (Start Of Script ;)--------------

traverse through an directory and its subdirectories

@param name Name of the directory to be traversed.

@param wildcard Wildcard for file entries.

@param prFile Procedure to be executed for every file.

@param prDir Procedure to be executed for every directory.

If this parameter is “false”, no recursion is done.

@param para This parameter is passed to the Procedures.

def traverseDir name, wildcard = /.*/, prFile = nil, prDir = nil,
para = nil
skip = false
begin
dd = Dir.open name
rescue SystemCallError
skip = true
end
unless skip
unless prFile == nil
dd.grep(wildcard){|e|
e = File.join(name,e)
prFile.call(e,para)
unless File.stat(e).directory?
}
end
unless prDir == false
dd.rewind
a=dd.find_all{|x|File.stat(
File.join (name,x)).directory? and x != ‘.’ and x != ‘…’}
a.each{|d|
d = File.join(name,d)
traverseDir(d,wildcard,prFile,prDir,para)
if prDir == nil or prDir.call(d,para)
}
end
end
end

List all *.exe-Files on

#df = proc{|s,pre| p “#{pre}<#{s}>:”;true}
#ff = proc{|s,pre| p “—#{pre}#{s}”}

···

#[“d:/”,“e:/”].each{|x|traverseDir(x,/.exe/,ff,df,“—>”)}

Thank you, but its a bit advance for me. I’d like to start on how to parse
arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

Using Bourne shell, I can use case statement and awk to parse each argument
and capture its value …

if -c then set count = c0
if -b then set blocksize = b0
if -f then set filename = 0

I’d love to get some simple example to do the above. Thanks.

“Gavin Sinclair” gsinclair@soyabean.com.au wrote in message
news:127599967147.20030219172252@soyabean.com.au…

Hi, I’m a newbie looking for any example of writing ruby script to do my
daily sysadmin chores. If anyone here knows their existence, please
forward

···

On Wednesday, February 19, 2003, 5:10:15 PM, Useko wrote:

me with the link. Thanks a bunch.

Take a look at

http://www.rubygarden.org/ruby?SysAdmin

Please post suggestions for others. People will happily write some
good ones and add them to the collection.

Gavin

Thanks Paul. I wish there are more of this conversion for, say, all the
startup/shutdown scripts in /etc/init.d/*. It is also gives us sysadmin some
working example that we can use for our daily admin.

I wish there is a repository of it somehere. Please post if anyone come
across them.

Thanks again.

“Paul Argentoff” argentoff@rtelekom.ru wrote in message
news:20030219074140.GA37796@paul.rtelekom.ru

Hi, I’m a newbie looking for any example of writing ruby script to do my
daily sysadmin chores. If anyone here knows their existence, please
forward
me with the link. Thanks a bunch.

Hi. This is a smbfs.sh replacement for FreeBSD’s /usr/local/etc/rc.d. It
mounts smbfs mounts found in /etc/fstab.

···

On Wed, Feb 19, 2003 at 03:10:15PM +0900, Useko Netsumi wrote:

Yours truly, WBR, Paul Argentoff.

Michael Bruschkewitz brusch2@gmx.net wrote in message news:MPG.18bed9327df8e79398968e@192.168.0.61

I’ve currently written a function which walks recursively through an
directory performing different callbacks for every file and every
directory.

Dir[‘**/*’] might help make your code shorter :slight_smile:

Tom

Thank you, but its a bit advance for me. I’d like to start on how to parse
arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

Using Bourne shell, I can use case statement and awk to parse each argument
and capture its value …

if -c then set count = c0
if -b then set blocksize = b0
if -f then set filename = 0

I’d love to get some simple example to do the above. Thanks.

This is just off the top of my head, I’m sure there’s better ways:

#!/usr/bin/env ruby

count = nil
f = nil
ARGV.each_with_index{ |x, i|

case x
when ‘-c’
count = ARGV[i+1]
when ‘-f’
f = ARGV[i+1]
end

}

puts count
puts f

···

On Wed, 19 Feb 2003 16:30:30 +0900 “Useko Netsumi” usenets@nyc.rr.com wrote:

“Gavin Sinclair” gsinclair@soyabean.com.au wrote in message
news:127599967147.20030219172252@soyabean.com.au…

On Wednesday, February 19, 2003, 5:10:15 PM, Useko wrote:

Hi, I’m a newbie looking for any example of writing ruby script to do my
daily sysadmin chores. If anyone here knows their existence, please
forward
me with the link. Thanks a bunch.

Take a look at

http://www.rubygarden.org/ruby?SysAdmin

Please post suggestions for others. People will happily write some
good ones and add them to the collection.

Gavin


Daniel P. Zepeda

Parsing arguments is best done with GetoptLong. See pickaxe.

optparse.rb (in the current source tree) is reputed to have some
benefits, but I don’t know of any documentation, apart from perhaps
in the source code.

Gavin

···

On Wednesday, February 19, 2003, 6:30:30 PM, Useko wrote:

Thank you, but its a bit advance for me. I’d like to start on how to parse
arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

“Useko Netsumi” usenets@nyc.rr.com wrote in message news:b2vb7j$1heg65$1@ID-159205.news.dfncis.de

Thank you, but its a bit advance for me. I’d like to start on how to parse
arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

The getoptlong and optparse modules are your friend. They both parse
command line arguments but are quite different in design. getoptlong
is easier to use, but optparse is allegedly more powerful.

Getoptlong example:

-----8<-----
require ‘getoptlong’

count = blocksize = filename = nil # default values
GetoptLong.new(
[‘-c’, GetoptLong::REQUIRED_ARGUMENT],
[‘-b’, GetoptLong::REQUIRED_ARGUMENT],
[‘-f’, GetoptLong::REQUIRED_ARGUMENT]).each do |opt, arg|
case opt
when ‘-c’ then count = arg
when ‘-b’ then blocksize = arg
when ‘-f’ then filename = arg
end
end
-----8<-----

Regards,

Tom

“Tom Payne” google@tompayne.org wrote in message
news:6756066a.0302200840.6adeb44c@posting.google.com

Michael Bruschkewitz brusch2@gmx.net wrote in message
news:MPG.18bed9327df8e79398968e@192.168.0.61

I’ve currently written a function which walks recursively through an
directory performing different callbacks for every file and every
directory.

Dir[‘**/*’] might help make your code shorter :slight_smile:

I had a dirwalker problem for fixing lowercase file and directory names
after moving a Perforce repository from Windows 2K to FreeBSD. Perforce do
have perl scripts for the purpose, but I was anxious to see something work,
so I cooked the following script based on error messages generated by the
verify command. It doesn’t address all issues, but handled 90%.

The verify.errors format is essentially “//path/to/file# some more text on
same line”.

The script looks for paths that matches the lowercase version and renames to
the case found in the file. It adds “,v” because this is what 90% of the
files are stored with in the repository (depot).

The script handles the 300+ incorrectly named files and many more directory
lookups in well under a second on a 400MHz PII laptop machine.

Mikkel

# script to convert file and directory names from lowercase to # case stored in perforce metadata # parses output previously generated by "p4 verify -q //... > verify.errors"

root = ARGV[0]

if not root or not File.exists?(root)
root = ENV[“P4ROOT”]
end
if not root or not File.exists?(root)
p “unspecified root”
exit
end

if not File.exists?(“verify.errors”)
p “verify.errors must be in current path”
exit
end

File.new(“verify.errors”).each { |l|
m = /^//(.*)#/.match(l)
if m && m[1]
path = root
file = m[1] + “,v”
file.split(‘/’).each { |n|
oldpath = path
path = oldpath + “/” + n
p "? " + path
next if File.exists?(path)
oldpath = oldpath + “/” + n.downcase
if File.exists?(oldpath)
p oldpath + " ==> " + path
File.rename(oldpath, path)
end
}
end
}

In article 6756066a.0302200840.6adeb44c@posting.google.com,
google@tompayne.org says…

Dir[‘**/*’] might help make your code shorter :slight_smile:
Maybe if it helps to find a knife :))

Thanks Gavin. If you see any good simple example of using GetoptLong, please
post them for us. Thanks again.

“Gavin Sinclair” gsinclair@soyabean.com.au wrote in message
news:74627672.20030219204249@soyabean.com.au…

Thank you, but its a bit advance for me. I’d like to start on how to
parse

···

On Wednesday, February 19, 2003, 6:30:30 PM, Useko wrote:

arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

Parsing arguments is best done with GetoptLong. See pickaxe.

optparse.rb (in the current source tree) is reputed to have some
benefits, but I don’t know of any documentation, apart from perhaps
in the source code.

Gavin

Thanks for the example. I got it to works with a very minor correction(put $
on variable name,e.g. $count, $blocksize, and $filename).

Suppose that I accidently put -g instead of -c, instead of giving me an
error, it should just ignore it, just like an *) in the ‘case’ statement
for everything else it does not understand

Thanks

“Tom Payne” google@tompayne.org wrote in message
news:6756066a.0302190627.754e22f9@posting.google.com

“Useko Netsumi” usenets@nyc.rr.com wrote in message
news:b2vb7j$1heg65$1@ID-159205.news.dfncis.de

Thank you, but its a bit advance for me. I’d like to start on how to
parse

···

arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

The getoptlong and optparse modules are your friend. They both parse
command line arguments but are quite different in design. getoptlong
is easier to use, but optparse is allegedly more powerful.

Getoptlong example:

-----8<-----
require ‘getoptlong’

count = blocksize = filename = nil # default values
GetoptLong.new(
[‘-c’, GetoptLong::REQUIRED_ARGUMENT],
[‘-b’, GetoptLong::REQUIRED_ARGUMENT],
[‘-f’, GetoptLong::REQUIRED_ARGUMENT]).each do |opt, arg|
case opt
when ‘-c’ then count = arg
when ‘-b’ then blocksize = arg
when ‘-f’ then filename = arg
end
end
-----8<-----

Regards,

Tom

See pickaxe!

···

On Thursday, February 20, 2003, 1:30:59 AM, Useko wrote:

Thanks Gavin. If you see any good simple example of using GetoptLong, please
post them for us. Thanks again.

“Gavin Sinclair” gsinclair@soyabean.com.au wrote in message
news:74627672.20030219204249@soyabean.com.au…

On Wednesday, February 19, 2003, 6:30:30 PM, Useko wrote:

Thank you, but its a bit advance for me. I’d like to start on how to
parse
arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

Parsing arguments is best done with GetoptLong. See pickaxe.

optparse.rb (in the current source tree) is reputed to have some
benefits, but I don’t know of any documentation, apart from perhaps
in the source code.

Gavin

I got it to works with a very minor correction(put $
on variable name,e.g. $count, $blocksize, and
$filename).

I’ve made a booboo…putting $ on the variable name makes the global
variables. Thanks to David for correcting me.

The code supplied by Tom Payne should work as it is(again, thanks Tom).

“Useko Netsumi” usenets@nyc.rr.com wrote in message
news:b308k0$1hasfc$1@ID-159205.news.dfncis.de

Thanks for the example. I got it to works with a very minor correction(put
$

···

on variable name,e.g. $count, $blocksize, and $filename).

Suppose that I accidently put -g instead of -c, instead of giving me an
error, it should just ignore it, just like an *) in the ‘case’ statement
for everything else it does not understand

Thanks

“Tom Payne” google@tompayne.org wrote in message
news:6756066a.0302190627.754e22f9@posting.google.com

“Useko Netsumi” usenets@nyc.rr.com wrote in message
news:b2vb7j$1heg65$1@ID-159205.news.dfncis.de

Thank you, but its a bit advance for me. I’d like to start on how to
parse
arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

The getoptlong and optparse modules are your friend. They both parse
command line arguments but are quite different in design. getoptlong
is easier to use, but optparse is allegedly more powerful.

Getoptlong example:

-----8<-----
require ‘getoptlong’

count = blocksize = filename = nil # default values
GetoptLong.new(
[‘-c’, GetoptLong::REQUIRED_ARGUMENT],
[‘-b’, GetoptLong::REQUIRED_ARGUMENT],
[‘-f’, GetoptLong::REQUIRED_ARGUMENT]).each do |opt, arg|
case opt
when ‘-c’ then count = arg
when ‘-b’ then blocksize = arg
when ‘-f’ then filename = arg
end
end
-----8<-----

Regards,

Tom

Below is a boilerplate that I use for my apps:

#! /usr/bin/env ruby

···

On Wednesday, 19 February 2003 at 23:30:59 +0900, Useko Netsumi wrote:

Thanks Gavin. If you see any good simple example of using GetoptLong, please
post them for us. Thanks again.

“Gavin Sinclair” gsinclair@soyabean.com.au wrote in message
news:74627672.20030219204249@soyabean.com.au…

On Wednesday, February 19, 2003, 6:30:30 PM, Useko wrote:

Thank you, but its a bit advance for me. I’d like to start on how to
parse
arguments from a shell scripts such as the following:

usage: mktmpswap -c c0 -b b0 -f f0

Parsing arguments is best done with GetoptLong. See pickaxe.

$Id$

$Source$

Author: Jim Freeze

Copyright (c) 2002 Jim Freeze

=DESCRIPTION

TODO: Add your description

=REVISION HISTORY

INI MO/DY/YEAR Changes

require ‘getoptlong’

TODO: Add Description Here

class App
VERSION = "$Revision$
REVISION_DATE = "$Date$
AUTHOR = “Jim Freeze”

Returns a version string similar to:

<app_name>: Version: 1.2 Created on: 2002/05/08 by Jim Freeze

The version number is maintained by CVS.

The date is the last checkin date of this file.

def version
"Version: #{VERSION.split[1]} Created on: " +
“#{REVISION_DATE.split[1]} by #{AUTHOR}”
end

TODO: Add description here

def initialize
@debug = false
@verbose = false

if 0 == ARGV.size
  STDERR.puts usage
  exit 0
end

get_options
rescue => err
  STDERR.puts err
  STDERR.puts usage
  exit 1

end

Returns usage string

def usage
#
# TODO: Fill out usage
#
<<-USAGE
Usage: #{File.basename $0} [-v] file
-v|–verbose print intermediate steps to STDERR
USAGE
end

Processes command line arguments

def get_options
opts = GetoptLong.new(
[ “–example”, “-e”, GetoptLong::REQUIRED_ARGUMENT ],
[ “–verbose”, “-v”, GetoptLong::NO_ARGUMENT ]
)

## process the parsed options
opts.each do |opt, arg|
  case opt
    when "--verbose"
      @verbose = true
    else
      raise "Invalid option '#{opt}'."
  end#case
end#each

raise "Missing file from the commandline." if 1 != ARGV.size

rescue NameError => err
  STDERR.puts "ERROR: #{err}"
  exit 1
rescue => err
  STDERR.puts "ERROR: #{err}"
  exit 1

end#get_opts

Launches the application

App.new.run

def run
## TODO: Add code here

end#run

end#App

app = App.new.run


Jim Freeze

I predict that today will be remembered until tomorrow!

Thanks for the example. I got it to works with a very minor correction(put $
on variable name,e.g. $count, $blocksize, and $filename).

You just made them global variables. It won’t matter now, but if
you put this in a function later, then it will. You should not have
had to do this for the script below to work. Invoke ruby with -w,
like
/usr/local/bin/ruby -w my_script.rb
as this can give useful hints as to what is happening.

Suppose that I accidently put -g instead of -c, instead of giving me an
error, it should just ignore it, just like an *) in the ‘case’ statement

That is the bourne shell case statement. Ruby uses “else” for the
same meaning. I’d advise you to look at some tutorial examples to
get a feel for this sort of thing. I’ve collected a few links here:
http://www.eng.cse.dmu.ac.uk/~hgs/ruby/#tutorials

for everything else it does not understand

Thanks
[…]

Getoptlong example:

-----8<-----
require ‘getoptlong’

count = blocksize = filename = nil # default values
GetoptLong.new(
[‘-c’, GetoptLong::REQUIRED_ARGUMENT],
[‘-b’, GetoptLong::REQUIRED_ARGUMENT],
[‘-f’, GetoptLong::REQUIRED_ARGUMENT]).each do |opt, arg|
case opt
when ‘-c’ then count = arg
when ‘-b’ then blocksize = arg
when ‘-f’ then filename = arg
end
end
-----8<-----

Regards,

    Hugh
···

On Thu, 20 Feb 2003, Useko Netsumi wrote:

Jim:

Seems like you could put a boatload of this functionality into a common
base class that all your apps could share.

Cheers

Dave

···

On Wednesday, Feb 19, 2003, at 15:11 US/Central, Jim Freeze wrote:

Below is a boilerplate that I use for my apps: