当前位置: > shell编程 >

shell多进程scp传文件

时间:2016-06-05 00:16来源:linux.it.net.cn 作者:IT
昨天给大家了一个shell多进程并发,今天我们来看怎么同时批量在定义数量的服务器上执行相关命令,比起普通for/while循环只能顺序一条一条执行的效率高非常多,在管理大批服务器时非常的实用.以下脚本功能是通过scp(也可选rsync)向上千台服务器传更新包,脚本运行后同时在后台有50个scp进程向服务器传包:
view source
 
print?
01 #!/bin/bash
02 ip=`cat iplist.txt|grep -v "#"|awk '{print $1}'`
03 dir='/usr/local/src'
04 answer="yes"    #定义yes/no应答变量
05 passwd="123456" #服务器密码
06 thead_num=50
07 tmp_fifo_file="/tmp/$$.fifo"
08 mkfifo $tmp_fifo_file
09 exec 4<>$tmp_fifo_file
10 rm -f $tmp_fifo_file
11 for ((i=0;i<$thead_num;i++))
12 do
13         echo ""
14 done >&4
15 for i in $ip
16 do
17         read -u4
18                 {
19                         expect <<EOF
20                         set timeout -1
21                         spawn scp -P 1000 $1 $i:$dir
22                         expect "(yes/no)?" {
23                         send "$answer\r"
24                         expect "Password:"
25                         send "$passwd\r"
26                         } "Password:" {send "$passwd\r"} "*host" {exit 1}
27                         expect eof
28                         EOF
29                        
30                         sleep 3
31                         echo "" >&4
32                 }&
33 done
34 wait
35 exec 4>&-
36 exit 0
 


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容