Ruby expect example

#!/usr/bin/ruby
#
# $Id$
#
# openf.rb
#
 
require 'pty'
require 'expect'
require 'getoptlong'
 
VER = "0.1a Dec 28, 2005"
 
def doHelp(h,c,l)
puts <<END
    #{__FILE__} #{VER}
    --host|-H    = file from which to read list of hosts : Default #{h}"
    --command|-c = file from which to read commands      : Default #{c}"
    --log|-l     = Log output to file                    : Default #{l}"
    --help|-h    = print this help
END
exit -1
end
 
def doOutput(terminal,msg)
    terminal.puts msg
end
 
def echoOff
    `stty -echo`
end
 
def echoOn
    `stty echo`
end
 
def getPassword
    STDERR.print "enter password:"
    echoOff
    passwd = STDIN.gets
    echoOn
    passwd
end
 
def readLines(fileName)
    lineArray = Array.new
 
    if (File.exists?(fileName)) then
        index = 0
        fin = File.open(fileName)
        fin.each_line { |lin|
            oneLine = lin.chomp.strip
            if ((oneLine.length > 1) && (oneLine.grep(/^#/).empty?)) then
                lineArray[index] = oneLine
                index = index.succ
            end
        }
    else
        STDERR.puts "Unable to access #{fileName}"
    end
    return lineArray
end
 
# ===== main() =====
hostFile = "hosts"
cmdFile = "commands"
logTo = "STDOUT"
prompt = "%"
 
hosts = Hash.new
commands = Array.new
 
opts = GetoptLong.new(
    [ "--host",    "-H", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--command", "-c", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--log", "-l", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--help", "-?", GetoptLong::OPTIONAL_ARGUMENT]
)
 
opts.each { |key,value|
    puts "Key : #{key} Value : #{value}"
    hostFile = value if (!key.grep(/host/).empty?)
    hostFile = value if (!key.grep(/H/).empty?)
    cmdFile = value if (!key.grep(/command/).empty?)
    logTo = value if (!key.grep(/log/).empty?)
}
 
puts "hostFile = #{hostFile}"
puts "cmdFile = #{cmdFile}"
puts "logFile = #{logTo}"
 
if (File.exist?(hostFile)) then
    hosts = readLines(hostFile)
else
    msg = "*ERROR* : Unable to open hostfile : #{hostFile}"
    STDERR.puts msg
    exit 1
end
 
if (File.exist?(cmdFile)) then
    commands = readLines(cmdFile)
else
    msg = "*ERROR* : Unable to open command file : #{hostFile}"
    STDERR.puts msg
    exit 2
end
 
puts "Hosts size : #{hosts.size}"
hosts.each { |h|
   puts "host #{h}"
}
puts "Commands size : #{commands.size}"
commands.each { |c|
   puts "command: #{c}"
}
 
trap("INT") {
    #r.flush
    #w.flush
    STDERR.puts("Interrupt")
    exit -1
}
 
# get password
passwd = getPassword()
puts
handlePassword = 0
hosts.each { |node|
    commands.each { |cmd|
        #puts "exec #{cmd} on #{node}"
 
        cmdStr = "ssh #{node} '#{cmd}'"
        puts "#{'='*5} #{cmdStr} #{'='*5}"
 
        begin
        puts "cmdStr #{cmdStr}"
        PTY.spawn(cmdStr) { |r,w|
            r.sync = true
            w.sync = true
 
            if (handlePassword) then
            if (r.expect(/assword:/)) then
                w.puts(passwd)
                r.gets  # flush out passwd line so it does not print to stdout
            end
            end
 
            until (r.eof?)
                line = r.gets
                puts line # if (line.grep(/#{passwd}/).empty?)
            end
            r.flush
            sleep 1
 
        }
        rescue => errMsg
            puts #{errMsg}
        end
    }
}
# eof : openf.rb

openf_rb.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0