【问题标题】:expect script + fit expect in case password not needed期望脚本+适合期望以防不需要密码
【发布时间】:2012-02-27 10:19:54
【问题描述】:

当 Linux 机器的 $IP 在登录后询问密码时,以下期望脚本可以正常工作

但是在某些情况下,一些Linux机器不需要ssh密码(我们可以不用密码登录),

所以我需要更改我的期望脚本以支持没有密码的机器

请建议如何适应我的期望脚本,以支持有密码的机器和没有密码的机器

 * target of the following expect script is to check the hostname on remote Linux machine


 expect_test=`cat << EOF
 set timeout -1
 spawn  ssh $IP  hostname
       expect {
                 ")?"   { send "yes\r"  ; exp_continue  }
                 word:  {send "pass123\r"     }
      }
 expect eof
 EOF`



 expect -c "$expect_test"

远程机器上的 exe 示例(带密码)(远程机器的名称 - Linux1_machine)

  IP=10.17.18.6

  expect -c "$expect_test"

  spawn ssh 10.17.18.6 hostname
  sh: /usr/local/bin/stty: not found
  This computer system, including all related equipment, networks and network devices     (specifically including Internet access),is pros
  yes
  Password: 
  Linux1_machine

在不需要密码登录的机器上执行expect脚本的例子

 IP=10.10.92.26


 expect -c "$expect_test"


 spawn ssh 10.10.92.26 hostname
 sh: /usr/local/bin/stty: not found
 Linux15_machine
 expect: spawn id exp5 not open
 while executing
 "expect eof"

【问题讨论】:

标签: linux bash shell ksh expect


【解决方案1】:

你有几个选择。

如果你知道你不需要登录的服务器会发生什么(例如会话直接进入提示符),那么添加一个期望语句:

expect {
                     ")?"   { send "yes\r"  ; exp_continue  }
                     word:  {send "pass123\r"     }
                     "prompt"    { do something }
          }

如果您没有一致的提示,您可以try using a Regular Expression 检测各种提示:

set prompt “(%|#|\\$) $” ;# default prompt
expect {
      ")?"   { send "yes\r"  ; exp_continue  }
      word:  {send "pass123\r"     }
      -re $prompt { do something }
}

如果您根本不知道自己会得到什么(似乎不太可能),那么您也可以在 Expect 超时时添加一个操作。

expect {
                 ")?"   { send "yes\r"  ; exp_continue  }
                 word:  {send "pass123\r"     }
                 timeout { do something }
      }

这将允许 expect 在没有收到其他预期行时移动。

请参阅http://wiki.tcl.tk/11583,了解您正在尝试做的具体示例。此外,Getting Started with Expect chapter from the Oreilly book 值得一读。

【讨论】:

  • hi dunxd 在你的三个案例中我仍然得到 --> 期望:spawn id exp5 not open while execution "expect eof" ,
猜你喜欢
  • 2015-11-28
  • 2017-06-20
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多