Shell分布式系统分发文件脚本

#! /bin/bash
########################################################
# Filename: ./dispatch_files.sdl
# Date: 2014-10-14 13:22:00
# Author: chenhuidong
# Email: chdyczx@live.com
# Note: 
########################################################

usage()
{
	echo "usage:"
	echo "	./dispatch_files.sh"
	echo "	dispatch files to users in translist.conf "
	echo "	translist.conf line format is ip user passwd"
	echo "	filelist.conf contains sourcepath and remote destpath"
	echo "	filelist.conf line format is sourcepath destpath"
	echo ""
}

trans_file()
{
expect << EOF
spawn scp $1
expect {
    "*password:" {
        	send "$2\n";exp_continue
        }
    eof {
        	exit
        }
}
EOF
}

remote_cmd()
{
	expect << EOF
	spawn ssh $2@$1 "$4"
	expect {
    "*password:" {
        	send "$3\n";exp_continue
        }
    eof {
        	exit
        }
}
EOF
}

dispatch_file()
{
	filename=./translist.conf
	filename2=./filelist.conf
	
	if [ ! -f $filename -o ! -f $filename2 ]
	then
		echo "File $filename or $filename2 is not exist."
		echo ""
		usage
		exit 1
	fi
	
	cat $filename | while read line
	do
		v_ip=`echo $line | awk -F " " '{print $1}'`
		v_user=`echo $line | awk -F " " '{print $2}'`
		v_passwd=`echo $line | awk -F " " '{print $3}'`
		echo $v_ip,$v_user,$v_passwd
		cat $filename2 | while read line2
		do
			v_srcname=`echo $line2 | awk -F " " '{print $1}'`
			v_destname=`echo $line2 | awk -F " " '{print $2}'`	
			
			v_dirname=`dirname $v_destname`
			echo "$v_dirname"
			remote_cmd $v_ip $v_user $v_passwd "mkdir -p $v_dirname"
			trans_file "$v_srcname $v_user@$v_ip:$v_destname" "$v_passwd"
		done
	done
}

main()
{
if [ $# -eq 0 ] || [ $1 == "--help" ] || [ $1 == "-h" ]
then
	usage
	exit 1
elif [ $1=="-all" ]
then
	dispatch_file
else
	usage
	exit 1
fi

}

main $*

exit 0


translist.conf中填入需发送到的主机信息,格式为:ip user passwd

如下:

192.168.0.101 chdyczx1 123456


fileslist.conf中填入原文件路径及发送到主机的文件路径

如下:

/home/chdyczx/1.sh  /home/chdyczx/test1/2.sh

/home/chdyczx/test/2.txt /home/chdyczx/2.txt




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值