我正在尝试将来自telnet的设备与bash脚本中的expect命令连接起来。我想做的事情如下:
#!/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
}
要使用该功能,我使用如下:
telnet_setup 1.1.1.1 > file.txt
我的目标是在$ mynumber中使用此函数在下一个命令中使用。你能否建议如何提供这个?
答案 0 :(得分:2)
您在将期望代码与shell代码分开时遇到了一些麻烦。要捕获数字,需要使用捕获括号的正则表达式
send "my command to be run\r"
expect -re {Last number to be captured:\s+(\d+)}
set mynumber $expect_out(1,string)