使用bash进行SSH登录并期待

时间:2012-10-28 05:28:06

标签: bash expect

我必须每天都进入众多设备,一遍又一遍...... 我可以使用bash& amp;期待这个脚本:

#!/usr/bin/expect
spawn  ssh hostname -l username
expect "password:"
send "secret\n"
interact

大多数设备都需要“root”作为登录名,因此上述脚本适用于此。我想修改上面的脚本尝试登录一次,如果失败,切换到另一个用户名并提示我输入密码。这可能吗?

我知道密钥,以及脚本中密码的不良做法,在我的情况下并不重要。

1 个答案:

答案 0 :(得分:0)

man page有很多很好的例子。

expect {
  "string that indicates success" {
  }
  "string that indicates failure" {
    close
    stty -echo
    send_user "Enter password: "
    expect_user -re "(.*)\n"
    send_user "\n"
    send "$expect_out(1,string)\r"
    stty echo
  }
}
interact