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

我编写了active.ksh脚本(基于expect),以便自动登录某些Solaris机器并执行hostname命令(登录虚拟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:

剧本

#!/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"

正确结果的例子

./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,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,is provided only for authorized uss
  solaris1:/ ROOT > hostname
  solaris1
  solaris1:/ ROOT > exit

  logout
  Connection to 10.10.18.61  closed.

解决方法@H_502_21@
如果您在登录期间监视字符串,您将希望避免使用“密码:”,您会发现它并不总是大写.

将你的期望改为-re“(.*)assword:”或“assword:”往往会更有效地抓住这条线.

如果你发现时间仍然太快,你可以睡一觉;在你发送之前

这就是我用来期待的东西

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 }
}

相关文章

文件查找(find) 1 find 简单的说,就是实时查找指定的内容或条件。特点:最新、最快、最准确。 用法:...
非交互式添加分区 方法一 添加/deb/sdb 下的分区,其实位置为1到1000M,第二个分区位置为1001至3000M,...
编译安装httpd 1 去官网下载源码包 为避免非法软件,一定要去官网下载http://www.apache.org httpd-2.4...
gdisk用法 gdisk - InteractiveGUIDpartitiontable (GPT) manipulator GPTfdisk (akagdisk) isatext-mo...
1 一定用快捷键 这里简单的说下几个常用的快捷按键。 1.1 移动光标快捷键 Crtl + a 光标回到命令行...
bash shell中测试命令 test命令提供了if-than语句中测试不同条件的途径。如果test命令中列出的条件成立...