2017-02-17 29 views
1

似乎有一個問題在腳本中的spawn ssh中執行awk命令。另外,我無法獲得輸出到一個變量。期望執行awk命令的腳本問題,並將變量puttng expect_out發出。

set file [open "hosts.test"] 
set hosts [split [read -nonewline $file] "\n"] 
close $file 

foreach host $hosts { 
     puts $host 
     spawn ssh -q -o StrictHostKeyChecking=no [lindex $argv 0]@$host 
     expect "Password: " 
     send "[lindex $argv 1]\r" 
     expect -re "(>|#) " 
     send "sudo su -\r" 
     expect "Enter YOUR password: " 
     send "[lindex $argv 1]\r" 
     expect -re "(>|#) " 
     send "cat /etc/SuSE-release | awk -F= '/=/ {print \$2}' | sed -e 's/ * //g' | tr '\012' '.' | sed -e 's/\.$//'" 
     set version $expect_out(buffer) 
     expect -re "(>|#) " 
     puts "$version" 
     send "exit\r" 
     expect -re "(>|#) " 
     send "logout\r" 
} 

錯誤:

# " 
send: sending "cat /etc/SuSE-release | awk -F= '/=/ {print $2}' | sed -e 's/ * //g' | tr '\n' '.' | sed -e 's/.$//'" to { exp4 } 
Gate keeper glob pattern for '(>|#) ' is ''. Not usable, disabling the performance booster. 

expect: does "\u001b(B\u001b[m" (spawn_id exp4) match regular  expression "(>|#) "? (No Gate, RE only) gate=yes re=no 
cat/
expect: does "\u001b(B\u001b[mcat /" (spawn_id exp4) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no 
etc/Su 
expect: does "\u001b(B\u001b[mcat /etc/Su" (spawn_id exp4) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no 
SuSE-release | awk -F= '/=/ {print $2}' | sed -e 's/ * //g' | tr ' 

' '.' | sed -e 's/.$//' expect: does "\u001b(B\u001b[mcat /etc/SuSE-release | awk -F= '/=/ {print $2}' | sed -e 's/ * //g' | tr '\r\n> ' '.' | sed -e 's/.$//'" (spawn_id exp4) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=yes expect: set expect_out(0,string) "> " expect: set expect_out(1,string) ">" expect: set expect_out(spawn_id) "exp4" expect: set expect_out(buffer) "\u001b(B\u001b[mcat /etc/SuSE-release | awk -F= '/=/ {print $2}' | sed -e 's/ * //g' | tr '\r\n> " send: sending "exit\r" to { exp4 } Gate keeper glob pattern for '(>|#) ' is ''. Not usable, disabling the performance booster.

expect: does "' '.' | sed -e 's/.$//'" (spawn_id exp4) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no 
exit 

回答

1

我不是expect專家,但可以簡化,通過這樣分析頗有幾分:

awk '/VERSION/ {a=$3} /PATCHLEVEL/ {a=a"."$3} END{print a}' /etc/SuSE-release 

或者如果你喜歡的東西更簡潔但不太明顯:

awk '/=/ {a=a?a"."$3:$3} END{print a}' /etc/SuSE-release 

也許你會不得不逃脫一些事情,也許是這樣?

send "awk '/VERSION/ {a=\$3} /PATCHLEVEL/ {a=a\".\"\$3} END{print a}' /etc/SuSE-release" 

此外,您還可以發送命令直接在遠程主機在ssh命令行,這可能是另一種途徑的探索。