2017-02-28 62 views
0

我有這樣保存expect_out的特定部分的變量

#!/opt/tools/unsupported/expect-5.39/bin/expect 

spawn ssh -l user ip 
expect_after eof {exit 0} 
set timeout 10 
log_file /report.txt 

expect "Password:" { send "pasword\r" } 
expect "$ " { send "date\r" } 

expect "$ " { send "readlink /somelink\r" } 

set CCM_BUILD $expect_out(buffer) 

send_log "CCM: $CCM_BUILD" 

expect "$ " { send "date\r" } 
expect "$ " { send "exit\r" } 

中的readlink的結果預計文件將是一個數字,如447,這就是我想在我的日誌文件。但我得到的是:

Password: 
Last login: Tue Feb 28 09:49:42 2017 from gbplr6gn01.genband.com^M 
^[[?1034h-bash-4.1$ date 
Tue Feb 28 09:50:42 CST 2017 
-bash-4.1$ CCM: readlink/localdisk2/jenkins/jobs/CCM/jobs/Deploy_CCM_build/builds/lastSuccessfulBuild 
447 
-bash-4.1$ date 
Tue Feb 28 09:50:42 CST 2017 
-bash-4.1$ 

我怎樣才能得到CCM:447?

回答

0

的更新,這個工作:

#!/opt/tools/unsupported/expect-5.39/bin/expect 

set timeout 10 
match_max 256 
expect_after eof {exit 0} 
log_file -noappend report.txt 

puts -nonewline "Fetching link from ip..." 
spawn -noecho ssh -l user ip 
log_user 0 

expect "Password:" { send "pasword\r" } 
expect "$ " { send "date\r" } 

expect "$ " { send "readlink somelink\r" } 

expect -r "readlink .*\r\n(.*)\r\n" { 
    set CCM_BUILD $expect_out(1,string) 
} 

send_log "CCM: $CCM_BUILD" 
puts "" 
expect "$ " { send "date\r" } 
expect "$ " { send "exit\r" }