思路:html
腳本一:自動下載和安裝JDKjava
#!/bin/sh #is root [ $UID -ne 0 ] &&{ echo "you need to root " exit 2 } #1. is install wget unset isInstall isInstall=`yum list installed |grep wget` [ -n "$isInstall" ] && echo "wget is installed" || { yum install -y wget \ echo "wget install successs" } #donwload jdk JDK="jdk-8u73-linux-x64.tar.gz" wget http://test/jdk/jdk-8u73-linux-x64.tar.gz >/dev/null 2>&1 [ -f "$JDK" ] && echo "download Success" ||{ echo "download Failed" exit 2 } #解壓 /bin/tar -zxf ./jdk-8u73-linux-x64.tar.gz -C /home/zy/tools/ #查看是否解壓成功 java=/home/zy/tools/`ls -l /home/zy/tools/|grep jdk1.8|awk ' {print $NF} '` [ -n "$java" ] && echo "$java" ||{ echo "tar Filed" exit 2 } #配置Java的環境變量 echo "export JAVA_HOME=$java" >> /etc/profile echo "PATH=$PATH:$java/bin" >> /etc/profile . /etc/profile java -version >/dev/null 2>&1 [ $? -eq 0 ] && { echo "jdk install Successs" source /etc/profile }||{ echo "jdk install Fialed" exit 2 }
腳本二:非交互式的設置免祕鑰登陸:mysql
#!/bin/sh #1. ssh-key-gen key(){ expect -c " spawn ssh-keygen expect { \"(/root/.ssh/id_rsa):\" {send \"\r\";exp_continue;} \"(empty for no passphrase)\" {send \"\r\";exp_continue;} \"passphrase again:\" {send \"\r\";exp_continue;} } " } key send_pub(){ expect -c " spawn ssh-copy-id $1 expect { \"(yes/no)?\" {send \"yes\r\";exp_continue;} \"password:\" {send \"123456\r\";exp_continue;} } " } for i in `cat ./hostnames.txt` do send_pub $i done
腳本三:批量分發自動安裝的腳本文件,並執行:linux
#!/bin/sh /bin/sh ./send_sshKey.sh [ "$?" -ne 0 ] && { echo "ssh key-pub faile!" exit 2 } for hosts in `cat ./hostnames.txt`; do ping -c 1 $hosts if [ "$?" -eq 0 ] then scp -r ./auto_install.sh root@$hosts:~/ ssh root@$hosts "sh ~/auto_install.sh" fi done
需求:監控Mysql服務是否正常啓動,若是未正常啓動,就啓動mysql服務
#方法一:過濾mysql端口的3306判斷sql
#!/bin/sh PORT=`netstat -lnt|grep 3306 |awk -F ':::' ' {print $2} '` if [ 「$PORT」 = 「3306」 ];then echo "db is runing " else service mysql restart fi
#方法二:使用wc命令,過濾mysql的行瀏覽器
#!/bin/sh PORT=`netstat -lnt|grep 3306 |wc -l` if [ $PORT -gt 0 ];then echo "db is runing " else service mysql restart fi
#方法三:若是是mysql端口和進程同時存在,即認爲mysql服務正常服務器
#!/bin/sh PORT=`netstat -lnt|grep 3306 |wc -l` PS_ID=` ps -ef |grep mysql|grep -v grep |wc -l` if [ $PORT -gt 0 -a $PS_ID -ge 2 ];then echo "db is runing " else service mysql restart fi