ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

linux-期望,进行交互,然后再次期望

2019-11-19 11:51:12  阅读:224  来源: 互联网

标签:interaction expect passwords login linux


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

>自动登录
>给用户交互输入密码
>将控制权交还给Expect脚本以继续工作

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

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

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

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

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

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

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

解决方法:

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

[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] #

标签:interaction,expect,passwords,login,linux
来源: https://codeday.me/bug/20191119/2035866.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有