【问题标题】:Using two interact in a Expect script在 Expect 脚本中使用两个交互
【发布时间】:2014-02-15 20:38:35
【问题描述】:

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

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

到目前为止我所做的是:

#!/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时出现的错误是:

Connection to my_linux.domain.com closed.
expect: spawn id exp4 not open
    while executing
"expect "root@my_linux:~#""
    (file "./aaa" line 11)

【问题讨论】:

  • Command go 是一个 bash 脚本,用于创建与设备的连接 (ssh/telnet)。

标签: linux bash expect


【解决方案1】:

我已经解决了这个问题:

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

说明:我加了几行:

# 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" 

【讨论】:

    猜你喜欢
    • 2019-06-29
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多