期望脚本 + 期望错过发送字符串 + 延迟问题

Eyt*_*tan 7 linux solaris ssh expect shell-scripting

我编写了 active.ksh 脚本(基于expect),以便自动登录到某些 Solaris 机器并执行主机名命令(登录到虚拟 IP 以验证哪个主机名是活动机器 - 我有两个集群 solaris 机器)

问题在于expect; 期望发送密码字符串 (pass123) 并且它错过了密码问题,它仍然等待密码。

所以实际上密码(pass123)是在密码问题之后输入的。在大多数情况下,expect 脚本工作正常,但有时会错过密码。

问题示例

 ./active.ksh
 spawn ssh 10.10.18.61
 sh: /usr/local/bin/stty: not found
 This computer system, including all related equipment, networks and network devices      (specifically including Internet access),is provided only for authorized uss
 Password:        * my remark - pass123 string was missed the Password Question        pass123
 Password: 
Run Code Online (Sandbox Code Playgroud)

剧本

  #!/bin/ksh

  VIP_ADDRESS=10.10.18.61


  expect_for_verify_which_active_machine=`cat << EOF
  set timeout -1
  spawn  ssh   $VIP_ADDRESS 
  expect {
  ")?"   { send "yes\r"  ; exp_continue  }
  Password:  {send "pass123\r"}
  }
  expect >  {send "hostname\r"}
  expect >    {send exit\r}
  expect eof
  EOF`


  expect -c  "$expect_for_verify_which_active_machine"
Run Code Online (Sandbox Code Playgroud)

正确结果的例子

  ./active.ksh 
  [Friday, February 24, 2012  2:32:06 PM IST] INFO Verify which is active SMU machine 
  spawn ssh 10.10.18.61
  sh: /usr/local/bin/stty: not found
  This computer system, including all related equipment, networks and network devices       (specifically including Internet access),is provided only for authorized uss
  yes
  Password: 
  Last login: Fri Feb 24 14:32:06 2012 from smu1a
  This computer system, including all related equipment, networks and network devices       (specifically including Internet access),is provided only for authorized uss
  solaris1:/ ROOT > hostname
  solaris1
  solaris1:/ ROOT > exit

  logout
  Connection to 10.10.18.61  closed.
Run Code Online (Sandbox Code Playgroud)

Xar*_*ses 7

如果您在登录期间监控您的字符串,您将希望避免使用“密码:”,您会发现它并不总是大写。

改变你的期望-re "(.*)assword:""assword:"趋向于更有效地赶上这条线。

如果你发现时间仍然太快,你可以把 sleep 1; 在您发送之前

这是我用于期望的

expect {
    "(yes/no)?" { send "yes\n" }
    "passphrase" { send "\r" }
    -re "(.*)assword:"  { sleep 1; send -- "password\r" }
    -re $prompt { return }
    timeout     { puts "un-able to login: timeout\n"; return }
    eof         { puts "Closed\n" ; return }
}
Run Code Online (Sandbox Code Playgroud)