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