1ba2d2c2dSDavid Shaw#!/usr/bin/expect -- 2ba2d2c2dSDavid Shaw 3ba2d2c2dSDavid Shaw# ssh using the parms provided by the caller. The benefit provided by this 4ba2d2c2dSDavid Shaw# program is that it will enter the password for you (i.e. non-interactively). 5ba2d2c2dSDavid Shaw 6ba2d2c2dSDavid Shaw# Description of arguments: 7ba2d2c2dSDavid Shaw# Arg0: The password. 8ba2d2c2dSDavid Shaw# Arg1: The ssh parm string. This is the totality of ssh parms you wish to 9ba2d2c2dSDavid Shaw# specify (e.g. userid, host, etc.). 10ba2d2c2dSDavid Shaw 11ba2d2c2dSDavid Shaw 12ba2d2c2dSDavid Shaw############################################################################### 13ba2d2c2dSDavid Shaw# Main 14ba2d2c2dSDavid Shaw 15ba2d2c2dSDavid Shaw # Get arguments. 16ba2d2c2dSDavid Shaw set password [lindex $argv 0] 17ba2d2c2dSDavid Shaw set ssh_parms [lreplace $argv 0 0] 18ba2d2c2dSDavid Shaw 19ba2d2c2dSDavid Shaw eval spawn ssh ${ssh_parms} 20ba2d2c2dSDavid Shaw 21ba2d2c2dSDavid Shaw set timeout 30 22ba2d2c2dSDavid Shaw 23ba2d2c2dSDavid Shaw set max_attempts 3 24ba2d2c2dSDavid Shaw 25ba2d2c2dSDavid Shaw set attempts 0 26ba2d2c2dSDavid Shaw while { 1 } { 27ba2d2c2dSDavid Shaw incr attempts 1 28ba2d2c2dSDavid Shaw expect { 29ba2d2c2dSDavid Shaw -re "assword:" { 30ba2d2c2dSDavid Shaw send "$password\r" 31ba2d2c2dSDavid Shaw break 32ba2d2c2dSDavid Shaw } 33ba2d2c2dSDavid Shaw -re "Are you sure you want to continue connecting" { 34ba2d2c2dSDavid Shaw if { $attempts > $max_attempts } { 35ba2d2c2dSDavid Shaw puts stderr "**ERROR** Exceeded $max_attempts attempts to ssh." 36ba2d2c2dSDavid Shaw exit 1 37ba2d2c2dSDavid Shaw } 38ba2d2c2dSDavid Shaw send "yes\r" 39ba2d2c2dSDavid Shaw } 40ba2d2c2dSDavid Shaw timeout { 41ba2d2c2dSDavid Shaw puts stderr "**ERROR** Timed out waiting for password prompt." 42ba2d2c2dSDavid Shaw exit 1 43ba2d2c2dSDavid Shaw } 44ba2d2c2dSDavid Shaw eof { 45ba2d2c2dSDavid Shaw puts stderr "**ERROR** End of data waiting for password prompt." 46ba2d2c2dSDavid Shaw exit 1 47ba2d2c2dSDavid Shaw } 48ba2d2c2dSDavid Shaw } 49ba2d2c2dSDavid Shaw } 50ba2d2c2dSDavid Shaw 51ba2d2c2dSDavid Shaw set timeout 3 52ba2d2c2dSDavid Shaw expect { 53ba2d2c2dSDavid Shaw "Permission denied, please try again." { 54ba2d2c2dSDavid Shaw puts "" 55ba2d2c2dSDavid Shaw puts "**ERROR** Incorrect userid or password provided to this program." 56ba2d2c2dSDavid Shaw exit 1 57ba2d2c2dSDavid Shaw } 58ba2d2c2dSDavid Shaw } 59ba2d2c2dSDavid Shaw 60ba2d2c2dSDavid Shaw set timeout -1 61ba2d2c2dSDavid Shaw 62ba2d2c2dSDavid Shaw # We don't ever expect to see this string. This will keep this program 63ba2d2c2dSDavid Shaw # running indefinitely. 64*aecfce00SMichael Walsh set never_string "When in the course of human events, it becomes :" 65*aecfce00SMichael Walsh if { [ catch {expect { "${never_string}" {send "whatever\r"} }} result ] } { 66*aecfce00SMichael Walsh set child_died {expect:[ ]spawn[ ]id[ ]exp4[ ]not[ ]open} 67*aecfce00SMichael Walsh if { [regexp -expanded ${child_died} $result] } { 68*aecfce00SMichael Walsh # The child died. This is not necessarily an error (for example, the 69*aecfce00SMichael Walsh # user may have included a command string to run on the target). 70*aecfce00SMichael Walsh exit 0 71*aecfce00SMichael Walsh } else { 72*aecfce00SMichael Walsh puts $result 73*aecfce00SMichael Walsh exit 1 74*aecfce00SMichael Walsh } 75ba2d2c2dSDavid Shaw } 76ba2d2c2dSDavid Shaw 77ba2d2c2dSDavid Shaw exit 0 78ba2d2c2dSDavid Shaw 79ba2d2c2dSDavid Shaw############################################################################### 80ba2d2c2dSDavid Shaw 81