2015-09-18 42 views
0

我有一個包含服務器(空間)的用戶列表的文件,現在我想將這個文件作爲參數傳遞給我的期望腳本,這樣我的腳本將產生一個ssh會話用戶@服務器,並執行了一堆命令和退出。如何傳遞多個參數從文件的foreach

cat HostsUserFile.txt 

Server1 User1 
Server2 User2 
Server3 User3 

cat CollectStats.exp 

### Get the list of hosts, one per line, whereas hosts.txt should be a file containing the list of servers and user ##### 
set password **** 
set f [open "HostsUserFile.txt"] 
set hosts [split [read -nonewline $f] "\n"] 
close $f 

### Loop through the hosts listed in host.txt ### 
foreach { host user } $hosts { 
### spawn ssh process ### 
spawn -noecho ssh -q [email protected]$host -o StrictHostKeyChecking=no 
expect "*?assword*" 
send "$password\r" 
expect "*$*" 
send "exit\r" 
} 

我想在第一次迭代期間將Server1替換爲主機並將User1替換爲用戶變量等等。

請幫我實現這一目標。謝謝。

+0

什麼你試過這麼遠嗎? –

回答

0

由於變量hosts是列表的列表,(即{Server1 User1} {Server2 User2} {Server3 User3}),你應該使用單變量的foreach循環和內,你可以提取項目,如

foreach hostinfo $hosts { 
    # If your 'Tcl' version is 8.5 or above, use lassign as, 
    #   lassign $hostinfo server user 
    # Else, use 'lindex' to get this done 
    set server [lindex $hostinfo 0] 
    set user [lindex $hostinfo 1] 
    puts "Server : $server , User : $user" 

    # Your further code on ssh 
} 
+0

它的工作!謝謝 –