期待,互动,然后再次期待 [英] expect, interact and then again expect

查看:123
本文介绍了期待,互动,然后再次期待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于相同的内容有几篇文章,但是我仍然无法使我期望的脚本正常工作.我的意图是使所有操作自动化,但保留用户输入的密码.因此,脚本包含3个部分:

There are several posts regarding the same, but i still not able to make my expect script work properly. My intention is to automate everything but leave the password enter for the user. So there are 3 parts of the script:

  1. 自动登录
  2. 允许用户交互输入密码
  3. 将控制权交还给Expect脚本以继续工作

所以我有将要生成的脚本,并且具有3个读取命令.第一个和最后一个应该由Expect填写,第二个我要输入我自己:

So i have script which will be spawned and which have 3 read commands. First and last should be filled by Expect and second one i would like to enter my self:

#!/bin/ksh
read user?User:
echo "Expect entered the username $user"
read pass?Password:
echo "User entered the password $pass"
read command?"Shell>"
echo "Expect entered the command $command"

我的期望脚本:

#!/usr/bin/expect
spawn ./some_script
expect User
send I-am-expect\r
expect Password
interact
expect Shell
send I-am-expect-again

不幸的是,我输入密码后,脚本无法继续运行,并处于交互模式:

Unfortunately after i have entered the password the script does not continue and left in the interact mode:

[root@localhost ~]# ./my-expect
spawn ./some_script
User:I-am-expect
Expect entered the username I-am-expect
Password:i am user
User entered the password i am user
Shell>

最后,当我在外壳"上输入内容并按[ENTER]时,期望退出并显示错误:

And finally when i entering something on the "Shell" and pressing [ENTER] expect exits with the error:

Expect entered the command
expect: spawn id exp4 not open
    while executing
"expect Shell"
    (file "./my-expect" line 7)
[root@localhost ~]#

我希望对此问题做出任何解释或解决.我正在使用期望版本5.45

I appriciate any explanation or resolution of this issue. I am using expect version 5.45

推荐答案

您可以自己读取(expect_user)用户密码,然后将其send读取到生成的程序中.例如:

You can read (expect_user) the user's password by yourself and then send it to the spawn'ed program. For example:

[STEP 101] # cat foo.exp
proc expect_prompt {} \
{
    global spawn_id
    expect -re {bash-[.0-9]+(#|\$)}
}

spawn ssh -t 127.0.0.1 bash --noprofile --norc
expect "password: "

stty -echo
expect_user -timeout 3600 -re "(.*)\[\r\n]"
stty echo
send "$expect_out(1,string)\r"

expect_prompt
send "exit\r"
expect eof
[STEP 102] # expect foo.exp
spawn ssh -t 127.0.0.1 bash --noprofile --norc
root@127.0.0.1's password:
bash-4.3# exit
exit
Connection to 127.0.0.1 closed.
[STEP 103] #

这篇关于期待,互动,然后再次期待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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