| 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 { | 
| Michael Walsh | 823a7ff | 2017-10-20 10:36:07 -0500 | [diff] [blame] | 29 | -re "Offending RSA key in (.*?)\[\r\n\]" { | 
|  | 30 | # We have been informed by ssh that we have a bad key. | 
| Gunnar Mills | 28e403b | 2017-10-25 16:16:38 -0500 | [diff] [blame^] | 31 | # Retrieve the file path and line number from the ssh output. | 
| Michael Walsh | 823a7ff | 2017-10-20 10:36:07 -0500 | [diff] [blame] | 32 | set fields [split $expect_out(1,string) ":"] | 
|  | 33 | set file_path [lindex $fields 0] | 
|  | 34 | set line_num [lindex $fields 1] | 
|  | 35 | # Use sed to delete the bad key. | 
|  | 36 | set cmd_buf "sed -i ${line_num}d ${file_path}" | 
|  | 37 | puts "Issuing: ${cmd_buf}" | 
|  | 38 | eval exec bash -c {$cmd_buf} | 
|  | 39 | # Kill the failed spawned ssh process. | 
|  | 40 | exec kill -9 [exp_pid] | 
|  | 41 | # Start a new process now that our stale key problem is fixed. | 
|  | 42 | eval spawn ssh ${ssh_parms} | 
|  | 43 | continue | 
|  | 44 | } | 
| David Shaw | ba2d2c2 | 2017-01-23 16:56:38 -0600 | [diff] [blame] | 45 | -re "assword:" { | 
|  | 46 | send "$password\r" | 
|  | 47 | break | 
|  | 48 | } | 
|  | 49 | -re "Are you sure you want to continue connecting" { | 
|  | 50 | if { $attempts > $max_attempts } { | 
|  | 51 | puts stderr "**ERROR** Exceeded $max_attempts attempts to ssh." | 
|  | 52 | exit 1 | 
|  | 53 | } | 
|  | 54 | send "yes\r" | 
|  | 55 | } | 
|  | 56 | timeout { | 
|  | 57 | puts stderr "**ERROR** Timed out waiting for password prompt." | 
|  | 58 | exit 1 | 
|  | 59 | } | 
|  | 60 | eof { | 
|  | 61 | puts stderr "**ERROR** End of data waiting for password prompt." | 
|  | 62 | exit 1 | 
|  | 63 | } | 
|  | 64 | } | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | set timeout 3 | 
|  | 68 | expect { | 
|  | 69 | "Permission denied, please try again." { | 
|  | 70 | puts "" | 
|  | 71 | puts "**ERROR** Incorrect userid or password provided to this program." | 
|  | 72 | exit 1 | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | set timeout -1 | 
|  | 77 |  | 
|  | 78 | # We don't ever expect to see this string.  This will keep this program | 
|  | 79 | # running indefinitely. | 
| Michael Walsh | aecfce0 | 2017-10-10 16:30:21 -0500 | [diff] [blame] | 80 | set never_string "When in the course of human events, it becomes :" | 
|  | 81 | if { [ catch {expect { "${never_string}" {send "whatever\r"} }} result ] } { | 
|  | 82 | set child_died {expect:[ ]spawn[ ]id[ ]exp4[ ]not[ ]open} | 
|  | 83 | if { [regexp -expanded ${child_died} $result] } { | 
|  | 84 | # The child died.  This is not necessarily an error (for example, the | 
|  | 85 | # user may have included a command string to run on the target). | 
|  | 86 | exit 0 | 
|  | 87 | } else { | 
|  | 88 | puts $result | 
|  | 89 | exit 1 | 
|  | 90 | } | 
| David Shaw | ba2d2c2 | 2017-01-23 16:56:38 -0600 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | exit 0 | 
|  | 94 |  | 
|  | 95 | ############################################################################### | 
|  | 96 |  |