这篇文章主要介绍了shell脚本自动化如何创建虚拟机,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。自动化shell脚本ps:此处安装的jdk,Maven,Tomcat均在/o
这篇文章主要介绍了shell脚本自动化如何创建虚拟机,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
自动化shell脚本
ps:此处安装的jdk,Maven,Tomcat均在/opt目录下,如需其他目录或者其他版本,可根据需要改动shell脚本即可。
ps:所有代码全部手敲,已亲测能够使用,全部分享出来。
一:虚拟机初始化
创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成虚拟机初始化
#!/bin/bash#1.改变机器名hostnamectl set-hostname $1#2.打通ip地址sed -i 's/dhcp/static/' /etc/sysconfig/network-scripts/ifcfgenp0s3sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfgenp0s3echo "IPADDR=$2" >> /etc/sysconfig/network-scripts/ifcfgenp0s3systemctl restart network#3.关闭防火墙systemctl stop firewalldsystemctl disable firewalld
二:jdk自动化安装
创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成jdk自动化安装
#!/bin/bashjdkTar="/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 fijdkline2=$(grep -n "export CLASSPATH=.*\$JAVA_HOME.*" /etc/profile | cut -f1 -d':') if [ -n "$jdkline2" ];then echo "存在CLASSPATH路径,将其删除" sed -i "${jdkline2}d" /etc/profile fijdkline3=$(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}# 查询是否有jdk压缩包if [ -e $jdkTar ];thenecho "--------存在jdk安装包-------"echo "正在解压jdk安装包。。。"tar -zxvf /opt/jdk-8u111-linux-x64.tar.gzif [ -e "/opt/soft2" ];thenecho "存在该文件夹,删除。。"rm -rf /opt/soft2fiecho "---------------------------------"echo "---------------------------------"echo "正在建立jdk安装路径。。"echo "---------------------------------"echo "---------------------------------"mkdir -p /opt/soft2/jdk180mv -f jdk1.8.0_111 /opt/soft2/jdk180# 检查配置信息checkExistecho "---------------------------------"echo "---------------------------------"echo "正在配置jdk环境。。。"sed -i '$a export JAVA_HOME=/opt/soft2/jdk180/jdk1.8.0_111' /etc/profilesed -i '$a export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profilesed -i '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profileecho "---------------------------------"echo "---------------------------------"echo "配置环境已完成。。。"echo 'this is the end'echo "---------------------------------" echo "---------------------------------" echo "配置版本信息如下:" source /etc/profile java -versionelseecho "未检测到安装包,请将安装包放到opt目录下"fi
三:maven自动化安装
创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成maven自动化安装
#!/bin/bashmavenTar="/opt/apache-maven-3.6.1-bin.tar.gz"mavenMirror=" <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/central</url> </mirror> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>Http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>"mavenrespo="<localRepository>/opt/soft2/maven361/apache-maven-3.6.1/jarstore</localRepository>"# 检查原先是否已安装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 fimavenline3=$(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 ];thenecho "--------存在maven安装包-------"echo "正在解压maven安装包。。。"tar -zxvf /opt/apache-maven-3.6.1-bin.tar.gzif [ -e "/opt/soft2/maven361" ];thenecho "存在该文件夹,删除。。"rm -rf /opt/soft2/maven361fiecho "---------------------------------"echo "---------------------------------"echo "正在建立maven安装路径。。"echo "---------------------------------"echo "---------------------------------"mkdir -p /opt/soft2/maven361mv -f apache-maven-3.6.1 /opt/soft2/maven361# 检查配置信息checkExistecho "---------------------------------"echo "---------------------------------"echo "正在配置maven环境。。。"sed -i '$a export MAVEN_HOME=/opt/soft2/maven361/apache-maven-3.6.1' /etc/profilesed -i '$a export PATH=$PATH:$MAVEN_HOME/bin' /etc/profileecho "---------------------------------"echo "---------------------------------"echo "----正在配置maven镜像源和mirror----"sed -i '/<\/mirror>/i ${mavenMirror}' /opt/soft2/maven361/apache-maven-3.6.1/conf/settings.xmlsed -i '/<\/settings>/i ${mavenrespo}' /opt/soft2/maven361/apache-maven-3.6.1/conf/settings.xmlecho "---------------------------------"echo "---------------------------------"echo "配置环境已完成。。。"echo 'this is the end'echo "---------------------------------" echo "---------------------------------" echo "配置版本信息如下:" source /etc/profile mvn -versionelseecho "未检测到安装包,请将安装包放到opt目录下"fi
四:tomcat自动化安装
创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成tomcat自动化安装
#!/bin/bashtomcatTar="/opt/apache-tomcat-8.5.54.tar.gz" # 查询是否有tomcat压缩包if [ -e $tomcatTar ];thenecho "--------存在tomcat安装包-------"echo "正在解压maven安装包。。。"tar -zxvf /opt/apache-tomcat-8.5.54.tar.gzif [ -e "/opt/soft2/tomcat85" ];thenecho "存在该文件夹,删除。。"rm -rf /opt/soft2/tomcat85fiecho "---------------------------------"echo "---------------------------------"echo "正在建立tomcat安装路径。。"echo "---------------------------------"echo "---------------------------------"mkdir -p /opt/soft2/tomcat85mv -f apache-tomcat-8.5.54 /opt/soft2/tomcat85echo "---------------------------------"echo "---------------------------------"echo "配置tomcat环境已完成。。。"echo 'this is the end'echo "---------------------------------"echo "---------------------------------"elseecho "未检测到安装包,请将安装包放到opt目录下"fi
五:mysql自动化安装
创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成Mysql自动化安装
#!/bin/bashmysqlInstall=$(rpm -qa | grep mysql)mysqlRepoInstall1=$(find /etc/yum.repos.d/ -name "mysql-commUnity.repo" | sed 's#.*/##')mysqlRepoInstall2=$(find /etc/yum.repos.d/ -name "mysql-community-source.repo" | sed 's#.*/##')wgetInstall=$(rpm -qa | grep wget)mariadbInstall=$(rpm -qa | grep mariadb)# 检查是否已安装了mysql,未安装再安装if [ ! -n "${mysqlInstall}" ];then # 检查Centos是否自带了mariadb,若有则删除 if [ -n "${mariadbInstall}" ];thenecho "自带mariadb---删除mariadb中----" rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64echo "--------------------------------"echo "--------------------------------"echo "--------------------------------"elseecho "centOS里没有mariadb软件"echo "-----------------------"echo "-----------------------" fiif [ -n "${wgetInstall}" ];thenecho "-------存在wget下载工具-------"echo "------------------------------"echo "------------------------------"elseecho "-------安装下载工具wget-------"echo "------------------------------"echo "------------------------------"yum install -y wgetfiecho "-------在线下载5.7mysql并安装-------"wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpmecho "-----------下载mysql成功-------------"echo "-------------------------------------"echo "-----------正在安装mysql中----------"echo "-------------------------------------"rpm -ivh mysql-community-release-el7-5.noarch.rpmecho "-----正在检测mysql安装环境是否成功----"elseecho "-------------已安装过mysql--------------"fiinstallmysqlServer(){if [ "${mysqlRepoInstall1}" = "mysql-community.repo" -a "${mysqlRepoInstall2}" = "mysql-community-source.repo" ];then echo "存在2个mysql安装环境,下载安装完成。。。" echo "----------------------------------------" echo "----------------------------------------"echo "-----------安装mysql-server中-----------" echo "----------------------------------------" echo "----------------------------------------"if [ ! -e "/var/lib/mysql" ];thenyum install -y mysql-serverelseecho "-----已安装过mysql-server-----"fi echo "修改访问权限为当前用户。。。" chown -R root:root /var/lib/mysql ls -l /var/lib echo "重启mysql服务中" service mysqld restart echo "安装完成---请登录mysql修改密码和字符集" else echo "${mysqlRepoInstall1}" echo "${mysqlRepoInstall2}" echo "----没有安装成功----"fi}installmysqlServer
感谢你能够认真阅读完这篇文章,希望小编分享的“shell脚本自动化如何创建虚拟机”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: shell脚本自动化如何创建虚拟机
本文链接: https://lsjlt.com/news/255917.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0