简体   繁体   English

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

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

I am trying to connect a device from telnet with expect command in bash script. 我正在尝试使用bash脚本中的Expect命令从telnet连接设备。 What I would like to do is something as follows: 我想做的事情如下:

#!/usr/bin/expect -f

    telnet_setup()
 {      sleep 10
        expect <<EOF
        spawn telnet $1
        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 $6}' 
        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

My goal is to use this function with $mynumber to be used in the next commands. 我的目标是将此功能与$ mynumber一起使用,以在下一个命令中使用。 Can you please suggest how to provide this? 您能建议如何提供吗?

You're having some trouble separating expect code from shell code. 您在将期望代码与外壳代码分离时遇到了一些麻烦。 To capture the number, expect a regular expression with capturing parentheses 要捕获数字,请期待带有捕获括号的正则表达式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM