#!/usr/bin/expect --

set env(LANG) C 
set env(LC_ALL) C 

set timeout -1

##
# This script is used to execute ssh applications
# like ssh, scp, ssh with remote command support.
#
# Usage:
# rsshcmd <ssh_subcommand> <args>
# e.g: rshcmd ssh -q -l admin 10.5.157.10
# e.g: rshcmd ssh -q -l admin 10.5.157.10 ifconfig -l
# e.g: rshcmd scp test.file.txt root@10.5.157.10:/tmp
##

if { $argc < 1 } {
  puts ""
  puts "Usage: rsshcmd <ssh_subcommand> <args>"
  puts "e.g:"
  puts "   rshcmd ssh -q -l admin 10.5.157.10"
  puts "   rshcmd ssh -q -l admin 10.5.157.10 ifconfig -l"
  puts "   rshcmd scp test.file.txt root@10.5.157.10:/tmp"
  puts ""

  exit 1
}

log_user 0
set pid [spawn /bin/sh -c [join $argv]]
log_user 1
interact
set ret [wait $pid]

if {[llength $ret] == 4} {
  set status [lindex $ret 3]
} else {
  set status [lindex $ret 2]
}

exit $status
