xref: /openbmc/openbmc-test-automation/bin/ssh_pw (revision b3705fc75414114f20f994bd0f04d65d0c6ec80f)
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# Main
13ba2d2c2dSDavid Shaw
14ba2d2c2dSDavid Shaw  # Get arguments.
15ba2d2c2dSDavid Shaw  set password [lindex $argv 0]
16ba2d2c2dSDavid Shaw  set ssh_parms [lreplace $argv 0 0]
17ba2d2c2dSDavid Shaw
18ba2d2c2dSDavid Shaw  eval spawn ssh ${ssh_parms}
19ba2d2c2dSDavid Shaw
20ba2d2c2dSDavid Shaw  set timeout 30
21ba2d2c2dSDavid Shaw
22ba2d2c2dSDavid Shaw  set max_attempts 3
23ba2d2c2dSDavid Shaw
24ba2d2c2dSDavid Shaw  set attempts 0
25ba2d2c2dSDavid Shaw  while { 1 } {
26ba2d2c2dSDavid Shaw    incr attempts 1
27ba2d2c2dSDavid Shaw    expect {
28823a7ffcSMichael Walsh      -re "Offending RSA key in (.*?)\[\r\n\]" {
29823a7ffcSMichael Walsh        # We have been informed by ssh that we have a bad key.
30*28e403b8SGunnar Mills        # Retrieve the file path and line number from the ssh output.
31823a7ffcSMichael Walsh        set fields [split $expect_out(1,string) ":"]
32823a7ffcSMichael Walsh        set file_path [lindex $fields 0]
33823a7ffcSMichael Walsh        set line_num [lindex $fields 1]
34823a7ffcSMichael Walsh        # Use sed to delete the bad key.
35823a7ffcSMichael Walsh        set cmd_buf "sed -i ${line_num}d ${file_path}"
36823a7ffcSMichael Walsh        puts "Issuing: ${cmd_buf}"
37823a7ffcSMichael Walsh        eval exec bash -c {$cmd_buf}
38823a7ffcSMichael Walsh        # Kill the failed spawned ssh process.
39823a7ffcSMichael Walsh        exec kill -9 [exp_pid]
40823a7ffcSMichael Walsh        # Start a new process now that our stale key problem is fixed.
41823a7ffcSMichael Walsh        eval spawn ssh ${ssh_parms}
42823a7ffcSMichael Walsh        continue
43823a7ffcSMichael Walsh      }
44ba2d2c2dSDavid Shaw      -re "assword:" {
45ba2d2c2dSDavid Shaw        send "$password\r"
46ba2d2c2dSDavid Shaw        break
47ba2d2c2dSDavid Shaw      }
48ba2d2c2dSDavid Shaw      -re "Are you sure you want to continue connecting" {
49ba2d2c2dSDavid Shaw        if { $attempts > $max_attempts } {
50ba2d2c2dSDavid Shaw          puts stderr "**ERROR** Exceeded $max_attempts attempts to ssh."
51ba2d2c2dSDavid Shaw          exit 1
52ba2d2c2dSDavid Shaw        }
53ba2d2c2dSDavid Shaw        send "yes\r"
54ba2d2c2dSDavid Shaw      }
55ba2d2c2dSDavid Shaw      timeout {
56ba2d2c2dSDavid Shaw        puts stderr "**ERROR** Timed out waiting for password prompt."
57ba2d2c2dSDavid Shaw        exit 1
58ba2d2c2dSDavid Shaw      }
59ba2d2c2dSDavid Shaw      eof {
60ba2d2c2dSDavid Shaw        puts stderr "**ERROR** End of data waiting for password prompt."
61ba2d2c2dSDavid Shaw        exit 1
62ba2d2c2dSDavid Shaw      }
63ba2d2c2dSDavid Shaw    }
64ba2d2c2dSDavid Shaw  }
65ba2d2c2dSDavid Shaw
66ba2d2c2dSDavid Shaw  set timeout 3
67ba2d2c2dSDavid Shaw  expect {
68ba2d2c2dSDavid Shaw    "Permission denied, please try again." {
69ba2d2c2dSDavid Shaw      puts ""
70ba2d2c2dSDavid Shaw      puts "**ERROR** Incorrect userid or password provided to this program."
71ba2d2c2dSDavid Shaw      exit 1
72ba2d2c2dSDavid Shaw    }
73ba2d2c2dSDavid Shaw  }
74ba2d2c2dSDavid Shaw
75ba2d2c2dSDavid Shaw  set timeout -1
76ba2d2c2dSDavid Shaw
77ba2d2c2dSDavid Shaw  # We don't ever expect to see this string.  This will keep this program
78ba2d2c2dSDavid Shaw  # running indefinitely.
79aecfce00SMichael Walsh  set never_string "When in the course of human events, it becomes :"
80aecfce00SMichael Walsh  if { [ catch {expect { "${never_string}" {send "whatever\r"} }} result ] } {
81aecfce00SMichael Walsh    set child_died {expect:[ ]spawn[ ]id[ ]exp4[ ]not[ ]open}
82aecfce00SMichael Walsh    if { [regexp -expanded ${child_died} $result] } {
83aecfce00SMichael Walsh      # The child died.  This is not necessarily an error (for example, the
84aecfce00SMichael Walsh      # user may have included a command string to run on the target).
85aecfce00SMichael Walsh      exit 0
86aecfce00SMichael Walsh    } else {
87aecfce00SMichael Walsh      puts $result
88aecfce00SMichael Walsh      exit 1
89aecfce00SMichael Walsh    }
90ba2d2c2dSDavid Shaw  }
91ba2d2c2dSDavid Shaw
92ba2d2c2dSDavid Shaw  exit 0
93ba2d2c2dSDavid Shaw
94ba2d2c2dSDavid Shaw
95