2016-02-27 42 views
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的此功能在下一個命令中使用。你能建議如何提供這個?

+1

您的shebang是期望的。爲什麼heredoc? –

回答

2

將預期代碼與shell代碼分離時遇到了一些麻煩。爲了捕捉數字,期望一個帶有捕捉括號的正則表達式

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