- 1. 一个Hello world脚本
- 2. 通过位置变量创建 Linux 系统账户及密码
- 3.备份日志
- 4.一键部署 LNMP(RPM 包版本)
- 5.查看内存和磁盘容量,小于给定值时报警
- 6、猜数字游戏
- 7. 检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员
- 8.检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员(使用UID对比)
- 9.编写创建用户脚本
- 10.输入三个数并进行升序排序
- 11.石头、剪刀、布游戏
- 12.编写脚本测试 192.168.11.90/24 11.110/24 网段中哪些主机处于开机状态,哪些主机处于关机状态(for)
- 13.编写脚本测试 192.168.11.90/24 11.110/24 网段中哪些主机处于开机状态,哪些主机处于关机状态(while)
- 14.编写脚本测试 192.168.11.90/24 11.110/24 网段中哪些主机处于开机状态,哪些主机处于关机状态(多进程版)
- 15、编写脚本,显示进度条
- 16、进度条,动态时针版本;定义一个显示进度的函数,屏幕快速显示| / ‐ \
- 17、9*9 乘法表
- 18 、使用死循环实时显示 ens33 网卡发送的数据包流量
- 19、使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码本脚本执行,需要提前准备一个 user.txt 文件,该文件中包含有若干用户名信息
- 20、编写批量修改扩展名脚本
- 21、使用 expect 工具自动交互密码远程其他主机安装 httpd 软件
- 22、一键部署 LNMP(源码安装版本)
- 23、点名器脚本
- 24、查看有多少远程的 IP 在连接本机
- 25、对 100 以内的所有正整数相加求和(1+2+3+4…+100)
- 26、统计 20:00 到 20:30 所有访问 nginx 服务器的请求有多少个
- 27、统计 20:00 到 20:30 所有访问本机nginx 服务器的远程 IP 地址是什么
- 28、打印国际象棋棋盘
- 29、统计每个远程 IP 访问了本机nginx 几次?
- 30、统计当前 Linux 系统中可以登录计算机的账户有多少个
- 31、统计/var/log 有多少个文件,并显示这些文件名
- 32、自动为其他脚本添加解释器信息
- 33、自动化部署varnish 源码包软件
- 34、编写nginx启动脚本
- 35、自动对磁盘分区、格式化、挂载
- 36、自动优化 Linux 内核参数
- 37、切割 Nginx 日志文件(防止单个文件过大,后期处理很困难)
- 38、检测mysql数据库连接数量
- 39、根据 md5 校验码,检测文件是否被修改
- 40、检测 MySQL 服务是否存活
- 41、备份 MySQL 的 shell 脚本(mysqldump版本)
- 42、将文件中所有的小写字母转换为大写字母
- 43、非交互自动生成 SSH 密钥文件
- 44.检查特定的软件包是否已经安装
- 45、监控 HTTP 服务器的状态(测试返回码)
- 46、自动添加防火墙规则,开启某些服务或端口(适用于 RHEL7)
- 47、使用脚本自动创建逻辑卷
- 48、显示 CPU 厂商信息
- 49、删除某个目录下大小为 0 的文件
- 50、查找 Linux 系统中的僵尸进程
1. 一个Hello world脚本
#/bin/bash
#一个hello world脚本
echo "Hello World!!!"
[root@localhost exerecise]# sh 1.sh
Hello World!!!
2. 通过位置变量创建 Linux 系统账户及密码
#/bin/bash
useradd $1
echo "$2" | passwd --stdin "$1"
[root@localhost exerecise]# sh 2.sh jh jh
更改用户 jh 的密码 。
passwd:所有的身份验证令牌已经成功更新。
3.备份日志
#/bin/bash
tar -czf log-`date +%Y%m%d`.tar.gz /var/log
[root@localhost exerecise]# sh 3.sh
[root@localhost exerecise]# ls
1.sh 2.sh 3.sh log-20220613.tar.gz
4.一键部署 LNMP(RPM 包版本)
#/bin/bash
yum install nginx
yum install mariadb mariadb‐devel mariadb‐server
yum install php php-fpm php-mysql
systemctl start nginx mariadb php-fpm
5.查看内存和磁盘容量,小于给定值时报警
#!/bin/bash
disk_size=$(df / | awk '/\//{print $4}')
mem_size=$(free | awk '/Mem/{print $4}')
while :
do
# 磁盘空间小于1G # 内存小于500M
if [ $disk_size -le 1024000 -a $mem_size -le 512000 ];then
echo "资源不足"
fi
sleep 10
done
# 每隔10s提升一次
[root@localhost ~]# sh 5.sh ~
[root@localhost ~]#
[root@localhost ~]#资源不足
[root@localhost ~]#资源不足
[root@localhost ~]#资源不足
[root@localhost ~]#资源不足
6、猜数字游戏
#/bin/bash
num=$[RAND0M%100+50]
echo "$num"
while :
do
read -p "计算机生成了一个 1-100 的随机数,你猜:" cai
if [ $cai -eq $num ];then
exit
elif [ $cai -gt $num ];then
echo "猜大了 再猜"
elif [ $cai -lt $num ];then
echo "猜小了"
fi
done
[root@localhost exerecise]# sh 6.sh
50
计算机生成了一个 1-100 的随机数,你猜:40
猜小了
计算机生成了一个 1-100 的随机数,你猜:49
猜小了
计算机生成了一个 1-100 的随机数,你猜:51
猜大了 再猜
计算机生成了一个 1-100 的随机数,你猜:50
[root@localhost exerecise]#
7. 检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员
#/bin/bash
install_vsftp=`yum -y install vsftpd`
login_user=`id -un`
if [ $login_user == "root" ];then
$install_vsftp
else
echo "您不是管理员,没有权限安装软件"
fi
[root@localhost exerecise]# su a1
[a1@localhost exerecise]$ sh 7.sh
您不是管理员,没有权限安装软件
8.检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员(使用UID对比)
#/bin/bash
install_vsftp=`yum -y install vsftpd`
if [ $UID -eq 0 ];then
$install_vsftp
else
echo "您不是管理员,没有权限安装软件"
fi
[root@localhost exerecise]# su a2
[a2@localhost exerecise]$ sh 8.sh
您需要 root 权限执行此命令。
您不是管理员,没有权限安装软件
9.编写创建用户脚本
提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默认的 123456 作为默认密码。
#/bin/bash
read -p "请输入用户名 :" user
if [ -z $user ];then
echo "您不需要输入用户名"
exit 2
fi
stty -echo
pass=${pass:-123456}
useradd "$user"
echo "$pass" | passwd --stdin "$user"
[root@localhost exerecise]# sh 9.sh
请输入用户名 :sa
更改用户 sa 的密码 。
passwd:所有的身份验证令牌已经成功更新。
10.输入三个数并进行升序排序
#!/bin/bash
read -p "请输入一个整数:" num1
read -p "请输入一个整数:" num2
read -p "请输入一个整数:" num3
tmp=0
if [ $num1 -gt $num2 ];then
tmp=$num1
num1=$num2
num2=$tmp
fi
if [ $num1 -gt $num3 ];then
tmp=$num1
num1=$num3
num3=$tmp
fi
if [ $num2 -gt $num3 ];then
tmp=$num2
num2=$num3
num3=$tmp
fi
echo "排序后数据(从小到大)为:$num1,$num2,$num3"
[root@localhost exerecise]# sh 10.sh
请输入一个整数:1
请输入一个整数:2
请输入一个整数:10
排序后数据(从小到大)为:1,2,10
[root@localhost exerecise]# sh 10.sh
请输入一个整数:3
请输入一个整数:2
请输入一个整数:1
排序后数据(从小到大)为:1,2,3
11.石头、剪刀、布游戏
#!/bin/bash
game=(石头 剪刀 布)
num=$[RANDOM%3]
computer=${game[$num]}
echo "请根据下列提示选择您的出拳手势"
echo "1.石头"
echo "2.剪刀"
echo "3.布"
read -p "请选择 1‐3:" person
case $person in
1)
if [ $num -eq 0 ]
then
echo "平局"
elif [ $num -eq 1 ]
then
echo "你赢"
else
echo "计算机赢"
fi;;
2)
if [ $num -eq 0 ]
then
echo "计算机赢"
elif [ $num -eq 1 ]
then
echo "平局"
else
echo "你赢"
fi;;
3)
if [ $num -eq 0 ]
then
echo "你赢"
elif [ $num -eq 1 ]
then
echo "计算机赢"
else
echo "平局"
fi;;
*)
echo "必须输入 1‐3 的数字"
esac
[root@localhost exerecise]# sh 11.sh
请根据下列提示选择您的出拳手势
1.石头
2.剪刀
3.布
请选择 1‐3:3
计算机赢
[root@localhost exerecise]# sh 11.sh
请根据下列提示选择您的出拳手势
1.石头
2.剪刀
3.布
请选择 1‐3:1
计算机赢
[root@localhost exerecise]# sh 11.sh
请根据下列提示选择您的出拳手势
1.石头
2.剪刀
3.布
请选择 1‐3:1
平局
12.编写脚本测试 192.168.11.90/24 11.110/24 网段中哪些主机处于开机状态,哪些主机处于关机状态(for)
#!/bin/bash
for i in {90..110}
do
ping ‐c 2 ‐i 0.3 ‐W 1 192.168.11.$i &>/dev/null
if [ $? -eq 0 ];then
echo "192.168.11.$i is up"
else
echo "192.168.11.$i is down"
fi
done
[root@localhost exerecise]# sh 12.sh #等太久不再做测试
192.168.11.90 is down
192.168.11.91 is down
13.编写脚本测试 192.168.11.90/24 11.110/24 网段中哪些主机处于开机状态,哪些主机处于关机状态(while)
#!/bin/bash
i=90
while [ $i -le 110 ]
do
ping -c 2 -i 0.3 -W 1 192.168.11.$i &>/dev/null
if [ $? -eq 0 ];then
echo "192.168.11.$i is up"
else
echo "192.168.11.$i is down"
fi
let i++
done
[root@localhost exerecise]# sh 13.sh
192.168.11.90 is down
192.168.11.91 is down
192.168.11.92 is down
192.168.11.93 is down
192.168.11.94 is down
192.168.11.95 is down
192.168.11.96 is down
192.168.11.97 is down
192.168.11.98 is down
192.168.11.99 is down
192.168.11.100 is down
192.168.11.101 is up
192.168.11.102 is up
192.168.11.103 is down
192.168.11.104 is down
192.168.11.105 is up
192.168.11.106 is down
192.168.11.107 is down
192.168.11.108 is down
192.168.11.109 is down
192.168.11.110 is down
14.编写脚本测试 192.168.11.90/24 11.110/24 网段中哪些主机处于开机状态,哪些主机处于关机状态(多进程版)
#!/bin/bash
myping(){
ping -c 2 -i 0.3 -W 1 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "$1 is up"
else
echo "$1 is down"
fi
}
for i in {90..110}
do
myping 192.168.11.$i &
done
[root@localhost exerecise]# sh 14.sh 192.168.11.101
[root@localhost exerecise]# 192.168.11.101 is up
192.168.11.102 is up
192.168.11.105 is up
192.168.11.90 is down
192.168.11.107 is down
192.168.11.91 is down
192.168.11.92 is down
192.168.11.103 is down
192.168.11.100 is down
192.168.11.106 is down
192.168.11.95 is down
192.168.11.93 is down
192.168.11.108 is down
192.168.11.98 is down
192.168.11.99 is down
192.168.11.94 is down
192.168.11.110 is down
192.168.11.97 is down
192.168.11.96 is down
192.168.11.109 is down
192.168.11.104 is down
15、编写脚本,显示进度条
#!/bin/bash
jindu(){
while :
do
echo -n '#'
sleep 0.5
done
}
jindu &
cp -a $1 $2
ps -ef | grep 15.sh | grep -v grep | awk '{print $2}' | xargs kill
echo "拷贝完成"
[root@localhost exerecise]# sh 15.sh /work/packages/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz /data/jhl/
############################################已终止
[root@localhost exerecise]# tree /data/jhl/
/data/jhl/
├── 12.sh
├── 13.sh
└── mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
0 directories, 3 files
16、进度条,动态时针版本;定义一个显示进度的函数,屏幕快速显示| / ‐ \
#!/bin/bash
rotate_line(){
INTERVAL=0.5
COUNT="0"
while :
do
COUNT=`expr $COUNT + 1`
case $COUNT in
"1")
echo -e '‐'"\b\c"
sleep $INTERVAL
;;
"2")
echo -e '\\'"\b\c"
sleep $INTERVAL
;;
"3")
echo -e "|\b\c"
sleep $INTERVAL
;;
"4")
echo -e "/\b\c"
sleep $INTERVAL
;;
*)
COUNT="0";;
esac
done
}
rotate_line
[root@localhost ~]# sh 16.sh
/
\
|
‐
/
/
|
\
/
\
/
‐
\
^C
[root@localhost ~]#
17、9*9 乘法表
#!/bin/bash
for i in `seq 9`
do
for j in `seq $i`
do
echo -n "$j*$i=$[i*j] "
done
echo
done
[root@localhost ~]# sh 17.sh
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
[root@localhost ~]#
18 、使用死循环实时显示 ens33 网卡发送的数据包流量
#!/bin/bash
while :
do
echo '本地网卡 eth0 流量信息如下: '
ifconfig ens33 | grep "RX pack" | awk '{print $5}'
ifconfig ens33 | grep "TX pack" | awk '{print $5}'
sleep 1
done
[root@localhost ~]# sh 18.sh
本地网卡 ens33 流量信息如下:
256721
851360
本地网卡 ens33 流量信息如下:
256781
851686
本地网卡 ens33 流量信息如下:
256901
852012
本地网卡 ens33 流量信息如下:
257021
852338
19、使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码本脚本执行,需要提前准备一个 user.txt 文件,该文件中包含有若干用户名信息
#!/bin/bash
for i in `cat user.txt`
do
useradd $i
echo "123456" | passwd ‐‐stdin $i
done
[root@localhost ~]# cat user.txt
as
aq
aw
ar
at
ay
[root@localhost ~]# sh 19.sh
[root@localhost ~]# cat /etc/passwd
as:x:1001:1001::/home/as:/bin/bash
aq:x:1002:1002::/home/aq:/bin/bash
aw:x:1003:1003::/home/aw:/bin/bash
ar:x:1004:1004::/home/ar:/bin/bash
at:x:1005:1005::/home/at:/bin/bash
ay:x:1006:1006::/home/ay:/bin/bash
20、编写批量修改扩展名脚本
#!/bin/bash
for i in `ls *.$1`
do
mv $i ${i%.*}.$2
done
[root@localhost ~]# touch a.txt
[root@localhost ~]# touch b.txt
[root@localhost ~]# touch c.txt
[root@localhost ~]# sh 20.sh txt doc
[root@localhost ~]# ls
a.doc anaconda-ks.cfg b.doc c.doc user.doc work.zip
21、使用 expect 工具自动交互密码远程其他主机安装 httpd 软件
#!/bin/bash
rm -rf ~/.ssh/known_hosts
ls ~/.ssh/known_hosts
if [ $? -eq 0 ];then
expect <<EOF
spawn ssh 192.168.11.102
expect "yes/no" {send "yes\r"}
expect "123" {send "192.168.11.102\r"}
expect "#" {send "yum -y install httpd\r"}
expect "#" {send "exit\r"}
EOF
else
echo "konwn_hosts未删除"
fi
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
[root@localhost ~]# exit
登出
Connection to 192.168.11.102 closed.
22、一键部署 LNMP(源码安装版本)
#/!bin/bash
#+++++++++++++++++++++++++++++++++++++++ 脚本信息 +++++++++++++++++++++++++++++++++++++++#
##################################################
# #
# System: centos7 #
# Version: v2.0 #
# Description: System init and install LNMP #
# Author: Sand #
# Mail: sandbenene@163.com #
# Date: 2022.6.16 #
# #
##################################################
###############################################################
### 注意点:运行脚本需要在脚本所在路径,系统初始环境必须干净
### 安装包存放位置 :~/package
### 配置文件存放位置 :~/config
### test : sh -vx system_init.sh
### grep -v '^$|^;' etc/php.ini
#################### 要求 #####################################
### 1,JDK 1.8.0_91
### 2,mysql 5.7.30 不对外访问
### 3,redis 5.0.14 不对外访问
### 4,php 7.3.32
### 5,nginx 1.20.2
###
### JDK 网络下载地址 : https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
### nginx 网络获取安装包: wget http://nginx.org/download/nginx-1.20.2.tar.gz
### php 网络获取安装包: wget https://www.php.net/distributions/php-7.3.32.tar.gz
### mysql 网络下载地址 : wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
### redis 网络下载安装包:wget https://download.redis.io/releases/redis-5.0.14.tar.gz
###
##############################################
### [root@localhost ~]# tree auto
### auto
#======================================= 脚本通用变量 =======================================#
filedir=$(cd `dirname $0`;pwd)
packages=$filedir/packages
config=$filedir/config
nginx_version=nginx-1.22.0
nginx_install_path=/usr/local/nginx
mysql_version=mysql-5.7.36
mysql_install_path=/usr/local/mysql
redis_version=redis-5.0.14
redis_install_path=/usr/local/redis
php_version=php-7.3.30
php_install_path=/usr/local/php
#======================================== 脚本插件 =======================================#
#################### 插件一:字体 ######################
### 设置文件输出警告色,info/warning/error "你要输出信息"
### 使用方法
### info "This is a info!"
### warnning "This is a warn"
### error "This is a error"
function info(){
# 32 绿色字
SAND=${1:-'没有传参'}
echo -e "\033[1;32mINFO: $SAND\033[0m"
}
function warnning(){
# 33 黄色字
SAND=${1:-'没有传参'}
echo -e "\033[1;33mWARN: $SAND\033[0m"
}
function error(){
# 31 红色字
SAND=${1:-'没有传参'}
echo -e "\033[1;5;41;37mERROR: $SAND\033[0m"
}
#################### 插件二:判断服务是否安装 ###########
function judge_server_install(){
NO_install=0
if [ -f $SERVERFILE ]; then
# NO_install 需不需要安装,需要安装=0 不用安装=1
NO_install=1
SERVERPID=`ps -C ${SERVERNAME} |awk -F' ' '{ if(NR==2) print $1 }'`
PID=`ss -ltnp | grep "pid=${SERVERPID}," |wc -l`
if [[ 1 -le "${PID}" ]]; then
info " ------------------------ ${SERVERNAME} \t当前状态: 已安装并已启动"
warnning " ------------------------ ${SERVERNAME} \t取消安装中...\n"
else
info " ------------------------ ${SERVERNAME} \t当前状态: 已安装但未启动 \033[1;33m[x]\033[0m"
warnning " ------------------------ ${SERVERNAME} \t取消安装中...\n"
fi
else
info " ------------------------ ${SERVERNAME} \t当前状态:未安装\n"
fi
}
#======================================== 功能模块 =======================================#
#################### 功能一:安装docker ####################
function install_docker(){
info "检测docker是否安装... -_-"
sleep 0.5
SERVERNAME=dockerd
SERVERFILE=/bin/docker
judge_server_install
if [ ! $NO_install -eq 1 ]; then
info "1. 卸载部分已安装软件"
yum -y remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine &>/dev/null
info "2. 安装yum-config-manager和yum-utils"
yum install -y yum-utils >&/dev/null
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo &>/dev/null
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
info "3. 安装20.10版本Docker"
yum -y install docker-ce-20.10.8-3.el7 docker-ce-cli-20.10.8-3.el7 containerd.io &>/dev/null
info "4. 配置加速源,配置ipv4路由转发"
[ ! -d /etc/docker ] && mkdir /etc/docker && touch /etc/docker/daemon.json
echo -e "{ \n \"registry-mirrors\": [\n \"https://hub-mirror.c.163.com\",\n \"https://mirror.baidubce.com\" \n ]\n}" >/etc/docker/daemon.json
info "5. 安装Docker-Compose"
if [ ! -f /usr/local/bin/docker-compose ]; then
curl -L https://download.fastgit.org/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m`> /usr/local/bin/docker-compose &>/dev/null
chmod +x /usr/local/bin/docker-compose
fi
info "6. 启动docker"
systemctl enable --now docker &>/dev/null
sleep 1
# 检查是否启动成功
status=`ps -C dockerd --no-header |wc -l`
if [[ $status -ne 0 ]];then
info "7. docker启动成功 [√] \n"
else
error "7. docker启动失败 [x] "
echo ""
fi
fi
}
#################### 功能二:安装zabbix_agent ##############
function install_zabbix_agent(){
info "检测zabbix-agent2是否安装... ^o^"
sleep 0.5
SERVERNAME=zabbix_agent2
SERVERFILE=/etc/zabbix/zabbix_agent2.conf
judge_server_install
if [ ! $NO_install -eq 1 ]; then
info "1. 获取zabbix源,请耐心等待..."
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm &>/dev/null
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#g' /etc/yum.repos.d/zabbix.repo
sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/zabbix.repo
info "2. 安装zabbix-agent2"
yum -y install zabbix-agent2 &>/dev/null
info "3. 配置zabbix客户端..."
host=`hostname -I`
sed -i 's/Server=127.0.0.1/Server=172.16.16.11/g' /etc/zabbix/zabbix_agent2.conf
sed -i "s/Hostname=Zabbix server/Hostname=${host}/g" /etc/zabbix/zabbix_agent2.conf
# sed -i 's/# ListenPort=10050/ListenPort=19991/g' /etc/zabbix/zabbix_agent2.conf
info "4. 启动zabbix-agent2服务"
systemctl enable --now zabbix-agent2 &>/dev/null
sleep 1
# 检查是否启动成功
status=`ss -antl |grep 10050 |wc -l`
if [[ $status -gt 0 ]];then
info "5. zabbix-agent启动成功 [√] \n"
else
error "5. zabbix-agent启动失败 [x] "
echo ""
fi
fi
}
#################### 功能三:安装nginx #####################
function install_nginx(){
info "检测nginx是否安装... +>_<+"
sleep 0.5
SERVERNAME=nginx
SERVERFILE=/usr/local/nginx/sbin/nginx
judge_server_install
if [ ! $NO_install -eq 1 ];then
info "1. 创建www用户"
user_www=`cat /etc/passwd | cut -f1 -d':' | grep -w www -c`
if [ $user_www -le 0 ]; then
groupadd www ; useradd -M -g www -s /sbin/nologin www
fi
info "2. 解决依赖"
yum install -y gcc gcc-c++ gd gd-devel glibc glibc-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel wget &>/dev/null
info "3. 下载nginx"
if [ ! -f $packages/$nginx_version.tar.gz ]; then
wget -O $packages/$nginx_version.tar.gz https://nginx.org/download/$nginx_version.tar.gz &>/dev/null
fi
info "4. 解压安装包..."
cd $packages && tar -xf $packages/$nginx_version.tar.gz
info "5. 编译nginx 请等待..."
cd $packages/$nginx_version
./configure --prefix=/usr/local/nginx/ --user=www --group=www --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log &>/dev/null
make &>/dev/null && make install &>/dev/null
info "6. 设置systemd管理"
ln -s $nginx_install_path/sbin/* /usr/local/sbin/
ln -s $nginx_install_path/sbin/* /usr/sbin/
\cp -f $config/nginx/nginx.service /usr/lib/systemd/system/nginx.service
info "7. 配置nginx"
mkdir -p /usr/local/nginx/logs/error/80/
mkdir -p /usr/local/nginx/conf/vhost/
\cp -f $config/nginx/nginx.conf $nginx_install_path/conf/nginx.conf
\cp -f $config/nginx/default.conf $nginx_install_path/conf/vhost/default.conf
warnning "文件/usr/local/nginx/conf/vhost/default.conf 及时更改root位置,及文件名"
info "8. 启动nginx"
systemctl enable --now nginx &>/dev/null
sleep 1
# 检查是否启动成功
status=`ps -C nginx --no-header |wc -l`
if [[ $status -ne 0 ]];then
info "9. nginx启动成功 [√] \n"
else
error "9. nginx启动失败 [x] "
echo ""
fi
fi
}
#################### 功能四:安装mysql #####################
function install_mysql(){
info "检测mysql是否安装... :)"
sleep 0.5
SERVERNAME=mysqld
SERVERFILE=/usr/local/mysql/bin/mysqld
judge_server_install
if [ ! $NO_install -eq 1 ];then
info "1. 解决依赖"
yum -y remove `rpm -qa | grep mariadb` &>/dev/null
yum -y install libaio-* &>/dev/null
yum -y install numactl &>/dev/null
info "2. 创建mysql用户"
UserAdd=`cat /etc/passwd | cut -f1 -d':' | grep -w mysql -c`
if [ $UserAdd -le 0 ]; then
groupadd mysql && useradd -M -s /sbin/nologin -r -g mysql mysql
fi
info "3. 解压,安装"
tar -xf $packages/$mysql_version-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
mv /usr/local/$mysql_version-linux-glibc2.12-x86_64/ $mysql_install_path
# 设置mysql目录权限
chown -R mysql.mysql $mysql_install_path
info "4. 设置systemd管理"
ln -s /usr/local/mysql/bin/* /bin/
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile && source /etc/profile
cd /usr/lib/systemd/system && touch mysqld.service && chmod 644 mysqld.service
cp $config/mysql/mysqld.service /etc/systemd/system/mysqld.service
cd /usr/lib/tmpfiles.d && touch mysql.conf && chmod 644 mysql.conf
echo 'd /usr/local/mysql/data 0750 mysql mysql -' > /usr/lib/tmpfiles.d/mysql.conf
info "5. 配置服务器启动选项,初始化数据目录"
cp $config/mysql/my.cnf /etc/my.cnf
mkdir -p /data/mysql/mysql3306/{data,logs,tmp}
chown -R mysql:mysql /data/mysql/
info "6. 初始化MySQL"
cd /usr/local/mysql
./bin/mysqld --defaults-file=/etc/my.cnf --initialize &>/dev/null
warnning "----请及时修改密码,密码:【SET PASSWORD = PASSWORD('lnmp@123');】"
warnning `grep "temporary password" /data/mysql/mysql3306/data/error.log |awk '{print "默认密码是: " $(NF-1)" "$NF}'`
info "7. 启动mysql"
systemctl enable --now mysqld &>/dev/null
info "8. 监测mysql是否启动..." ; sleep 0.5
status=`ps -C mysqld --no-header |wc -l`
if [[ $status -ne 0 ]];then
info "9. mysql启动成功 [√] \n"
else
error "9. mysql启动失败 [x] "
echo ""
fi
fi
}
#################### 功能五:安装redis #####################
function install_redis(){
info "检测redis是否安装... o_O"
sleep 0.5
SERVERNAME=redis-server
SERVERFILE=/usr/local/redis/bin/redis-server
judge_server_install
if [ ! $NO_install -eq 1 ];then
info "1. 创建redis用户"
useradd -M -s /sbin/nologin -r redis
info "2. 解压redis安装包..."
tar -xf $packages/$redis_version.tar.gz -C /usr/local/
mv /usr/local/$redis_version $redis_install_path
cd $redis_install_path
info "3. 解决依赖"
yum -y install gcc gcc-c++ &>/dev/null
info "4. 安装redis..."
make &>/dev/null && make install PREFIX=$redis_install_path &>/dev/null
info "5. 设置systemd管理"
cp $redis_install_path/src/redis-server /usr/local/bin/
cp $redis_install_path/src/redis-cli /usr/local/bin/
\cp -f $config/redis/redis.service /etc/systemd/system/redis.service
info "6. 配置启动redis..."
\cp -f $config/redis/redis.conf $redis_install_path/
systemctl enable --now redis &>/dev/null
info "7. 监测redis是否启动..." ; sleep 0.5
status=`ps -C redis-server --no-header |wc -l`
if [[ $status -ne 0 ]];then
info "8. redis启动成功 [√] \n"
else
error "8. redis启动失败 [x] "
echo ""
fi
fi
}
#################### 功能六:安装php #######################
function install_php(){
info "检测php7是否安装... (-_-;)"
sleep 0.5
SERVERNAME=php-fpm
SERVERFILE=/usr/local/php/sbin/php-fpm
judge_server_install
if [ ! $NO_install -eq 1 ];then
info "1. 解决依赖"
yum -y install libxslt-devel bxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libzip-devel pcre-devel &>/dev/null
info "2. 创建www用户"
id www &>/dev/null
if [ ! $? -eq 0 ]; then
useradd -M -s /sbin/nologin www
fi
info "3. 解压安装包"
cd $packages && tar -xf $php_version.tar.gz
info "4. 正在安装PHP..."
cd $packages/php-7.3.30
./configure --prefix=$php_install_path --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-pdo-mysql --with-openssl --with-pcre-regex --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml >&/dev/null
info "5. 正在编译php7"
make -j 5 &>/dev/null && make install &>/dev/null
info "6. 设置命令路径优化"
ln -s $php_install_path/sbin/php-fpm /usr/sbin/
ln -s $php_install_path/bin/* /bin/
info "7. 配置php7"
cp $php_install_path/etc/{php-fpm.conf.default,php-fpm.conf}
cp $php_install_path/etc/php-fpm.d/{www.conf.default,www.conf}
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
cp php.ini-production $php_install_path/etc/php.ini
sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#' /usr/local/php/etc/php.ini
sed -i 's#max_execution_time = .*#max_execution_time = 300#' /usr/local/php/etc/php.ini
sed -i 's#post_max_size =.*#post_max_size = 32M#' /usr/local/php/etc/php.ini
sed -i 's#max_input_time = .*#max_input_time = 300#' /usr/local/php/etc/php.ini
info "8. 启动php-fpm"
systemctl enable --now php-fpm &>/dev/null
status=`ps -C php-fpm --no-header |wc -l`
if [[ $status -ne 0 ]];then
info "9. php-fpm启动成功 [√] \n"
else
error "9. php-fpm启动失败 [x] "
echo ""
fi
fi
}
#################### 功能七:安装jdk8 ######################
function install_jdk(){
info "检测jdk1.8是否安装... |*(*|"
sleep 0.5
SERVERNAME=java
SERVERFILE=/usr/local/java/bin/java
judge_server_install
if [ ! $NO_install -eq 1 ];then
info "1. 正在解压安装包..."
cd $packages && tar -xf jdk-8u91-linux-x64.tar.gz
mv jdk1.8.0_91/ /usr/local/java
info "2. 配置JAVA环境变量"
echo '### auto/config/jdk/java.sh' >/etc/profile.d/java.sh
echo 'export JAVA_HOME=/usr/local/java' >>/etc/profile.d/java.sh
echo 'export PATH=$PATH:$JAVA_HOME/bin' >>/etc/profile.d/java.sh
echo 'export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >>/etc/profile.d/java.sh
echo 'export JRE_HOME=$JAVA_HOME/jre' >>/etc/profile.d/java.sh
warnning "3. 你需要手动执行一条命令【source /etc/profile】"
source /etc/profile
if command -v javac >/dev/null ; then
info "4. JDK8环境安装成功\n"
else
error "4. JDK安装失败"
echo ""
fi
fi
}
#################### 合集:其他环境 ########################
function install_lnmp(){
install_nginx
install_mysql
install_redis
install_php
}
function install_all(){
install_zabbix_agent
install_jdk
install_docker
install_nginx
install_mysql
install_redis
install_php
}
#======================================== main =======================================#
function main(){
INSTALL=${1:-'info'} # 默认值
# 判断输入安装服务
if [ $INSTALL == '1' -o $INSTALL == 'init' ]; then
source $filedir/init.sh
elif [ $INSTALL == '2' -o $INSTALL == 'docker' ]; then
install_docker
elif [ $INSTALL == '3' -o $INSTALL == 'java' -o $INSTALL == 'jdk' ]; then
install_jdk
elif [ $INSTALL == '4' -o $INSTALL == 'zabbix-agent' ]; then
install_zabbix_agent
elif [ $INSTALL == '5' -o $INSTALL == 'nginx' ]; then
install_nginx
elif [ $INSTALL == '6' -o $INSTALL == 'mysql' ]; then
install_mysql
elif [ $INSTALL == '7' -o $INSTALL == 'redis' ]; then
install_redis
elif [ $INSTALL == '8' -o $INSTALL == 'php' ]; then
install_php
elif [ $INSTALL == '9' -o $INSTALL == 'lnmp' ]; then
install_lnmp
elif [ $INSTALL == '10' -o $INSTALL == 'all' ]; then
install_all
elif [ $INSTALL == '0' -o $INSTALL == 'info' ]; then
source $filedir/info.sh
info "请输入 0.info"
info "1.init 2.docker 3.java 4.zabbix-agent 5.nginx"
info "6.mysql 7.redis 8.php 9.lnmp 10.all"
fi
}
main $1
[root@localhost auto]# ./main.sh 9
INFO: 检测nginx是否安装... +>_<+
INFO: ------------------------ nginx 当前状态: 已安装但未启动 [x]
WARN: ------------------------ nginx 取消安装中...
INFO: 检测mysql是否安装... :)
INFO: ------------------------ mysqld 当前状态: 已安装并已启动
WARN: ------------------------ mysqld 取消安装中...
INFO: 检测redis是否安装... o_O
INFO: ------------------------ redis-server 当前状态: 已安装并已启动
WARN: ------------------------ redis-server 取消安装中...
INFO: 检测php7是否安装... (-_-;)
INFO: ------------------------ php-fpm 当前状态: 已安装并已启动
WARN: ------------------------ php-fpm 取消安装中...
23、点名器脚本
[root@localhost auto]# cat 23.sh
#!/bin/bash
while :
do
line=`cat user.txt | wc -l`
num=$[RANDOM%line+1]
sed -n "${num}p" user.txt
sleep 0.8
clear
done
[root@localhost auto]# sh 23.sh
fds
bc
sad
fds
fds
sad
vxc
24、查看有多少远程的 IP 在连接本机
#!/bin/bash
netstat -ant |awk '{print $5}' |awk '{print $1}' |sort -nr |uniq -c
[root@localhost auto]# cat 24.sh
#!/bin/bash
netstat -ant |awk '{print $5}' |awk '{print $1}' |sort -nr |uniq -c
[root@localhost auto]# sh 24.sh
1 192.168.11.254:62008
1 192.168.11.254:62007
1 and
1 Address
3 0.0.0.0:*
3 :::*
25、对 100 以内的所有正整数相加求和(1+2+3+4…+100)
#!/bin/bash
sum=0
for i in `seq 100`
do
sum=$[sum+i]
done
echo "总和 $sum"
[root@localhost auto]# cat 25.sh
#!/bin/bash
sum=0
for i in `seq 100`
do
sum=$[sum+i]
done
echo "总和 $sum"
[root@localhost auto]# sh 25.sh
总和 5050
26、统计 20:00 到 20:30 所有访问 nginx 服务器的请求有多少个
#!/bin/bash
awk -F "[ /:]" '$7":"$8>="20:00" && $7":"$8<="20:30"' /usr/local/nginx/logs/access80.log | wc -l
[root@localhost logs]# sh 26.sh
9
27、统计 20:00 到 20:30 所有访问本机nginx 服务器的远程 IP 地址是什么
#!/bin/bash
awk -F "[ /:]" ' {print $1}' /usr/local/nginx/logs/access80.log |uniq
[root@localhost logs]# sh 27.sh
192.168.11.102
192.168.11.254
28、打印国际象棋棋盘
#!/bin/bash
for i in {1..8}
do
for j in {1..8}
do
sum=$[i+j]
if [ $[sum%2] -eq 0 ];then
echo -ne "\033[46m \033[0m"
else
echo -ne "\033[47m \033[0m"
fi
done
echo
done
[root@localhost logs]# sh 28.sh
方块盘
29、统计每个远程 IP 访问了本机nginx 几次?
#!/bin/bash
awk '{ip[$1]++}END{for(i in ip){print ip[i],i}}' /usr/local/nginx/logs/access80.log
[root@localhost logs]# sh 29.sh
7 192.168.11.254
2 192.168.11.102
30、统计当前 Linux 系统中可以登录计算机的账户有多少个
#!/bin/bash
awk -F: '/bash$/{x++}END{print x}' /etc/passwd
grep "bash$" /etc/passwd |wc -l
[root@localhost logs]# sh 30.sh
9
9
31、统计/var/log 有多少个文件,并显示这些文件名
#!/bin/bash
cd /var/log
sum=0
for i in `ls -r *`
do
if [ -f $i ];then
let sum++
echo "文件名:$i"
fi
done
echo "总文件数量为:$sum"
[root@localhost log]# sh 31.sh
文件名:yum.log
文件名:wtmp
文件名:vmware-vmsvc.log
文件名:vmware-vgauthsvc.log.0
文件名:vmware-network.log
文件名:vmware-network.9.log
文件名:vmware-network.8.log
文件名:vmware-network.7.log
文件名:vmware-network.6.log
文件名:vmware-network.5.log
文件名:vmware-network.4.log
文件名:vmware-network.3.log
文件名:vmware-network.2.log
文件名:vmware-network.1.log
文件名:tallylog
文件名:spooler
文件名:secure
文件名:messages
文件名:maillog
文件名:lastlog
文件名:grubby_prune_debug
文件名:firewalld
文件名:dmesg.old
文件名:dmesg
文件名:cron
文件名:btmp
文件名:boot.log-20220620
文件名:boot.log-20220617
文件名:boot.log
文件名:31.sh
总文件数量为:30
32、自动为其他脚本添加解释器信息
#!/bin/bash
if ! grep -q "^#!" $1; then
sed -i '1i #!/bin/bash' $1
sed -i '2i #Description: '
fi
vim +2 $1
[root@localhost ~]# sh 32.sh 32.sh
sed: 没有输入文件
#!/bin/bash
if ! grep -q "^#!" $1; then
sed -i '1i #!/bin/bash' $1
sed -i '2i #Description: '
fi
vim +2 $1
33、自动化部署varnish 源码包软件
#!/bin/bash
yum -y install gcc readline‐devel pcre‐devel &>/dev/null
useradd -s /sbin/nologin varnish
tar -xf varnish‐6.0.10.tgz
cd varnish‐6.0.10
./configure ‐‐prefix=/usr/local/varnish &>/dev/null
make &>/dev/null && make install &>/dev/null
cp redhat/varnish.initrc /etc/init.d/varnish
cp redhat/varnish.sysconfig /etc/sysconfig/varnish
cp redhat/varnish_reload_vcl /usr/bin/
ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/
ln -s /usr/local/varnish/bin/* /usr/bin
mkdir /etc/varnish
cp /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/
uuidgen > /etc/varnish/secret
34、编写nginx启动脚本
#!/bin/bash
program=/usr/local/nginx/sbin/nginx
pid=/usr/local/nginx/logs/nginx.pid
function start(){
if [ -f $pid ];then
echo "nginx 服务已经处于开启状态"
else
$program
fi
}
function stop(){
if [ ! -f $pid ];then
echo "nginx 服务已经关闭"
else
$program -s stop
echo "关闭服务 ok"
fi
}
function status(){
if [ -f $pid ];then
echo "服务正在运行..."
else
echo "服务已经关闭"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "你输入的语法格式错误"
esac
[root@localhost ~]# sh 34.sh status
服务已经关闭
[root@localhost ~]# sh 34.sh start
[root@localhost ~]# sh 34.sh status
服务正在运行...
[root@localhost ~]# ss -antl |grep 80
LISTEN 0 128 *:80 *:*
35、自动对磁盘分区、格式化、挂载
#!/bin/bash
fdisk /dev/sda1 << EOF
n
p
3
wq
EOF
mkfs.xfs /dev/sda3
if [ -e /data ]; then
exit
fi
mkdir /data
echo '/dev/sda3 /data xfs defaults 1 2' >> /etc/fstab
mount -a
36、自动优化 Linux 内核参数
#!/bin/bash
cat >> /usr/lib/sysctl.d/00‐system.conf <<EOF
fs.file‐max=65535
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 5
net.ipv4.tcp_syn_retries = 5
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
kernel.shmall = 2097152
kernel.shmmax = 2147483648
kernel.shmmni = 4096
kernel.sem = 5010 641280 5010 128
net.core.wmem_default=262144
net.core.wmem_max=262144
net.core.rmem_default=4194304
net.core.rmem_max=4194304
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_keepalive_time = 30
net.ipv4.tcp_window_scaling = 0
net.ipv4.tcp_sack = 0
EOF
modprobe br_netfilter
sysctl -p
[root@localhost ~]# sh 36.sh
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 5
net.ipv4.tcp_syn_retries = 5
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.ip_local_port_range = 1024 65535
kernel.shmall = 2097152
kernel.shmmax = 2147483648
kernel.shmmni = 4096
kernel.sem = 5010 641280 5010 128
net.core.wmem_default = 262144
net.core.wmem_max = 262144
net.core.rmem_default = 4194304
net.core.rmem_max = 4194304
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_keepalive_time = 30
net.ipv4.tcp_window_scaling = 0
net.ipv4.tcp_sack = 0
37、切割 Nginx 日志文件(防止单个文件过大,后期处理很困难)
#mkdir /data/scripts
#vim /data/scripts/nginx_log.sh
#!/bin/bash
logs_path="/usr/local/nginx/logs/"
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
38、检测mysql数据库连接数量
#!/bin/bash
log_file=/var/log/mysql_count.log
user=root
passwd=123456
while :
do
sleep 2
count=`mysqladmin -u "$user" -p "$passwd" status | awk '{print $4}'`
echo "`date +%Y‐%m‐%d` 并发连接数为:$count" >> $log_file
done
39、根据 md5 校验码,检测文件是否被修改
#!/bin/bash
for i in $(ls /etc/*.conf)
do
md5sum "$i" >> /var/log/conf_file.log
echo "112"
done
[root@localhost scripts]# sh 39.sh
112
112
112
112
112
112
112
112
112
40、检测 MySQL 服务是否存活
#!/bin/bash
host=192.168.11.101
user=root
passwd=123456
mysqladmin -h '$host' -u '$user' -p'$passwd' ping &>/dev/null
if [ $? -eq 0 ]
then
echo "MySQL is UP"
else
echo "MySQL is down"
fi
[root@localhost scripts]# sh 40.sh
MySQL is down
41、备份 MySQL 的 shell 脚本(mysqldump版本)
#!/bin/bash
user=root
passwd=123456
dbname=mysql
date=$(date +%Y%m%d)
[ ! -d /mysqlbackup ] && mkdir /mysqlbackup
mysqldump -u "$user" -p "$passwd" "$dbname" > /mysqlbackup/"$dbname"-${date}.sql
42、将文件中所有的小写字母转换为大写字母
#!/bin/bash
tr "[a‐z]" "[A‐Z]" < $1
[root@localhost scripts]# sh 42.sh 1.txt
gs_pAth="/usr/locAl/nginx/logs/"
43、非交互自动生成 SSH 密钥文件
#!/bin/bash
rm -rf ~/.ssh/{known_hosts,id_rsa*}
ssh-keygen -t RSA -N '' -f ~/.ssh/id_rsa
[root@node1 ~]# sh 43.sh
Generating public/private RSA key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WeHlqSyLySvkWFG21wm4eipfT6cV6g6/F+q9EZS+y9w root@node1
The key's randomart image is:
+---[RSA 2048]----+
| . . . |
| + .. = . |
| o o o=.o |
| . o .*o. |
| o .S * |
| +..o +.+ |
| = o* +.=. |
| o +. B.B.+ |
| o...oOo*.E |
+----[SHA256]-----+
44.检查特定的软件包是否已经安装
#!/bin/bash
if [ $# -eq 0 ];then
echo "你需要制定一个软件包名称作为脚本参数"
echo "用法:$0 软件包名称 ..."
fi
for package in "$@"
do
if rpm -q ${package} &>/dev/null ;then
echo -e "${package}\033[32m 已经安装\033[0m"
else
echo -e "${package}\033[34;1m 未安装\033[0m"
fi
done
[root@node1 ~]# sh 44.sh nginx
nginx 已经安装
[root@node1 ~]# sh 44.sh mysql
mysql 未安装
[root@node1 ~]# sh 44.sh mas
mas 未安装
45、监控 HTTP 服务器的状态(测试返回码)
#!/bin/bash
url=http://127.0.0.1/
check_http()
{
status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} $url)
}
while :
do
check_http
date=$(date +%Y%m%d‐%H:%M:%S)
echo "当前时间为:$date
$url 服务器异常,状态码为${status_code}.
请尽快排查异常." > /tmp/http$$.pid
if [ $status_code -ne 200 ];then
mail -s Warning root < /tmp/http$$.pid
else
echo "$url 连接正常" >> /var/log/http.log
fi
sleep 5
done
46、自动添加防火墙规则,开启某些服务或端口(适用于 RHEL7)
#!/bin/bash
service="nfs http ssh"
port="80 22 8080"
for i in $service
do
echo "Adding $i service to firewall"
firewall‐cmd --add-service=${i}
done
for i in $port
do
echo "Adding $i Port to firewall"
firewall‐cmd --add-port=${i}/tcp
done
firewall‐cmd --runtime-to-permanent
47、使用脚本自动创建逻辑卷
#!/bin/bash
clear
echo -e "\033[32m !!!!!!警告(Warning)!!!!!!\033[0m"
echo
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
echo "脚本会将整个磁盘转换为 PV,并删除磁盘上所有数据!!!"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
echo
read -p "请问是否继续 y/n?:" sure
[ $sure != y ] && exit
read -p "请输入磁盘名称,如/dev/vdb:" disk
[ -z $disk ] && echo "没有输入磁盘名称" && exit
read -p "请输入卷组名称:" vg_name
[ -z $vg_name ] && echo "没有输入卷组名称" && exit
read -p "请输入逻辑卷名称:" lv_name
[ -z $lv_name ] && echo "没有输入逻辑卷名称" && exit
read -p "请输入逻辑卷大小:" lv_size
[ -z $lv_size ] && echo "没有输入逻辑卷大小" && exit
pvcreate $disk
vgcreate $vg_name $disk
lvcreate -L ${lv_size}M -n ${lv_name} ${vg_name}
[root@node1 ~]# sh 47.sh
!!!!!!警告(Warning)!!!!!!
+++++++++++++++++++++++++++++++++++++++++++++++++
脚本会将整个磁盘转换为 PV,并删除磁盘上所有数据!!!
+++++++++++++++++++++++++++++++++++++++++++++++++
请问是否继续 y/n?:y
请输入磁盘名称,如/dev/vdb:/dev/sea
请输入卷组名称:asd
请输入逻辑卷名称:dasfds
请输入逻辑卷大小:sdfsd
Device /dev/sea not found.
Device /dev/sea not found.
Size requires number argument.
Invalid argument for --size: sdfsdM
Error during parsing of command line.
48、显示 CPU 厂商信息
#!/bin/bash
awk '/vendor_id/{print $3}' /proc/cpuinfo | uniq
[root@node1 ~]# sh 48.sh
GenuineIntel
49、删除某个目录下大小为 0 的文件
#!/bin/bash
dir="/var/www/html"
find $dir -type f -size 0 -exec rm -rf {} \;
[root@node1 ~]# sh 49.sh
[root@node1 ~]# ls /var/www/html/
50、查找 Linux 系统中的僵尸进程
#!/bin/bash
ps aux | awk '{if($8 == "Z"){print $2,$11}}'
[root@node1 ~]# sh 50.sh