搜外网>搜外问答>问答页面

shell脚本自动化创建虚拟机的基本配置--tomcat?


自动化shell脚本

ps:此处安装的jdk,maven,tomcat均在/opt目录下,如需其他目录或者其他版本,可根据需要改动shell脚本即可。

ps:所有代码全部手敲,已亲测能够使用,全部分享出来。

ps:上述代码在放进sh文件中,需要chmod改变文件权限赋予它执行权限。

chmod 777 xxx.sh

1

所有文中用到的tar包,均在链接中,可自行下载


www.amuro-namie.cn

提取码:4ae1

一:虚拟机初始化

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成虚拟机初始化

#!/bin/bash

#1.改变机器名

hostnamectl set-hostname $1


www.jzwargame.cn

#2.打通ip地址

sed -i 's/dhcp/static/' /etc/sysconfig/network-scripts/ifcfg-enp0s3

sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-enp0s3

echo "IPADDR=$2" >> /etc/sysconfig/network-scripts/ifcfg-enp0s3

systemctl restart network

#3.关闭防火墙

systemctl stop firewalld


www.a2679.cn

systemctl disable firewalld

1234567891011121314

二:jdk自动化安装

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成jdk自动化安装

#!/bin/bash

jdkTar="/opt/jdk-8u111-linux-x64.tar.gz"

# 检查原先是否已安装

checkExist(){

jdkline1=$(grep -n "export JAVA_HOME=.*" /etc/profile | cut -f1 -d':')

if [ -n "$jdkline1" ];then

echo "存在JAVA_HOME路径,将其删除"

sed -i "${jdkline1}d" /etc/profile

fi

jdkline2=$(grep -n "export CLASSPATH=.*\$JAVA_HOME.*" /etc/profile | cut -f1 -d':')

if [ -n "$jdkline2" ];then

echo "存在CLASSPATH路径,将其删除"

sed -i "${jdkline2}d" /etc/profile

fi

jdkline3=$(grep -n "export PATH=.*\$JAVA_HOME.*" /etc/profile | cut -f1 -d':')

if [ -n "$jdkline3" ];then

echo "存在PATH-JAVA路径,将其删除"

sed -i "${jdkline3}d" /etc/profile

fi


www.racepigeon.cn

}

# 查询是否有jdk压缩包

if [ -e $jdkTar ];then

echo "--------存在jdk安装包-------"

echo "正在解压jdk安装包。。。"

tar -zxvf /opt/jdk-8u111-linux-x64.tar.gz

if [ -e "/opt/soft2" ];then

echo "存在该文件夹,删除。。"

rm -rf /opt/soft2

fi

echo "---------------------------------"

echo "---------------------------------"

echo "正在建立jdk安装路径。。"

echo "---------------------------------"

echo "---------------------------------"

mkdir -p /opt/soft2/jdk180

mv -f jdk1.8.0_111 /opt/soft2/jdk180

# 检查配置信息

checkExist

echo "---------------------------------"

echo "---------------------------------"

echo "正在配置jdk环境。。。"

sed -i '$a export JAVA_HOME=/opt/soft2/jdk180/jdk1.8.0_111' /etc/profile

sed -i '$a export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profile

sed -i '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile

echo "---------------------------------"

echo "---------------------------------"

echo "配置环境已完成。。。"

echo 'this is the end'

echo "---------------------------------"

echo "---------------------------------"

echo "配置版本信息如下:"

source /etc/profile

java -version

else

echo "未检测到安装包,请将安装包放到opt目录下"

fi

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263

三:maven自动化安装

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成maven自动化安装

#!/bin/bash

mavenTar="/opt/apache-maven-3.6.1-bin.tar.gz"

mavenMirror="

alimaven

central

aliyun maven

https://maven.aliyun.com/repository/central


alimaven

aliyun maven

http://maven.aliyun.com/nexus/content/groups/public/

central

"

mavenrespo="/opt/soft2/maven361/apache-maven-3.6.1/jarstore"

