简体   繁体   English

通过bash中的while循环执行时,通过rsync ssh文件传输期望显示错误

[英]Rsync ssh file transfer through expect showing error when executed through while loop in bash

My program needs automatic file transfer to many remote systems. 我的程序需要自动将文件传输到许多远程系统。 So tried using expect for RSYNC SSH file transfer. 因此,尝试将Expect用于RSYNC SSH文件传输。 While I run the .ex file through terminal providing the necessary inputs or through a simple bash script containing passing values, it worked fine. 当我通过提供必要输入的终端或通过包含传递值的简单bash脚本运行.ex文件时,它运行良好。

But I want to call the .ex file to transfer file through RSYNC to more than 100 machines after checking from database, so I need to run the script through some loops, populating the variables dynamically... 但是我想调用.ex文件以从数据库检查后通过RSYNC将文件传输到100多台机器,因此我需要通过一些循环运行脚本,以动态方式填充变量...

MY .ex file: 我的.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
}
}

My Bash script that shows error: 我的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

Errors Generated : 产生的错误:

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]

But a simple calling from the bash code not showing any error: 但是从bash代码进行的简单调用不会显示任何错误:

#!/bin/bash

./rsync1.ex rups78 10.129.30.44 deba /home/deba/cron.sh 

Please suggest the solution to completely automate the whole file transfer process without the need of any human interference... 请提出完全自动化整个文件传输过程的解决方案,而无需任何人为干扰...

Thanks in advance.. 提前致谢..

It's a bit hard to figure out what you are trying to do, but one simple advise would be to use ssh keys for password-less logon's. 弄清楚您要执行的操作有点困难,但是一个简单的建议是使用ssh密钥进行无密码登录。 This alone have two advantages: 仅此一项就有两个优点:

  • no need for passwords stored in plain text 不需要以纯文本形式存储的密码
  • simpler login procedure from your scipt, you will most likely not need the expect part at all. 通过scipt更简单的登录过程,您很可能根本不需要期望的部分。

Of cause this requires that you have privileges to install the ssh keys on the remote server. 因此,这要求您具有在远程服务器上安装ssh密钥的特权。 Lets assume you have that. 让我们假设你有。 Then the setup job is something along these points: 然后,设置工作就是以下几点:

  1. generate public keys on local machines: ssh-keygen -t dsa 在本地计算机上生成公用密钥: ssh-keygen -t dsa
  2. copy the generated ~/.ssh/id_dsa.pub files to ~/.ssh/authorized_keys on the remote machine 将生成的~/.ssh/id_dsa.pub文件复制到远程计算机上的~/.ssh/authorized_keys

In step 2. you will have to put the content of multiple files into one file, simply append them together. 在步骤2中,您将必须将多个文件的内容放到一个文件中,只需将它们一起添加即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM