blob: 674378825a650aa907b2d2a6519e19c3d15faba4 [file] [log] [blame]
#!/usr/bin/expect --
# ssh using the parms provided by the caller. The benefit provided by this
# program is that it will enter the password for you (i.e. non-interactively).
# Description of arguments:
# Arg0: The password.
# Arg1: The ssh parm string. This is the totality of ssh parms you wish to
# specify (e.g. userid, host, etc.).
###############################################################################
# Main
# Get arguments.
set password [lindex $argv 0]
set ssh_parms [lreplace $argv 0 0]
eval spawn ssh ${ssh_parms}
set timeout 30
set max_attempts 3
set attempts 0
while { 1 } {
incr attempts 1
expect {
-re "assword:" {
send "$password\r"
break
}
-re "Are you sure you want to continue connecting" {
if { $attempts > $max_attempts } {
puts stderr "**ERROR** Exceeded $max_attempts attempts to ssh."
exit 1
}
send "yes\r"
}
timeout {
puts stderr "**ERROR** Timed out waiting for password prompt."
exit 1
}
eof {
puts stderr "**ERROR** End of data waiting for password prompt."
exit 1
}
}
}
set timeout 3
expect {
"Permission denied, please try again." {
puts ""
puts "**ERROR** Incorrect userid or password provided to this program."
exit 1
}
}
set timeout -1
# We don't ever expect to see this string. This will keep this program
# running indefinitely.
set never_string "When in the course of human events, it becomes :"
if { [ catch {expect { "${never_string}" {send "whatever\r"} }} result ] } {
set child_died {expect:[ ]spawn[ ]id[ ]exp4[ ]not[ ]open}
if { [regexp -expanded ${child_died} $result] } {
# The child died. This is not necessarily an error (for example, the
# user may have included a command string to run on the target).
exit 0
} else {
puts $result
exit 1
}
}
exit 0
###############################################################################