-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyfileto.sh
executable file
·67 lines (56 loc) · 1.37 KB
/
copyfileto.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
start_time=`date +%s`
configfile=copyfileto.cfg
work=4
log=record
# 判断文件是否存在
if [ ! -f $configfile ]; then
echo "[$configfile] not exist ."
# echo "usr ip pw src_dir dst_dir" > configs
else
echo "[$configfile] has created ..."
fi
# 增加记录文件
if [ ! -d $log ]; then
mkdir $log
echo "[$log] created ..."
fi
# 清空原来的记录文件
rm $log/* -rf
# 建立有名管道用于多进程数量控制
[ -e fd1 ] || mkfifo fd1
exec 3<>fd1
rm -f fd1
# 建立 work 个线程
for((i=1; i<=$work; i++)) do
echo >&3
done
# 分发到各个节点,这里分发到 configfile 文件中的主机中.
while read line
do
read -u3
usr=`echo $line | cut -d " " -f 2`
ip=`echo $line | cut -d " " -f 1`
pw=`echo $line | cut -d " " -f 3`
src_dir=`echo $line | cut -d " " -f 4`
dst_dir=`echo $line | cut -d " " -f 5`
echo "send [$src_dir] to [$usr@$ip:$dst_dir]"
{
expect <<EOF
set timeout 600
spawn rsync -av $src_dir $usr@$ip:$dst_dir
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$pw\n" }
}
expect "password" { send "$pw\n" }
EOF
echo >&3
echo "send [$src_dir] to [$usr@$ip:$dst_dir] done."
} >> $log/$usr@$ip.log & # 以追加的方式增加记录文件, 避免多条同一个设备的传输记录被覆盖
done < $configfile
wait
stop_time=`date +%s`
echo "TIME:`expr $stop_time - $start_time`"
exec 3<&-
exec 3>&-