#!/usr/bin/expect -f
#
# Quantum Confidential
#
# Automated Tape Library Firmware Source
# (C) Copyright Quantum 2011. All Rights Reserved.
# Non-published
#

# 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; }
  -re $DAB_PROMPT       { set timeout 300
                           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; }
                              -re $DAB_PROMPT { puts "$DAB_CMD success on $IP" }
                           }
                        }
}
close $spawn_id

exit $ret
