gpt4 book ai didi

linux - 期待,互动,然后再次期待

转载 作者:IT王子 更新时间:2023-10-29 01:21:11 27 4
gpt4 key购买 nike

有几篇关于相同内容的帖子,但我仍然无法使我的 expect 脚本正常工作。我的意图是将所有内容自动化,但为用户保留输入密码。所以脚本有 3 个部分:

  1. 自动登录
  2. 让用户输入密码
  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 ~]#

我感谢对此问题的任何解释或解决方案。我正在使用 expect 版本 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] #

关于linux - 期待,互动,然后再次期待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33854568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com