使用 expect 远程登录时,如何通过 expect_out 将输出词作为下一个命令输入变量进行 grep?

When telneting with expect, how to grep an output word as the next command input variable via expect_out ?

我正在尝试使用 bash 脚本中的 expect 命令从 telnet 连接设备。我想做的事情如下:

#!/usr/bin/expect -f

    telnet_setup()
 {      sleep 10
        expect <<EOF
        spawn telnet 
        expect "Password:"
        send "mypass\n"
        expect "#"
        send "my command to be run\n"
        expect "Last number to be captured:" 
#Here the whole sentence to be captured something like:
#Last number to be captured: 213124
        set mynumber expect_out(buffer,0) | awk '{print }' 
        send "my next command with number $mynumber\"
        expect "Correct number $mynumber is achieved!"
        send "exit\n"
EOF

}

To use the function, I use as follows:

telnet_setup 1.1.1.1 > file.txt

我的目标是将此函数与 $mynumber 结合使用,以便在下一个命令中使用。你能建议如何提供这个吗?

您在将期望代码与 shell 代码分开时遇到了一些问题。要捕获数字,需要一个带有捕获括号的正则表达式

    send "my command to be run\r"
    expect -re {Last number to be captured:\s+(\d+)} 
    set mynumber $expect_out(1,string)