进阶版Shell脚本合集
生活随笔
收集整理的這篇文章主要介紹了
进阶版Shell脚本合集
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、監控服務端口腳本
- 二、編譯安裝Nginx腳本
- 三、監控一個主機狀態腳本
- 四、統計內存、CPU使用前十進程腳本
- 五、I/O列長度監控腳本
- 六、計算內存使用率占比
- 七、一鍵部署wordpress博客平臺
一、監控服務端口腳本
腳本是基于telnet命令進行監控端口服務,所以運行腳本前務必檢查telnet工具是否安裝!
[root@shell jao]# cat monitor_prot.sh #!/bin/bash prot_status(){ temp_file=`mktemp prot_status.XXX`# 1、判斷telnet命令是否存在 [ ! -x /usr/bin/telnet ] && echo "telnet: not found comand" && exit 1# 2、測試端口 $1:IP地址 $2:端口號 ( telnet $1 $2 <<EOF quit EOF) &> $temp_file# 3、分析文件中內容 判斷結果if egrep "\^]" $temp_file &>/dev/null;thenecho "$1 $2 open" elseecho "$1 $2 down" firm -f $temp_file } prot_status $1 $2運行腳本
# 語法: IP地址 端口號 [root@shell jao]# bash monitor_prot.sh 192.168.1.68 22 192.168.1.68 22 open [root@shell jao]# bash monitor_prot.sh 192.168.1.68 80 192.168.1.68 80 open [root@shell jao]# bashmonitor_prot.sh 192.168.1.68 21 192.168.1.68 21 down二、編譯安裝Nginx腳本
[root@shell jao]# cat nginx.sh #!/bin/bashclear echo "Preparations before installation..." ##variabled NGINX_VERSION="nginx-1.16.0" NGINX_INSTALL_DOC="/usr/local/nginx" NGINX_USER="nginx" NGINX_GROUP="nginx" NGINX_CONFIGURE="--prefix=${NGINX_INSTALL_DOC} --user=${NGINX_USER} --group=${NGINX_GROUP} --with-http_ssl_module --with-http_stub_status_module"##function nginx_check(){ # 1、監測當前用戶 要求為root if [ "$USER" != 'root' ];thenecho "need to be root so that"exit 5 fi# 2、檢查wget命令 WGET_CHECK=$(rpm -q wget) if [ $? -ne 0 ];then yum -y install wget &> /dev/null fi }nginx_install_pre(){ # 1、安裝依賴 if ! (yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel openssl openssl-devel elinks 1>/dev/null);thenecho "ERROR:YUM install error"exit 5 fi # 2、下載nginx源碼包 if (wget http://nginx.org/download/${NGINX_VERSION}.tar.gz &>/dev/null);thentar zxf ${NGINX_VERSION}.tar.gzif [ ! -d ${NGINX_VERSION} ];thenecho "ERROR:not found ${NGINX_VERSION}";exit 5 fi elseecho "ERROR:wget download file ${NGINX_VERSION}.tar.gz fail" fi }nginx_install_make(){ (groupadd ${NGINX_GROUP} ;useradd -s /sbin/nologin -r -M -g ${NGINX_GROUP} ${NGINX_USER}) &>/dev/null cd ${NGINX_VERSION} echo "nginx configure..." if ./configure ${NGINX_CONFIGURE} 1>/dev/null;thenecho "nginx make ..."if make 1>/dev/null;thenecho "nginx make install ..."if make install 1>/dev/null;thenecho "nginx install success"elseecho "ERROR: nginx install tail!";exit 5fielseecho "ERROR: nginx make tail!";exit 5fi elseecho"ERROR: nginx configure tail!";exit 5 fi }# 配置nginx開機自啟,使用systemctl 管理nginx服務 nginx_enable(){ cat > /usr/lib/systemd/system/nginx.service <<EOF [Unit] Description=nginx After=network.target[Service] Type=forking ExecStart=${NGINX_INSTALL_DOC}/sbin/nginx ExecReload=${NGINX_INSTALL_DOC}/sbin/nginx -s reload ExecStop=${NGINX_INSTALL_DOC}/sbin/nginx -s quit PrivateTmp=true[Install] WantedBy=multi-user.target EOF systemctl enable nginx.service 1>/dev/null }nginx_start(){ TEMP_NGINX=$(mktemp nginx.XXX) if systemctl start nginx.service;thenecho "nginx start SUCCESS!"clearelinks http://localhost -dump >${TEMP_NGINX}head -n +11 ${TEMP_NGINX}rm -f ${TEMP_NGINX}echo -e "\e[1;36m Manager Nginx:\e[0m \e[0;32m systemctl start|stop|status|restart| nginx.service \e[0m" elseecho "nginx stop FAIL" fi}nginx_check nginx_install_pre nginx_install_make nginx_enable nginx_start運行腳本
[root@shell jao]# bash nginx.sh腳本運行成功效果圖:
可改變量:
三、監控一個主機狀態腳本
[root@shell jao]# cat monitor_host.sh #!/bin/bashfor ((i=1;i<4;i++));doif ping -c1 $1 &>/dev/null;thenexport ping_count"$i"=1elseexport ping_count"$i"=0fi# 時間間隔sleep 0.5 done# 3次ping 失敗報警 if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] && [ $ping_count1 -eq 0 ]thenecho "$1 is down"elseecho "$1 is up" fi# 取消上面定義的變量 unset ping_count1 unset ping_count2 unset ping_count3運行腳本
語法: bash monitor_host.sh 監控IP地址 [root@shell jao]# bash monitor_host.sh 192.168.1.68 192.168.1.68 is up [root@shell jao]# bash monitor_host.sh 192.168.1.69 192.168.1.69 is down四、統計內存、CPU使用前十進程腳本
[root@shell jao]# cat top10.sh #!/bin/bashmemory(){ #1、收集任務管理器進程信息 temp_file=$(mktemp memory.XXX) top -b -n1 > $temp_file # -b 顯示所有top內容 # -n1 動態顯示1次后退出#2、按進程統計內存使用大小 tail -n +8 $temp_file | awk '{array[$NF]+=$6}END{for (i in array) print array[i],i}'| sort -k 1 -n -r | head -10 rm -f $temp_file # -n +8 從第八行開始輸出內容 # sort -k 表示從第幾列排序 -r 倒序 -n 以數字類型排序 }cpu(){ #1、收集任務管理器進程信息 temp_file=$(mktemp cpu.XXX) top -b -n1 > $temp_file#2、按進程統計內存使用大小 tail -n +8 $temp_file | awk '{array[$NF]+=$9}END{for (i in array) print array[i],i}'| sort -k 1 -n -r | head -10 rm -f $temp_file }echo '----------memory----------' memoryecho '----------cpu----------' cpu執行腳本
[root@shell jao]# bash top10.sh ----------memory---------- 28684 firewalld 16572 tuned 14496 sshd 12888 polkitd 11316 NetworkManager 9380 bash 7084 ssh 6244 systemd 6120 vmtoolsd 6072 VGAuthService ----------cpu---------- 0 xfs-reclaim/sda 0 xfs-reclaim/dm- 0 xfs_mru_cache 0 xfs-log/sda1 0 xfs-log/dm-0 0 xfs-eofblocks/s 0 xfs-eofblocks/d 0 xfs-data/sda1 0 xfs-data/dm-0 0 xfs-conv/sda1五、I/O列長度監控腳本
[root@shell jao]# cat io.sh #!/bin/bash# 判斷系統是否安裝iostat scipt 基于iostat命令 [ ! -x /usr/bin/iostat ] && echo "iostat:command not found"&& exit 1 io(){#匹配以sd開頭的磁盤 如磁盤是vd,或其他開頭 這里直接改 Device_name變量即可。 Device_name=sd Device_length=`iostat -d -x | egrep "^$Device_name[[:alpha:]]|^dm" |wc -l`# tail -n +K 表示從第K個開始輸出 iostat -x 1 3 | egrep "^sd[[:alpha:]]|^dm" |tail -n +$((Device_length+1)) | awk '{io_long[$1]+=$9}END{for (i in io_long)print io_long[i],i}'echo "-----" }for ((i=1;i<6;i++));doio done運行腳本
[root@shell jao]# bash io.sh 0 dm-0 0 dm-1 0 sda ----- 0 dm-0 0 dm-1 0 sda ----- 0 dm-0 0 dm-1 0 sda ----- 0 dm-0 0 dm-1 0 sda ----- 0 dm-0 0 dm-1 0 sda -----六、計算內存使用率占比
[root@shell jao]# cat memoryinfo.sh #!/bin/bash# free參數詳解: total(總量),free(未使用),buff/cache(緩存),shared(共享內存),swap(交換分區) # buff和cache區別? # buff 做塊設備的緩沖大小 # cache 用來做文件的緩沖 MEM_TOTAL=`free -h |awk 'NR==2{print $2}'`MEM_USE=`free -h |awk 'NR==2{print $3}'` MEM_USED=`free | grep -i mem |awk '{print $3/$2*100"%"}'`MEM_FREE=`free -h |awk 'NR==2{print $4}'` MEM_FREED=`free | grep -i mem |awk '{print $4/$2*100"%"}'`MEM_BUFF_CACHE=`free -h |awk 'NR==2{print $6}'` MEM_BUFF_CACHED=`free | grep -i mem |awk '{print $6/$2*100 "%"}'`MEM_SHARE=`free -h |awk 'NR==2{print $5}'` MEM_SHARED=`free | grep -i mem |awk '{print $5/$2*100 "%"}'` MEM_ZONE=`head -3 /proc/meminfo | awk 'NR==1{t=$2}NR==2{f=$2;print(t-f)*100/t"%"}'`echo -e "\t\e[1;32m內存總量: $MEM_TOTAL \e[0m" echo -e "\t\e[1;32m內存總量減剩余內存(所有已使用)占比: $MEM_ZONE \e[0m" echo -e "\t\e[1;32m已使用: $MEM_USE 占比: $MEM_USED \e[0m" echo -e "\t\e[1;32m剩 余: $MEM_FREE 占比: $MEM_FREED \e[0m" echo -e "\t\e[1;32m共 享: $MEM_SHARE 占比: $MEM_SHARED \e[0m" echo -e "\t\e[1;32m緩沖區: $MEM_BUFF_CACHE 占比: $MEM_BUFF_CACHED \e[0m"執行腳本
[root@shell jao]# bash memoryinfo.sh 內存總量: 976M 內存總量減剩余內存(所有已使用)占比: 69.5806% 已使用: 130M 占比: 13.3869% 剩 余: 296M 占比: 30.4062% 共 享: 6.7M 占比: 0.691013% 緩沖區: 548M 占比: 56.2177%七、一鍵部署wordpress博客平臺
[root@shell jao]# cat wordpress.sh #!/bin/bash clear ## variabled WORDPRESS_VERSION='5.5.6' DB_ROOT_PWD='123.com' DB_USER='lisi' DB_USER_PWD='123.com' DB_NAME='blog'## function echo "Please wait a few minutes..." word_yum(){ (firewall-cmd --add-port=80/tcp --permanent) &>/dev/null (firewall-cmd --add-service=mysql --permanent;firewall-cmd --add-port=9000/tcp --permanent) &>/dev/null firewall-cmd --reload &>/dev/null (sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config;setenforce 0) &>/dev/null TIME_CURRENT=$(date +%X | awk '{print $1}') mkdir /etc/yum.repos.d/${TIME_CURRENT}.bak mv /etc/yum.repos.d/* /etc/yum.repos.d/${TIME_CURRENT}.bak &>/dev/null[ ! -x /usr/bin/wget ] && echo "wget: command not found" && exit 5if ! (wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null);thenecho "ERROR: wget CentOS-Base.repo fail"exit 5 fiif ! (wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null);thenecho "ERROR: wget epel.repo fail"exit 5 fi }word_nginx_install(){ cat > /etc/yum.repos.d/nginx.repo <<EOF [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/\$basearch/ gpgcheck=0 enabled=1 EOF[ ! -f /etc/yum.repos.d/nginx.repo ] && echo "ERROR: nginx.repo fail" && exit 5 yum clean all &>/dev/nullif (yum install nginx -y &>/dev/null);thenecho "nginx install success..."if systemctl start nginx &>/dev/null;thenecho "nginx start sucess..."if systemctl enable nginx &>/dev/null;thenecho "nginx enable success..."elseecho "nginx enable fail"exit 5fielseecho "nginx start fail"exit 5fi elseecho "nginx install fail"exit 5fi }word_php_install(){ if ! (rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm &>/dev/null);then echo "ERROR: epel-release-latest-7.noarch.rpm fail" fiif ! (rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm &>/dev/null);thenecho "ERROR: webtatic-release.rpm fail" fi yum clean all &>/dev/nullif (yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache &>/dev/null);thenif systemctl start php-fpm &>/dev/null;thenecho "php-fpm start success..."if systemctl enable php-fpm &>/dev/null;thenecho "php-fpm enable success..."elseecho "ERROR: php-fpm enable fail"exit 5fielseecho "ERROR: php-fpm start fail"exit 5fi elseecho "ERROR: install php fail"exit 5 fi}word_mysql_install(){if ! (rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm &>/dev/null);thenecho "ERROR: repo.mysql.com fail" fiif (yum install mysql-community-server -y &>/dev/null);thenecho "install mysql success..."if systemctl start mysqld &>/dev/null;thenecho "start mysql success..."if systemctl enable mysqld &>/dev/null;thenecho "enable mysql success..."elseecho "ERROR: enable mysqld fail"exit 5fielseecho "ERROR: start mysqld fail"exit 5fi elseecho "ERROR: install mysqld fail"exit 5 fimysql <<EOF set password for root@localhost=password('$DB_ROOT_PWD') EOFmysql -uroot -p$DB_ROOT_PWD -e "create database $DB_NAME" &>/dev/nullmysql -uroot -p$DB_ROOT_PWD -e "grant all on $DB_NAME.* to $DB_USER@localhost identified by '$DB_USER_PWD'" &>/dev/null }word_install(){ if ! wget https://cn.wordpress.org/wordpress-$WORDPRESS_VERSION-zh_CN.tar.gz &>/dev/null;thenecho "ERROR: install wordpress fail"exit 5 fi tar zxf wordpress-$WORDPRESS_VERSION-zh_CN.tar.gz -C / &>/dev/null chmod -R 777 /wordpress/ &>/dev/null }nginx_end(){ cat > /etc/nginx/conf.d/blog.conf <<EOF server {listen 80;server_name blog.benet.com;root /wordpress;index index.php index.html;location ~ \.php$ {root /wordpress;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;include fastcgi_params;}}EOF systemctl reload nginx &>/dev/null }echo_t1(){ clear echo -e "\e[1;36m 數據庫root用戶密碼:${DB_ROOT_PWD}\e[0m" echo -e "\e[1;36m 博客管理員:${DB_USER} 密碼:${DB_USER_PWD}\e[0m" echo -e "\e[1;36m 博客數據庫名字:${DB_NAME}\e[0m" echo echo -e "\e[1;36m http://本地IP地址 進行訪問博客首頁\e[0m" } word_yum word_nginx_install word_php_install word_mysql_install word_install nginx_end echo_t1總結
以上是生活随笔為你收集整理的进阶版Shell脚本合集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java时钟代码_JAVA实现时钟
- 下一篇: 微信公众号硬件开发杂谈