| David Shaw | ba2d2c2 | 2017-01-23 16:56:38 -0600 | [diff] [blame] | 1 | #!/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. | 
|  | 64 | expect { | 
|  | 65 | "When in the course of human events, it becomes :" {send "whatever\r"} | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | exit 0 | 
|  | 69 |  | 
|  | 70 | ############################################################################### | 
|  | 71 |  |