#!/usr/bin/expect -f
#
# $Header: singleBladeOp 1.3 2007/04/25 16:05:28MDT Brian Szmyd (bszmyd) dev  $
#
# $ADICCopyright: Copyright 2007 Quantum Corp. $
#

# Globals
# --------------
set DAB_PROMPT "> $"
# --------------


# Do not send output from our spawned process to STDOUT
log_user 0

# Extract args: format is singleBladeOp IP "cmd"
# where cmd is what you want run on blade
set DAB_CMD [lindex $argv 1];
set IP [lindex $argv 0 ];

# append \r to execute cmd
set DAB_CMD_R "$DAB_CMD \r";

# Return codes:
#    0 success
#    1 Telnet succeeded, but operation faild on blade
#    2 Telnet session could not be established
set ret 0

# Telnet
spawn telnet $IP

expect {
  "No route to host"   { puts "$IP no route to host"   ; set ret 1; }
  "Connection refused" { puts "$IP connection refused" ; set ret 2; }
  timeout              { puts "$IP timed out on telnet"; set ret 3; }
  Sorry                { puts "$IP failed to telnet"   ; set ret 4; }
  $DAB_PROMPT          { set timeout 90
                         send $DAB_CMD_R
                         expect {
                             timeout     { puts "$IP timed out on command" ; set ret 5; }
                             "errno"     { puts "Failed to $DAB_CMD on $IP"; set ret 6; }
                             $DAB_PROMPT { puts "$DAB_CMD success on $IP" }
                         }
                       }
}
close $spawn_id

exit $ret
