为什么'\ n'('\ r')在我的期望脚本中不起作用

时间:2019-02-13 02:47:37

标签: expect

我想在执行y+enter时输入copy tftp:something来回答问题。 该脚本将发送y,但是\n不起作用。它将停留在(y/n)y并保持在那里,而无需退出或执行其他操作。我尝试过\r,结果是一样的。有人知道原因吗?

#!bin/bash

expect -c "
set timeout -1
spawn telnet x.x.x.x
expect \"username\"
send \"user\n\"
expect \"password\"
send \"pw\n\"
expect \"model\"
send \"copy tftp:something\n\"
expect \"(y/n)\"
send \"y\n\"
expect eof
"
exit 0

1 个答案:

答案 0 :(得分:0)

我更喜欢使用\r“按Enter”。

第二,需要整个bash脚本,因此请删除外部bash层。

#!/usr/bin/expect -f
set timeout -1
spawn telnet x.x.x.x
expect "username"
send "user\r"
expect "password"
send "pw\r"
expect "model"
send "copy tftp:something\r"
expect "(y/n)"
send "y\r"
expect eof

如果您在bash部分中有更多逻辑,为避免引用地狱,请使用heredoc:

#!/bin/bash
expect <<'END_EXPECT'
    set timeout -1
    spawn telnet x.x.x.x
    expect "username"
    send "user\r"
    expect "password"
    send "pw\r"
    expect "model"
    send "copy tftp:something\r"
    expect "(y/n)"
    send "y\r"
    expect eof
END_EXPECT
exit 0