xref: /openbmc/openbmc-test-automation/bin/ssh_pw (revision ba2d2c2dee762457e7d48b88a5a6ceeae3e739fa)
1*ba2d2c2dSDavid Shaw#!/usr/bin/expect --
2*ba2d2c2dSDavid Shaw
3*ba2d2c2dSDavid Shaw# ssh using the parms provided by the caller.  The benefit provided by this
4*ba2d2c2dSDavid Shaw# program is that it will enter the password for you (i.e. non-interactively).
5*ba2d2c2dSDavid Shaw
6*ba2d2c2dSDavid Shaw# Description of arguments:
7*ba2d2c2dSDavid Shaw# Arg0: The password.
8*ba2d2c2dSDavid Shaw# Arg1: The ssh parm string.  This is the totality of ssh parms you wish to
9*ba2d2c2dSDavid Shaw#       specify (e.g. userid, host, etc.).
10*ba2d2c2dSDavid Shaw
11*ba2d2c2dSDavid Shaw
12*ba2d2c2dSDavid Shaw###############################################################################
13*ba2d2c2dSDavid Shaw# Main
14*ba2d2c2dSDavid Shaw
15*ba2d2c2dSDavid Shaw  # Get arguments.
16*ba2d2c2dSDavid Shaw  set password [lindex $argv 0]
17*ba2d2c2dSDavid Shaw  set ssh_parms [lreplace $argv 0 0]
18*ba2d2c2dSDavid Shaw
19*ba2d2c2dSDavid Shaw  eval spawn ssh ${ssh_parms}
20*ba2d2c2dSDavid Shaw
21*ba2d2c2dSDavid Shaw  set timeout 30
22*ba2d2c2dSDavid Shaw
23*ba2d2c2dSDavid Shaw  set max_attempts 3
24*ba2d2c2dSDavid Shaw
25*ba2d2c2dSDavid Shaw  set attempts 0
26*ba2d2c2dSDavid Shaw  while { 1 } {
27*ba2d2c2dSDavid Shaw    incr attempts 1
28*ba2d2c2dSDavid Shaw    expect {
29*ba2d2c2dSDavid Shaw      -re "assword:" {
30*ba2d2c2dSDavid Shaw        send "$password\r"
31*ba2d2c2dSDavid Shaw        break
32*ba2d2c2dSDavid Shaw      }
33*ba2d2c2dSDavid Shaw      -re "Are you sure you want to continue connecting" {
34*ba2d2c2dSDavid Shaw        if { $attempts > $max_attempts } {
35*ba2d2c2dSDavid Shaw          puts stderr "**ERROR** Exceeded $max_attempts attempts to ssh."
36*ba2d2c2dSDavid Shaw          exit 1
37*ba2d2c2dSDavid Shaw        }
38*ba2d2c2dSDavid Shaw        send "yes\r"
39*ba2d2c2dSDavid Shaw      }
40*ba2d2c2dSDavid Shaw      timeout {
41*ba2d2c2dSDavid Shaw        puts stderr "**ERROR** Timed out waiting for password prompt."
42*ba2d2c2dSDavid Shaw        exit 1
43*ba2d2c2dSDavid Shaw      }
44*ba2d2c2dSDavid Shaw      eof {
45*ba2d2c2dSDavid Shaw        puts stderr "**ERROR** End of data waiting for password prompt."
46*ba2d2c2dSDavid Shaw        exit 1
47*ba2d2c2dSDavid Shaw      }
48*ba2d2c2dSDavid Shaw    }
49*ba2d2c2dSDavid Shaw  }
50*ba2d2c2dSDavid Shaw
51*ba2d2c2dSDavid Shaw  set timeout 3
52*ba2d2c2dSDavid Shaw  expect {
53*ba2d2c2dSDavid Shaw    "Permission denied, please try again." {
54*ba2d2c2dSDavid Shaw      puts ""
55*ba2d2c2dSDavid Shaw      puts "**ERROR** Incorrect userid or password provided to this program."
56*ba2d2c2dSDavid Shaw      exit 1
57*ba2d2c2dSDavid Shaw    }
58*ba2d2c2dSDavid Shaw  }
59*ba2d2c2dSDavid Shaw
60*ba2d2c2dSDavid Shaw  set timeout -1
61*ba2d2c2dSDavid Shaw
62*ba2d2c2dSDavid Shaw  # We don't ever expect to see this string.  This will keep this program
63*ba2d2c2dSDavid Shaw  # running indefinitely.
64*ba2d2c2dSDavid Shaw  expect {
65*ba2d2c2dSDavid Shaw    "When in the course of human events, it becomes :" {send "whatever\r"}
66*ba2d2c2dSDavid Shaw  }
67*ba2d2c2dSDavid Shaw
68*ba2d2c2dSDavid Shaw  exit 0
69*ba2d2c2dSDavid Shaw
70*ba2d2c2dSDavid Shaw###############################################################################
71*ba2d2c2dSDavid Shaw
72