0
我想在bash腳本中用expect命令連接telnet的設備。我想這樣做的東西如下:當用期望進行通信時,如何通過expect_out將輸出字grep作爲下一個命令輸入變量?
#!/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的此功能在下一個命令中使用。你能建議如何提供這個?
您的shebang是期望的。爲什麼heredoc? –