配置文件
deploy.conf
#规划集群角色
cdh1,master,all,
cdh2,slave,all,
cdh3,slave,all,
deploy.sh分发脚本
#!/bin/bash
if [ $# -lt 3 ] //如果参数的个数小于3,则报错 $#:获取参数个数
then
echo "Usage: ./deploy.sh srcFile(or Dir) descFile(or Dir) MachineTag"
echo "Usage: ./deploy.sh srcFile(or Dir) descFile(or Dir) MachineTag confFile"
exit
fi
src=$1
dest=$2
tag=$3
if [ 'a'$4'a' == 'aa' ]
then
confFile=/home/hadoop/tools/deploy.conf
else
confFile=$4
fi
if [ -f $confFile ] //判断文件是否存在 -d判断目录是否存在
then
if [ -f $src ]
then
for server in `cat $confFile | grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
//-v 排除 去除含有#行 包含slave,用,分割,得到第一个变量
do
scp $src $server":"${dest}
done
elif [ -d $src ]
then
for server in `cat $confFile | grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
//【去除含有#行】 【用,分割,匹配,slave,的字符串】,【用,分割,解析,{print $1}为获取第一列】
do
scp -r $src $server":"${dest}
done
else
echo "Error: No source file exist"
fi
else
echo "Error: Please assign config file or run deploy.sh command with deploy.conf in same directory"
fi
#!/bin/bash
if [ $# -lt 3 ]
then
echo "Usage: ./deploy.sh srcFile(or Dir) descFile(or Dir) MachineTag"
echo "Usage: ./deploy.sh srcFile(or Dir) descFile(or Dir) MachineTag confFile"
exit
fi
src=$1
dest=$2
tag=$3
if [ 'a'$4'a' == 'aa' ]
then
confFile=/root/tools/deploy.conf
else
confFile=$4
fi
if [ -f $confFile ]
then
if [ -f $src ]
then
for server in `cat $confFile | grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
do
scp $src $server":"${dest}
done
elif [ -d $src ]
then
for server in `cat $confFile | grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
do
scp -r $src $server":"${dest}
done
else
echo "Error: No source file exist"
fi
else
echo "Error: Please assign config file or run deploy.sh command with deploy.conf in same directory"
fi
分发测试
[root@cdh1 tools]# ./deploy.sh deploy.conf /root/ slave
deploy.conf 100% 68 0.1KB/s 00:00
deploy.conf 100% 68 0.1KB/s 00:00
远程执行
runRemoteCmd.sh
#!/bin/bash
if [ $# -lt 2 ]
then
echo "Usage: ./runRemoteCmd.sh Command MachineTag"
echo "Usage: ./runRemoteCmd.sh Command MachineTag confFile"
exit
fi
cmd=$1
tag=$2
if [ 'a'$3'a' == 'aa' ]
then
confFile=/home/hadoop/tools/deploy.conf
else
confFile=$3
fi
if [ -f $confFile ]
then
for server in `cat $confFile | grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
do
echo "*******************$server***********************"
ssh $server "source ~/.bashrc; $cmd" //先生效,再执行
done
else
echo "Error: Please assign config file or run deploy.sh command with deploy.conf in same directory"
fi
#!/bin/bash
if [ $# -lt 2 ]
then
echo "Usage: ./runRemoteCmd.sh Command MachineTag"
echo "Usage: ./runRemoteCmd.sh Command MachineTag confFile"
exit
fi
cmd=$1
tag=$2
if [ 'a'$3'a' == 'aa' ]
then
confFile=/root/tools/deploy.conf
else
confFile=$3
fi
if [ -f $confFile ]
then
for server in `cat $confFile | grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
do
echo "*******************$server***********************"
ssh $server "source /etc/profile; $cmd"
done
else
echo "Error: Please assign config file or run deploy.sh command with deploy.conf in same directory"
fi
/etc/profile 是对所有用户都生效的环境变量
~/.bashrc 是对某个用户生效,不影响其他用户
远程执行测试
[root@cdh1 tools]# ./runRemoteCmd.sh "mkdir /root/app" all
*******************cdh1***********************
*******************cdh2***********************
*******************cdh3***********************
[root@cdh1 tools]# ./runRemoteCmd.sh "mkdir /root/data" all
*******************cdh1***********************
*******************cdh2***********************
*******************cdh3***********************