shell多进程scp传文件
昨天给大家了一个shell多进程并发,今天我们来看怎么同时批量在定义数量的服务器上执行相关命令,比起普通for/while循环只能顺序一条一条执行的效率高非常多,在管理大批服务器时非常的实用.以下脚本功能是通过scp(也可选rsync)向上千台服务器传更新包,脚本运行后同时在后台有50个scp进程向服务器传包:
更多的功能就要大家自己去发掘了.
#!/bin/bash
ip=`cat iplist.txt|grep -v "#"|awk '{print $1}'`
dir='/usr/local/src'
answer="yes" #定义yes/no应答变量
passwd="123456" #服务器密码
thead_num=50
tmp_fifo_file="/tmp/$$.fifo"
mkfifo $tmp_fifo_file
exec 4<>$tmp_fifo_file
rm -f $tmp_fifo_file
for ((i=0;i<$thead_num;i++))
do
echo ""
done >&4
for i in $ip
do
read -u4
{
expect <<EOF
set timeout -1
spawn scp -P 1000 $1 $i:$dir
expect "(yes/no)?" {
send "$answer\r"
expect "Password:"
send "$passwd\r"
} "Password:" {send "$passwd\r"} "*host" {exit 1}
expect eof
EOF
sleep 3
echo "" >&4
}&
done
wait
exec 4>&-
exit 0更多的功能就要大家自己去发掘了.


评论: