Linux 集群脚本基础

版权声明:欢迎转载,转载请说明出处. 大数据Github项目地址https://github.com/SeanYanxml/bigdata。 https://blog.csdn.net/u010416101/article/details/88758103

前言

在最近安装虚拟机集群的时候,随意写了下虚拟机集群的脚本.


基础知识

#!/bin/bash
string="hello,shell,haha"  
array=(${string//,/ })  
for var in ${array[@]}
do
   echo $var
done 
  1. SSH免密登陆
    cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

  2. 远程操作
    ssh root@${i} "<操作1>;<操作2>"
    例如: ssh [email protected] "cat /etc/profile;cat /ect/profile"


正文

集群安装JDK的脚本.

#!/bin/bash
# 需要安装的机器(也可以从配置文件读取)
cluster_nodes="192.168.31.60,192.168.31.61,192.168.31.62"
cluster_nodes=${cluster_nodes//,/ }
# 安装名字
jdk_tar_name="jdk-1.8.0_151.tar.gz";
# 安装目录
install_folder="/opt/apps/jdk"

# 循环所有的结点
for i in ${cluster_nodes}; do 
# make dir
	ssh root@${i} "mkdir -p ${install_folder};" 
# 
# 分发
	scp ${jdk_tar_name} root@${i}:${install_folder}/
# 解压
	ssh root@${i} "cd ${install_folder};tar -zxvf jdk-1.8.0_151.tar.gz; mv jdk1.8.0_151 jdk-1.8;"
done

for i in ${cluster_nodes}; do 
	echo ${i};
	ssh root@${i} "if [[ ! $PATH =~ /opt/apps/jdk/jdk-1.8 ]]; then echo 'export JAVA_HOME=/opt/apps/jdk/jdk-1.8' >> /etc/profile; echo 'export PATH=\$JAVA_HOME/bin:\$PATH' >> /etc/profile; . /etc/profile; fi"
done

Reference

[1]. shell script 在if 的判断条件正则表达式=~中引号问题
[2]. 请教会linux shell脚本的=~是什么意思?
[3]. Shell中判断文件,目录是否存在
[4]. shell 使用指定的分割符来分割字符串

猜你喜欢

转载自blog.csdn.net/u010416101/article/details/88758103