在预期收益做出决定 [英] Making decisions on expect return

查看:170
本文介绍了在预期收益做出决定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个expect脚本将发送基于不同的密码字符串的期望


  • 条件A:如果思科的设备没有使用用户名已经设置那么第一个提示将只显示密码: - 那么它应该使用passwordA(没有用户名)


  • 条件B:如果已经设置了用户名,然后迅速将用户名:其次是密码: - 那么它应该使用的用户名和PasswordB


 #!/斌/庆典
#声明主机变量作为输入变量
主机= $ 1
 #启动expect脚本
(预计-c
设定的超时值20
#先从输入变量的会话和主机名的其余部分
产卵的telnet主机$
设置超时3
如果{预计\\密码:\\} {
发送\\PasswordA \\}
ELSEIF {预计\\用户名:\\}
发送\\UsersName \\ r \\}
预计\\密码:\\
LOG_USER 0
发送\\PasswordB \\ r \\
LOG_USER 1
预计\\* GT; \\
#发送\\秀版\\ r \\
#组结果$ expect_out(缓冲)
#expect \\密码:\\
#send \\SomeEnablePassword \\ r \\
#允许我们与交换机交互自己
#停止期望脚本一次telnet会话被关闭
发送\\退出\\ r \\
预计EOF


解决方案

您就错了。 :)

期望语句不看看,看看什么是要先等待,直到它看到你问什么(和时间,如果它不能及时到达),然后运行您传递给它的命令。我想你可以使用它像你想的方式,但它不是很好。

期望可采取的替代品清单去找,像一个C 开关语句,或壳情况语句,这就是你需要在这里。

我已经的没有测试的这一点,但你想应该是这个样子的:

 期望{
  -ex用户名:{
    发送UsersName \\ R
    预计-ex密码:{发送PasswordB \\ r}
  }
  -ex密码:{发送PasswordA \\ r}
}

在的话,期待会寻找任何用户名和密码:( -ex 表示精确匹配,没有正则表达式),以先到者为准,并运行与之相关联的命令。


在回应的意见,我会尝试这样的第二个密码(假设登录成功给人以#提示符):

 期望{
  -ex用户名:{
    发送UsersName \\ R
    预计-ex密码:{发送PasswordB \\ r}
  }
  -ex密码:{
    发送PasswordA1 \\ R
    预计{
      -ex密码:{发送PasswordA2 \\ r}
      -ex#{}
    }
  }
}

您的 的能做到这一点,而不寻找提示,但你必须依靠第二密码:预计时间出,这是不理想的。

I am trying to create an expect script which will send a different password string based on the "expect"

  • Condition A: If a cisco device has not been setup with a username then the first prompt will simply be "Password:" - then it should use passwordA (no username)

  • Condition B: If it has been setup with a username then the prompt will be "Username:" followed by "Password:" - then it should use Username and PasswordB

#!/bin/bash
# Declare host variable as the input variable
host=$1 
 # Start the expect script
(expect -c "
set timeout 20
# Start the session with the input variable and the rest of the hostname
spawn telnet $host
set timeout 3 
if {expect \"Password:\"} {
send \"PasswordA\"}
elseif { expect \"Username:\"}
send \"UsersName\r\"}
expect \"Password:\"
log_user 0 
send \"PasswordB\r\"
log_user 1
expect \"*>\"
# send \"show version\r\"
# set results $expect_out(buffer) 
#expect \"Password:\"
#send \"SomeEnablePassword\r\"
# Allow us to interact with the switch ourselves
# stop the expect script once the telnet session is closed
send \"quit\r\"
expect eof
")

解决方案

You're doing it wrong. :)

The expect statement doesn't look to see what comes first, it waits until it sees what you ask for (and times out if it doesn't arrive in time), and then runs a command you pass to it. I think you can use it something like the way you're trying to, but it's not good.

expect can take a list of alternatives to look for, like a C switch statement, or a shell case statement, and that's what you need here.

I've not tested this, but what you want should look something like this:

expect {
  -ex "Username:" {
    send "UsersName\r"
    expect -ex "Password:" {send "PasswordB\r"}
  }
  -ex "Password:" {send "PasswordA\r"}
}

In words, expect will look for either "Username:" or "Password:" (-ex means exact match, no regexp), whichever comes first, and run the command associated with it.


In response to the comments, I would try a second password like this (assuming that a successful login gives a '#' prompt):

expect {
  -ex "Username:" {
    send "UsersName\r"
    expect -ex "Password:" {send "PasswordB\r"}
  }
  -ex "Password:" {
    send "PasswordA1\r"
    expect {
      -ex "Password:" {send "PasswordA2\r"}
      -ex "#" {}
    }
  }
}

You could do it without looking for the # prompt, but you'd have to rely on the second Password: expect timing-out, which isn't ideal.

这篇关于在预期收益做出决定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