栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

进阶版Shell脚本合集

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

进阶版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 < $temp_file

# 3、分析文件中内容 判断结果

if egrep "^]" $temp_file &>/dev/null;then
    echo "$1 $2 open"
else
    echo "$1 $2 down"
fi

rm -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/bash

clear
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' ];then
   echo "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);then
	echo "ERROR:YUM install error"
	exit 5
fi
# 2、下载nginx源码包
if (wget http://nginx.org/download/${NGINX_VERSION}.tar.gz &>/dev/null);then
	tar zxf ${NGINX_VERSION}.tar.gz
	if [ ! -d ${NGINX_VERSION} ];then
		echo "ERROR:not found ${NGINX_VERSION}";exit 5 
	fi
else
	echo "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;then
	echo "nginx make ..."
    if make 1>/dev/null;then
		echo "nginx make install ..."
		if make install 1>/dev/null;then
			echo "nginx install success"
        else
			echo "ERROR: nginx install tail!";exit 5
		fi
	else
		echo "ERROR: nginx make tail!";exit 5
	fi
else
	echo"ERROR: nginx configure tail!";exit 5
fi
}

# 配置nginx开机自启,使用systemctl 管理nginx服务
nginx_enable(){
cat > /usr/lib/systemd/system/nginx.service </dev/null
}

nginx_start(){
TEMP_NGINX=$(mktemp nginx.XXX)
if systemctl start nginx.service;then
    echo "nginx start SUCCESS!"
    clear
    elinks 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"
else
    echo "nginx stop FAIL"
fi

}

nginx_check
nginx_install_pre
nginx_install_make
nginx_enable
nginx_start

运行脚本

[root@shell jao]# bash nginx.sh 

脚本运行成功效果图:
可改变量:

  6 NGINX_VERSION="nginx-1.16.0"           #定义安装nginx版本
  7 NGINX_INSTALL_DOC="/usr/local/nginx"   #定义安装目录
  8 NGINX_USER="nginx"                     #定义nginx用户
  9 NGINX_GROUP="nginx"                    #定义nginx组
 10 NGINX_CONFIGURE="...省略"               #定义nginx编译需要模块及参数!
 # 注意:在定义 NGINX_CONFIGURE变量模块时  需要安装此模块需要的依赖(如:with-http_ssl_module模块就需要依赖openssl 否则就会报错!)
三、监控一个主机状态脚本
[root@shell jao]# cat monitor_host.sh 
#!/bin/bash

for ((i=1;i<4;i++));do
    if ping -c1 $1 &>/dev/null;then
	export ping_count"$i"=1
    else
	export ping_count"$i"=0
    fi

# 时间间隔
    sleep 0.5
done

# 3次ping 失败报警
if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] && [ $ping_count1 -eq 0 ]
    then
	echo "$1 is down"
    else
	echo "$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/bash

memory(){
#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----------'
memory

echo '----------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++));do
        io
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 "te[1;32m内存总量: $MEM_TOTAL e[0m" 
echo -e "te[1;32m内存总量减剩余内存(所有已使用)占比: $MEM_ZONE e[0m"
echo -e "te[1;32m已使用: $MEM_USE  占比: $MEM_USED e[0m"
echo -e "te[1;32m剩  余: $MEM_FREE  占比: $MEM_FREED e[0m"
echo -e "te[1;32m共  享: $MEM_SHARE  占比: $MEM_SHARED e[0m"
echo -e "te[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 5

if ! (wget -O /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null);then
	echo "ERROR: wget CentOS-base.repo fail"
	exit 5
fi

if ! (wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null);then
	echo "ERROR: wget epel.repo fail"
	exit 5
fi
}

word_nginx_install(){
cat > /etc/yum.repos.d/nginx.repo </dev/null

if (yum install nginx -y &>/dev/null);then
	echo "nginx install success..."
	if systemctl start nginx &>/dev/null;then
		echo "nginx start sucess..."
		if systemctl enable nginx &>/dev/null;then
			echo "nginx enable success..."
		else
			echo "nginx enable fail"
			exit 5
		fi
	else
		echo "nginx start fail"
		exit 5
	fi
else
	echo "nginx install fail"
	exit 5

fi
}

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"
fi

if ! (rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm &>/dev/null);then
	echo "ERROR: webtatic-release.rpm fail"
fi
yum clean all &>/dev/null

if  (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);then
	if systemctl start php-fpm &>/dev/null;then
		echo "php-fpm start success..."
		if systemctl enable php-fpm &>/dev/null;then
			echo "php-fpm enable success..."
		else
			echo "ERROR: php-fpm enable fail"
			exit 5
		fi
	else
		echo "ERROR: php-fpm start fail"
		exit 5
	fi
else
	echo "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);then
	echo "ERROR: repo.mysql.com fail"
fi

if (yum install mysql-community-server -y &>/dev/null);then
	echo "install mysql success..."
	if systemctl start mysqld &>/dev/null;then
		echo "start mysql success..."
		if systemctl enable mysqld &>/dev/null;then
		echo "enable mysql success..."
		else
			echo "ERROR: enable mysqld fail"
			exit 5
		fi
	else
		echo "ERROR: start mysqld fail"
		exit 5
	fi
else
	echo "ERROR: install mysqld fail"
	exit 5
fi

mysql  </dev/null

mysql -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;then
	echo "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 </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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/333441.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号