首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Expect脚本中使用两个交互

问在Expect脚本中使用两个交互
EN

Stack Overflow用户
提问于 2014-02-16 04:38:35
回答 1查看 11K关注 0票数 7

我正在尝试编写一个脚本,该脚本通过SSH连接到Linux机器,并允许从那里交互控制Cisco设备;在完成对设备的控制之后,我还想退出shell。

我有SSH密钥,不需要密码即可连接。下面代码中的go是一个通过SSH/Telnet连接到目标设备的Bash脚本。

到目前为止,我所做的是:

代码语言:javascript
复制
#!/usr/bin/expect
set arg1 [lindex $argv 0]
spawn ssh -p 24 my_username@my_linux.domain.com
expect "#"
send "go $arg1 \n"
expect "sername:"
send "my_username\n"
expect "assword:"
send "my_password\n"
interact
expect "root@my_linux:~#"
send "logout\n"
expect "my_username@my_linux:~ $"
send "logout\n"
interact

当我退出shell时,我得到的错误是:

代码语言:javascript
复制
Connection to my_linux.domain.com closed.
expect: spawn id exp4 not open
    while executing
"expect "root@my_linux:~#""
    (file "./aaa" line 11)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-16 06:44:42

我已经解决了这个问题:

代码语言:javascript
复制
#!/usr/bin/expect
set timeout -1

set arg1 [lindex $argv 0]
spawn ssh -p 24 my_username@my_linux.domain.com
expect "#"
send "go $arg1 \n"
expect "sername:"
send "my_username\n"
expect "assword:"
send "my_password\n"
expect "#"
interact timeout 5 return
send "\n"
expect "root@my_linux:~#"
send "exit\n exit\n"
interact

说明:我添加了几行:

代码语言:javascript
复制
# This prevents commands from timing out (default timeout is 10 seconds).
set timeout -1

# When I type something, the timeout is ignored, but when I'm not typing,
# it waits 5 seconds and then continues.
interact timeout 5 return
send "\n"
expect "root@my_linux:~#"
send "exit\n exit\n" 
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21803337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档