Linux系列——自动化批量安装软件

前言

使用脚本为集群自动化安装软件,能给开发者省很多时间。

准备工作:

带有安装包的服务器需要有httpd、scp命令(也可以写到脚本里面)

yum install -y httpd openssh-clients

被安装的服务器需要有wget命令

yum install -y openssh-clients wget

步骤

1. 编写脚本文件 boot.sh

#!/bin/bash

# 需要安装软件的服务器列表
SERVERS="192.168.137.112 192.168.137.113"
# 登陆服务器的root密码
PASSWORD=123456
# 带有安装包的服务器
BASE_SERVER=192.168.137.111

yum install -y httpd openssh-clients

auto_ssh_copy_id() {
	expect -c "set timeout -1; #设置不超时
		spawn ssh-copy-id $1; #启动新进程,用于执行shell命令
		expect {	#从发起交互的命令的进程接受字符串,用于匹配我们预想的字符串
			*(yes/no)* {send -- yes/r;exp_continue;} #自动输入数据
			*assword:* {send -- $2\r;exp_continue;} #自动输入数据
			eof	       {exit 0;}
		}";
}

ssh_copy_id_to_all() {
	for SERVER in $SERVERS
	do
		auto_ssh_copy_id $SERVER $PASSWORD
	done
}

ssh_copy_id_to_all

for SERVER in $SERVERS
do
	scp install.sh root@$SERVER:/root@$SERVER
	ssh root@$SERVER /root/install.sh
done

2.编写脚本文件 install.sh

#!/bin/bash

BASE_SERVER=192.168.137.111
yum install -y openssh-clients wget
wget $BASE_SERVER/soft/jdk-7u45-linux-x64.tar.gz
tar -zxvf jdk-7u45-linux-x64.tar.gz -C /usr/local
cat >> /etc/profile << EOF
export JAVA_HOME=/usr/local/jdk1.7.0_45
export PATH=\$PATH:\$JAVA_HOME/bin
EOF

 3.给脚本加执行权限

chmod +x boot.sh install.sh

4.执行boot.sh

./boot.sh

原文地址:https://www.cnblogs.com/shuhao66666/p/15196326.html