期待,互动,然后再次期待
expect, interact and then again expect
有几篇关于相同内容的帖子,但我仍然无法使我的 expect 脚本正常工作。我的意图是将所有内容自动化,但为用户保留输入密码。所以脚本有 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
interact
应该为退出标准提供适当的条件。
以下脚本将执行 shell
中的用户命令
exeCmds.sh
#!/bin/bash
read -p "User: " user
echo "Expect entered the username $user"
read -p "Password: " pass
echo "User entered the password $pass"
while :
do
# Simply executing the user inputs in the shell
read -p "Shell> " command
$command
done
automateCmdsExec.exp
#!/usr/bin/expect
spawn ./exeCmds.sh
expect User
send dinesh\r
expect Password
send welcome!2E\r
expect Shell>
puts "\nUser can interact now..."
puts -nonewline "Type 'proceed' for the script to take over\nShell> "
while 1 {
interact "proceed" {puts "User interaction completed.";break}
}
puts "Script take over the control now.."
# Now, sending 'whoami' command from script to shell
send "whoami\r"
expect Shell>
# Your further code here...
脚本automateCmdsExec.exp
将解决bash 脚本的登录需求,当出现提示时,它会将控制权移交给用户。
我们应该为 interact
定义一个退出标准,我已经为它使用了 proceed
这个词。 (您可以根据需要更改它)。
曾经 interact
匹配单词 proceed
。它会将 return 控制权交还给 expect 脚本。
出于演示目的,我保留了一对 send
-expect
命令。
即
send "whoami\r"
expect Shell>
您可以将进一步的代码保留在interact
之下,这样它就可以通过脚本执行。
您可以自己读取(expect_user
) 用户的密码,然后send
将其发送给生成的程序。例如:
[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] #
有几篇关于相同内容的帖子,但我仍然无法使我的 expect 脚本正常工作。我的意图是将所有内容自动化,但为用户保留输入密码。所以脚本有 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
interact
应该为退出标准提供适当的条件。
以下脚本将执行 shell
中的用户命令exeCmds.sh
#!/bin/bash
read -p "User: " user
echo "Expect entered the username $user"
read -p "Password: " pass
echo "User entered the password $pass"
while :
do
# Simply executing the user inputs in the shell
read -p "Shell> " command
$command
done
automateCmdsExec.exp
#!/usr/bin/expect
spawn ./exeCmds.sh
expect User
send dinesh\r
expect Password
send welcome!2E\r
expect Shell>
puts "\nUser can interact now..."
puts -nonewline "Type 'proceed' for the script to take over\nShell> "
while 1 {
interact "proceed" {puts "User interaction completed.";break}
}
puts "Script take over the control now.."
# Now, sending 'whoami' command from script to shell
send "whoami\r"
expect Shell>
# Your further code here...
脚本automateCmdsExec.exp
将解决bash 脚本的登录需求,当出现提示时,它会将控制权移交给用户。
我们应该为 interact
定义一个退出标准,我已经为它使用了 proceed
这个词。 (您可以根据需要更改它)。
曾经 interact
匹配单词 proceed
。它会将 return 控制权交还给 expect 脚本。
出于演示目的,我保留了一对 send
-expect
命令。
即
send "whoami\r"
expect Shell>
您可以将进一步的代码保留在interact
之下,这样它就可以通过脚本执行。
您可以自己读取(expect_user
) 用户的密码,然后send
将其发送给生成的程序。例如:
[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] #