您好,登錄后才能下訂單哦!
這篇文章主要介紹了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/ifcfgenp0s3 sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfgenp0s3 echo "IPADDR=$2" >> /etc/sysconfig/network-scripts/ifcfgenp0s3 systemctl restart network #3.關閉防火墻 systemctl stop firewalld systemctl disable firewalld
二: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 } # 查詢是否有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
三:maven自動化安裝
創建shell腳本,把命令放進.sh腳本里,./xx.sh運行此腳本可以完成maven自動化安裝
#!/bin/bash mavenTar="/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 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 '/<\/mirror>/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
四: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
五:mysql自動化安裝
創建shell腳本,把命令放進.sh腳本里,./xx.sh運行此腳本可以完成mysql自動化安裝
#!/bin/bash mysqlInstall=$(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}" ];then echo "自帶mariadb---刪除mariadb中----" rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64 echo "--------------------------------" echo "--------------------------------" echo "--------------------------------" else echo "centOS里沒有mariadb軟件" echo "-----------------------" echo "-----------------------" fi if [ -n "${wgetInstall}" ];then echo "-------存在wget下載工具-------" echo "------------------------------" echo "------------------------------" else echo "-------安裝下載工具wget-------" echo "------------------------------" echo "------------------------------" yum install -y wget fi echo "-------在線下載5.7mysql并安裝-------" wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm echo "-----------下載mysql成功-------------" echo "-------------------------------------" echo "-----------正在安裝mysql中----------" echo "-------------------------------------" rpm -ivh mysql-community-release-el7-5.noarch.rpm echo "-----正在檢測mysql安裝環境是否成功----" else echo "-------------已安裝過mysql--------------" fi installmysqlServer(){ 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" ];then yum install -y mysql-server else echo "-----已安裝過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腳本自動化如何創建虛擬機”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。