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