2016-08-24 53 views
0

SCRIPT:Expect腳本運行只需要一個命令,並顯示錯誤

#!/usr/bin/expect 
set username "sv111111" 
set password "Sandy789" 
set f [open "servers.txt"] 
set hosts [split [read $f] "\n"] 
close $f 
foreach host $hosts { 
#spawn ssh -o StrictHostKeyChecking=no [email protected]$hosts 
spawn ssh [email protected]$host 
expect "[email protected]$host's password:" 
send -- "$password\n" 
expect "$" 
send -- "sudo -u sandy /opt/sandy/ship/common/tools/arpq.sh\n" 
expect "[email protected]$host's password:" 
send -- "$password\n" 
expect "$" 
send -- "sudo -u sandy /opt/sandy/ship/common/tools/showps2.pl\n" 

expect "$" 
send -- "exit\n" 
expect eof 
close 
} 
在上述腳本

它完美地運行在servers.txt文件中提到的所有服務器的第一須藤命令。第二個sudo命令沒有執行,並顯示下面的錯誤。

spawn ssh [email protected] 
ssh: : Name or service not known 
send: spawn id exp10 not open 
    while executing 
"send -- "$password\n"" 
    ("foreach" body line 5) 
    invoked from within 
"foreach host $hosts { 
#spawn ssh -o StrictHostKeyChecking=no [email protected]$hosts 
spawn ssh [email protected]$host 
expect "[email protected]$host's password:" 
send -- ..." 
    (file "./sandy_try.sh" line 8) 

請幫助

+0

喜@Glenn傑克曼:我需要使用郵件服務器的日誌文件發送到兩個用戶。你能幫忙嗎? – sandeep

+0

提問題 –

+0

我不知道glenn。我不能再問新問題 – sandeep

回答

0

read命令抓住後面有個換行符文件內容。然後,當你在換行符上分割時,列表中有一個空字符串的尾部元素。使用以下之一:

set f [open "servers.txt"] 
set hosts [split [read -nonewline $f] "\n"] 
# .....................^^^^^^^^^^ 
close $f 
foreach host $hosts { ... 

set f [open "servers.txt"] 
while {[gets $f host] != -1} { 
    spawn ssh ... 
} 
close $f 
+0

非常感謝glenn :)。第一個爲我工作。 – sandeep

相關問題