![](/img/trans.png)
[英]Removing a remote file through ssh stops the while loop the command is in?
[英]Rsync ssh file transfer through expect showing error when executed through while loop in bash
我的程序需要自動將文件傳輸到許多遠程系統。 因此,嘗試將Expect用於RSYNC SSH文件傳輸。 當我通過提供必要輸入的終端或通過包含傳遞值的簡單bash腳本運行.ex文件時,它運行良好。
但是我想調用.ex文件以從數據庫檢查后通過RSYNC將文件傳輸到100多台機器,因此我需要通過一些循環運行腳本,以動態方式填充變量...
我的.ex文件:
#!/usr/bin/expect -f
set pass [lindex $argv 0]
set server [lindex $argv 1]
set name [lindex $argv 2]
set addr [lindex $argv 3]
set timeout -1
spawn rsync -e "ssh -q -o StrictHostKeyChecking=no" ${addr} ${name}@${server}:/home/deba/
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\n"
exp_continue
#look for the password prompt
}
"*?assword:*" {
send ${pass}
send "\n"
interact
#The expect command will now return
}
}
我的Bash腳本顯示錯誤:
#!/bin/bash
cat /home/deba/content_sync/test/result_remote/sync_Dest_List.txt | while read line2
do
java -cp .:mysql-connector-java-5.1.24-bin.jar Remote_sync_pw_ip_reciever $line2
cat /home/deba/content_sync/test/result_remote/sync_Dest_List.txt | while read line
# this folder contains the arguments for password and server IP separated by space
do
IFS=" " read var1 var2 <<< "$line"
./rsync1.ex $var1 $var2 $line2 /home/deba/cron.sh
done
done
產生的錯誤:
spawn rsync -e ssh -q -o StrictHostKeyChecking=no /home/deba/cron.sh@deba:/home/deba/
skipping directory .
rsync: mkdir "/home/deba/cron.sh@deba:/home/deba" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(605) [Receiver=3.0.9]
rsync: connection unexpectedly closed (9 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [sender=3.0.9]
但是從bash代碼進行的簡單調用不會顯示任何錯誤:
#!/bin/bash
./rsync1.ex rups78 10.129.30.44 deba /home/deba/cron.sh
請提出完全自動化整個文件傳輸過程的解決方案,而無需任何人為干擾...
提前致謝..
弄清楚您要執行的操作有點困難,但是一個簡單的建議是使用ssh密鑰進行無密碼登錄。 僅此一項就有兩個優點:
因此,這要求您具有在遠程服務器上安裝ssh密鑰的特權。 讓我們假設你有。 然后,設置工作就是以下幾點:
ssh-keygen -t dsa
~/.ssh/id_dsa.pub
文件復制到遠程計算機上的~/.ssh/authorized_keys
在步驟2中,您將必須將多個文件的內容放到一個文件中,只需將它們一起添加即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.