# 检查原先是否已安装

checkExist(){

mavenline1=$(grep -n "export MAVEN_HOME=.*" /etc/profile | cut -f1 -d':')

if [ -n "$mavenline1" ];then

echo "存在MAVEN_HOME路径,将其删除"

sed -i "${mavenline1}d" /etc/profile

fi

mavenline3=$(grep -n "export PATH=.*\$MAVEN_HOME.*" /etc/profile | cut -f1 -d':')

if [ -n "$mavenline3" ];then

echo "存在PATH-MAVEN路径,将其删除"

sed -i "${mavenline3}d" /etc/profile

fi

}

# 查询是否有maven压缩包

if [ -e $mavenTar ];then

echo "--------存在maven安装包-------"

echo "正在解压maven安装包。。。"

tar -zxvf /opt/apache-maven-3.6.1-bin.tar.gz

if [ -e "/opt/soft2/maven361" ];then

echo "存在该文件夹,删除。。"

rm -rf /opt/soft2/maven361

fi

echo "---------------------------------"

echo "---------------------------------"

echo "正在建立maven安装路径。。"

echo "---------------------------------"

echo "---------------------------------"

mkdir -p /opt/soft2/maven361

mv -f apache-maven-3.6.1 /opt/soft2/maven361

# 检查配置信息

checkExist

echo "---------------------------------"

echo "---------------------------------"

echo "正在配置maven环境。。。"

sed -i '$a export MAVEN_HOME=/opt/soft2/maven361/apache-maven-3.6.1' /etc/profile

sed -i '$a export PATH=$PATH:$MAVEN_HOME/bin' /etc/profile

echo "---------------------------------"

echo "---------------------------------"

echo "----正在配置maven镜像源和mirror----"

sed -i '/<\/mirrors>/i ${mavenMirror}' /opt/soft2/maven361/apache-maven-3.6.1/conf/settings.xml

sed -i '/<\/settings>/i ${mavenrespo}' /opt/soft2/maven361/apache-maven-3.6.1/conf/settings.xml

echo "---------------------------------"

echo "---------------------------------"

echo "配置环境已完成。。。"

echo 'this is the end'

echo "---------------------------------"

echo "---------------------------------"

echo "配置版本信息如下:"

source /etc/profile

mvn -version

else

echo "未检测到安装包,请将安装包放到opt目录下"

fi

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778

四:tomcat自动化安装

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成tomcat自动化安装

#!/bin/bash

tomcatTar="/opt/apache-tomcat-8.5.54.tar.gz"

# 查询是否有tomcat压缩包

if [ -e $tomcatTar ];then

echo "--------存在tomcat安装包-------"

echo "正在解压maven安装包。。。"

tar -zxvf /opt/apache-tomcat-8.5.54.tar.gz

if [ -e "/opt/soft2/tomcat85" ];then

echo "存在该文件夹,删除。。"

rm -rf /opt/soft2/tomcat85

fi

echo "---------------------------------"

echo "---------------------------------"

echo "正在建立tomcat安装路径。。"

echo "---------------------------------"

echo "---------------------------------"

mkdir -p /opt/soft2/tomcat85

mv -f apache-tomcat-8.5.54 /opt/soft2/tomcat85

echo "---------------------------------"

echo "---------------------------------"

echo "配置tomcat环境已完成。。。"

echo 'this is the end'

echo "---------------------------------"

echo "---------------------------------"

else

echo "未检测到安装包,请将安装包放到opt目录下"

fi

12345678910111213141516171819202122232425262728293031323334

五:mysql自动化安装

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成mysql自动化安装—mysql脚本要执行两遍,第一遍可能检测不到mysql,server无法执行,安装好后登录mysql,没有密码,直接enter。

————————————————

版权声明:本文为CSDN博主「NICEDAYSS」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_35050438/java/article/details/106199592

追加问题
    2 人参与回答
权重蜘蛛-池出租 等 1 人赞同该回答

这就是,垃圾内容