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

前端之家收集整理的这篇文章主要介绍了linux – 期望脚本期望错过发送字符串延迟问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我编写了active.ksh脚本(基于expect),以便自动登录某些Solaris机器并执行hostname命令(登录虚拟IP以验证哪个主机名是活动机器 – 我有两个集群solaris机器)

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

实际上密码后输入了密码(pass123)
题.在大多数情况下,expect脚本工作正常,但有时它会丢失密码.

问题的例子

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

剧本

  1. #!/bin/ksh
  2.  
  3. VIP_ADDRESS=10.10.18.61
  4.  
  5.  
  6. expect_for_verify_which_active_machine=`cat << EOF
  7. set timeout -1
  8. spawn ssh $VIP_ADDRESS
  9. expect {
  10. ")?" { send "yes\r" ; exp_continue }
  11. Password: {send "pass123\r"}
  12. }
  13. expect > {send "hostname\r"}
  14. expect > {send exit\r}
  15. expect eof
  16. EOF`
  17.  
  18.  
  19. expect -c "$expect_for_verify_which_active_machine"

正确结果的例子

  1. ./active.ksh
  2. [Friday,February 24,2012 2:32:06 PM IST] INFO Verify which is active SMU machine
  3. spawn ssh 10.10.18.61
  4. sh: /usr/local/bin/stty: not found
  5. This computer system,networks and network devices (specifically including Internet access),is provided only for authorized uss
  6. yes
  7. Password:
  8. Last login: Fri Feb 24 14:32:06 2012 from smu1a
  9. This computer system,is provided only for authorized uss
  10. solaris1:/ ROOT > hostname
  11. solaris1
  12. solaris1:/ ROOT > exit
  13.  
  14. logout
  15. Connection to 10.10.18.61 closed.

解决方法

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

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

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

这就是我用来期待的东西

  1. expect {
  2. "(yes/no)?" { send "yes\n" }
  3. "passphrase" { send "\r" }
  4. -re "(.*)assword:" { sleep 1; send -- "password\r" }
  5. -re $prompt { return }
  6. timeout { puts "un-able to login: timeout\n"; return }
  7. eof { puts "Closed\n" ; return }
  8. }

猜你在找的Linux相关文章