ShellScript

Be happy for this moment, this moment is your life.
享受当下的快乐,因为这一刻正是你的人生

OneShellScript

OneShellScript一键下载

rm -rf OneShellScript.sh && curl -O https://wangjian.run/ShellScript/OneShellScript.sh && clear && bash OneShellScript.sh
rm -rf OneShellScript.sh && curl -O https://wangjian.run/ShellScript/OneShellScript.sh && clear && bash OneShellScript.sh
rm -rf OneShellScript.sh && curl -O https://wangjian.run/ShellScript/OneShellScript.sh && clear && bash OneShellScript.sh

debug模式

sh -x 全部执行检查

sh -x 显示脚本执行过程
1、带+号 表示执行过程
2、没有+号表示标准输出
sh -x 显示脚本执行过程
1、带+号 表示执行过程
2、没有+号表示标准输出
sh -x 显示脚本执行过程 1、带+号 表示执行过程 2、没有+号表示标准输出

set 指定位置执行检查

set命令 设置开始debug和结束debug的位置,显示脚本某一个部分执行过程,解决复杂故障
#开启显示
[root@wj ~]# set -x #在控制台执行了命令后,下面的执行都会显示
++ printf '\033]0;%s@%s:%s\007' root wj '~'
[root@wj ~]# pwd
+ pwd
/root
++ printf '\033]0;%s@%s:%s\007' root wj '~'
#结束显示
[root@wj ~]# set +x
+ set +x
#实战中想看哪段代码执行结果,就添加在哪个位置
[root@wj ~]# more check_host_ping.sh.sh
. /etc/init.d/functions
check_ip(){
ip=192.168.15.$1
set -x #开始
ping -c1 -i1 -w1 $ip &>/dev/null
if [[ $? -eq 0 ]];then
action "$ip is true" /bin/true
else
action "$ip is false" /bin/false
fi
set +x #结束
}
main(){
for n in {128..129}
do
check_ip $n
usleep 100
done
}
main
[root@wj ~]# sh check_host_ping.sh.sh
+ ping -c1 -i1 -w1 192.168.15.128
+ [[ 0 -eq 0 ]]
+ action '192.168.15.128 is true' /bin/true
+ local STRING rc
+ STRING='192.168.15.128 is true'
+ echo -n '192.168.15.128 is true '
#
set命令 设置开始debug和结束debug的位置,显示脚本某一个部分执行过程,解决复杂故障
#开启显示
[root@wj ~]# set -x #在控制台执行了命令后,下面的执行都会显示
++ printf '\033]0;%s@%s:%s\007' root wj '~'
[root@wj ~]# pwd
+ pwd
/root
++ printf '\033]0;%s@%s:%s\007' root wj '~'
#结束显示
[root@wj ~]# set +x
+ set +x

#实战中想看哪段代码执行结果,就添加在哪个位置
[root@wj ~]# more check_host_ping.sh.sh 
. /etc/init.d/functions
check_ip(){
  ip=192.168.15.$1
  set -x #开始
  ping -c1 -i1 -w1 $ip &>/dev/null
  if [[ $? -eq 0 ]];then
    action "$ip is true" /bin/true
  else
    action "$ip is false" /bin/false
  fi
  set +x #结束
}
main(){
  for n in {128..129}
  do
    check_ip $n 
    usleep 100
  done
}
main

[root@wj ~]# sh check_host_ping.sh.sh 
+ ping -c1 -i1 -w1 192.168.15.128
+ [[ 0 -eq 0 ]]
+ action '192.168.15.128 is true' /bin/true
+ local STRING rc
+ STRING='192.168.15.128 is true'
+ echo -n '192.168.15.128 is true '

#
set命令 设置开始debug和结束debug的位置,显示脚本某一个部分执行过程,解决复杂故障 #开启显示 [root@wj ~]# set -x #在控制台执行了命令后,下面的执行都会显示 ++ printf '\033]0;%s@%s:%s\007' root wj '~' [root@wj ~]# pwd + pwd /root ++ printf '\033]0;%s@%s:%s\007' root wj '~' #结束显示 [root@wj ~]# set +x + set +x #实战中想看哪段代码执行结果,就添加在哪个位置 [root@wj ~]# more check_host_ping.sh.sh . /etc/init.d/functions check_ip(){ ip=192.168.15.$1 set -x #开始 ping -c1 -i1 -w1 $ip &>/dev/null if [[ $? -eq 0 ]];then action "$ip is true" /bin/true else action "$ip is false" /bin/false fi set +x #结束 } main(){ for n in {128..129} do check_ip $n usleep 100 done } main [root@wj ~]# sh check_host_ping.sh.sh + ping -c1 -i1 -w1 192.168.15.128 + [[ 0 -eq 0 ]] + action '192.168.15.128 is true' /bin/true + local STRING rc + STRING='192.168.15.128 is true' + echo -n '192.168.15.128 is true ' #

单步执行检查

判断IP是否能ping通

版本1

#!/bin/bash
. /etc/init.d/functions
check_ip(){
ip=192.168.15.$1
ping -c1 -i1 -w1 $ip &>/dev/null
if [[ $? -eq 0 ]];then
action "$ip is true" /bin/true
else
action "$ip is false" /bin/false
fi
}
main(){
for n in {128..129}
do
check_ip $n
usleep 100
done
}
main
#!/bin/bash
. /etc/init.d/functions
check_ip(){
  ip=192.168.15.$1
  ping -c1 -i1 -w1 $ip &>/dev/null
  if [[ $? -eq 0 ]];then
    action "$ip is true" /bin/true
  else
    action "$ip is false" /bin/false
  fi
}
main(){
  for n in {128..129}
  do
    check_ip $n 
    usleep 100
  done
}
main
#!/bin/bash . /etc/init.d/functions check_ip(){ ip=192.168.15.$1 ping -c1 -i1 -w1 $ip &>/dev/null if [[ $? -eq 0 ]];then action "$ip is true" /bin/true else action "$ip is false" /bin/false fi } main(){ for n in {128..129} do check_ip $n usleep 100 done } main

版本2

docker

一键在线安装最新版本的docker

yum -y install wget && wget https://wangjian.run/ShellScript/docker-install.sh --no-check-certificate && bash docker-install.sh
yum -y install wget && wget https://wangjian.run/ShellScript/docker-install.sh --no-check-certificate && bash docker-install.sh
yum -y install wget && wget https://wangjian.run/ShellScript/docker-install.sh --no-check-certificate && bash docker-install.sh

tomcat

一键安装tomcat

yum -y install wget && wget https://wangjian.run/ShellScript/tomjb.sh --no-check-certificate && bash tomjb.sh
yum -y install wget && wget https://wangjian.run/ShellScript/tomjb.sh --no-check-certificate && bash tomjb.sh
yum -y install wget && wget https://wangjian.run/ShellScript/tomjb.sh --no-check-certificate && bash tomjb.sh

openssh

yum -y install wget && wget https://wangjian.run/ShellScript/ssh.sh --no-check-certificate && bash ssh.sh
yum -y install wget && wget https://wangjian.run/ShellScript/ssh.sh --no-check-certificate && bash ssh.sh
yum -y install wget && wget https://wangjian.run/ShellScript/ssh.sh --no-check-certificate && bash ssh.sh

nginx

#nginx重启脚本
vim nginx-restart.sh
#nginx重启脚本
vim nginx-restart.sh 
#nginx重启脚本 vim nginx-restart.sh
ngmul=/opt/nginx-1.24.0/sbin/
id=$(pgrep nginx)
kill -9 $id > /dev/null
#启动nginx
cd $ngmul && ./nginx
#查看进程
ps -ef|grep nginx
ngmul=/opt/nginx-1.24.0/sbin/
id=$(pgrep nginx)
kill -9 $id > /dev/null
#启动nginx
cd $ngmul && ./nginx 
#查看进程
ps -ef|grep nginx
ngmul=/opt/nginx-1.24.0/sbin/ id=$(pgrep nginx) kill -9 $id > /dev/null #启动nginx cd $ngmul && ./nginx #查看进程 ps -ef|grep nginx

菜单制作select

方式1:使用select

  • 优点select命令提供了一个简洁的方式来创建菜单,它自动将选项列表显示成编号菜单,并处理用户的选择。此命令还支持自定义提示符,使得用户体验更加友好。select命令还负责获取输入的答案并自动处理,减少了需要编写的代码量。
  • 缺点select命令创建的菜单在视觉上不如图形界面美观,但这对于命令行界面来说是可以接受的。
  • 适用场景:适合于快速实现一个文本菜单,并且菜单项较多的情况。
export LANG=en_US.utf8
clear
#点菜函数
function diancai(){
sum=0
PS3="点菜选择对应的序号:"
select menu in 北京烤鸭 小龙虾 买单 退出点菜游戏
do
case $REPLY in
1)
echo $menu "价格是:100元"
let sum+=100
;;
2)
echo $menu "价格是:50元"
let sum+=50
;;
3)
echo "本次点菜价格是:$sum元"
sum=0
;;
4)
zhucaidan
;;
esac
done
}
function zhucaidan(){
PS3="请选择对应的序号: "
while true;
do
select num in 点菜游戏 退出;
do
case $num in
点菜游戏)
diancai
;;
退出)
exit
;;
*)
echo "输入错误,请重新输入对应的序号!"
break
esac
done
done
}
zhucaidan
export LANG=en_US.utf8
clear
#点菜函数
function diancai(){
  sum=0
  PS3="点菜选择对应的序号:"
  select menu in 北京烤鸭 小龙虾 买单 退出点菜游戏
  do
    case $REPLY in     
      1)
      echo $menu "价格是:100元"
      let sum+=100
      ;;
      2)
      echo $menu "价格是:50元"
      let sum+=50
      ;;
      3)
      echo "本次点菜价格是:$sum元"
      sum=0
      ;;
      4)
      zhucaidan
      ;;
    esac
  done
  
}
function zhucaidan(){
PS3="请选择对应的序号: "
while true; 
do
  select num in 点菜游戏 退出;
    do
      case $num in
      点菜游戏)
        diancai
        
          ;;
      退出)
        exit
        ;;
      *)
        echo "输入错误,请重新输入对应的序号!"
          break
      esac
    done
done
}
zhucaidan
export LANG=en_US.utf8 clear #点菜函数 function diancai(){ sum=0 PS3="点菜选择对应的序号:" select menu in 北京烤鸭 小龙虾 买单 退出点菜游戏 do case $REPLY in 1) echo $menu "价格是:100元" let sum+=100 ;; 2) echo $menu "价格是:50元" let sum+=50 ;; 3) echo "本次点菜价格是:$sum元" sum=0 ;; 4) zhucaidan ;; esac done } function zhucaidan(){ PS3="请选择对应的序号: " while true; do select num in 点菜游戏 退出; do case $num in 点菜游戏) diancai ;; 退出) exit ;; *) echo "输入错误,请重新输入对应的序号!" break esac done done } zhucaidan

方式2:使用 echo 命令和 read 命令

  • 优点:这种方式通常结合case语句来处理用户的输入,可以清晰地列出所有选项,并且每个选项后都可以跟随特定的命令。例如,可以通过read命令读取用户的输入,然后使用case语句判断用户输入的值,并执行相应的命令。
  • 缺点:需要手动编写更多的代码来显示菜单项和处理用户输入,且对用户输入的错误处理较为复杂。
  • 适用场景:适合于功能选项较少,逻辑简单或需要详细定制菜单交互过程的场合。
  • 这种方法是最基础的,它通过连续的 echo 命令打印出菜单项,然后使用 read 命令读取用户的输入
#!/bin/sh
while true; do
echo "1. 目录内容"
echo "2. 切换目录"
echo "3. 创建文件"
echo "4. 编辑文件"
echo "5. 删除文件"
echo "6. 退出菜单"
read -p "请输入选择(1-6): " input
case $input in
1) ls ;;
2) echo "Enter target directory: "
read dir
cd $dir ;;
3) echo "Enter a file name: "
read file
touch $file ;;
4) echo "Enter a file name: "
read file
vi $file ;;
5) echo "Enter a file name: "
read file
rm $file ;;
6) break ;;
*) echo "错误选择!" ;;
esac
done
#!/bin/sh
while true; do
    echo "1. 目录内容"
    echo "2. 切换目录"
    echo "3. 创建文件"
    echo "4. 编辑文件"
    echo "5. 删除文件"
    echo "6. 退出菜单"
    
    read -p "请输入选择(1-6): " input
    case $input in
        1) ls ;;
        2) echo "Enter target directory: "
           read dir
           cd $dir ;;
        3) echo "Enter a file name: "
           read file
           touch $file ;;
        4) echo "Enter a file name: "
           read file
           vi $file ;;
        5) echo "Enter a file name: "
           read file
           rm $file ;;
        6) break ;;
        *) echo "错误选择!" ;;
    esac
done
#!/bin/sh while true; do echo "1. 目录内容" echo "2. 切换目录" echo "3. 创建文件" echo "4. 编辑文件" echo "5. 删除文件" echo "6. 退出菜单" read -p "请输入选择(1-6): " input case $input in 1) ls ;; 2) echo "Enter target directory: " read dir cd $dir ;; 3) echo "Enter a file name: " read file touch $file ;; 4) echo "Enter a file name: " read file vi $file ;; 5) echo "Enter a file name: " read file rm $file ;; 6) break ;; *) echo "错误选择!" ;; esac done

速查表定时更新

#!/bin/bash
#速查表定时获取最新版本脚本
#查找容器
rongqiid=$(docker ps | awk 'NR!=1{print }' | awk '{print $1}')
#停止容器
docker stop "$rongqiid"
#查看所有容器
rongqiids=$(docker ps -qa)
docker rm "$rongqiids"
#查找镜像删除
JiangXiangId=$(docker images | awk 'NR!=1{print }' | awk '{print $3}')
docker rmi -f "$JiangXiangId"
#下载镜像并启动容器
docker pull wcjiang/reference
docker run --name reference --rm -d -p 9667:3000 wcjiang/reference:latest
#!/bin/bash
#速查表定时获取最新版本脚本
#查找容器
rongqiid=$(docker ps | awk 'NR!=1{print }' | awk '{print $1}')
#停止容器
docker stop "$rongqiid"
#查看所有容器
rongqiids=$(docker ps -qa)
docker rm "$rongqiids"
#查找镜像删除
JiangXiangId=$(docker images | awk 'NR!=1{print }' | awk '{print $3}')
docker rmi -f "$JiangXiangId"
#下载镜像并启动容器
docker pull wcjiang/reference
docker run --name reference --rm -d -p 9667:3000 wcjiang/reference:latest
#!/bin/bash #速查表定时获取最新版本脚本 #查找容器 rongqiid=$(docker ps | awk 'NR!=1{print }' | awk '{print $1}') #停止容器 docker stop "$rongqiid" #查看所有容器 rongqiids=$(docker ps -qa) docker rm "$rongqiids" #查找镜像删除 JiangXiangId=$(docker images | awk 'NR!=1{print }' | awk '{print $3}') docker rmi -f "$JiangXiangId" #下载镜像并启动容器 docker pull wcjiang/reference docker run --name reference --rm -d -p 9667:3000 wcjiang/reference:latest

其他脚本

vim mysql-install-centos7.9.sh
vim mysql-install-centos7.9.sh
vim mysql-install-centos7.9.sh
#!/bin/bash
echo -e "\e[32m##########mysql安装环境开始检测##########\e[0m"
#软件安装环境检查
mariadb=$(rpm -qa|grep -c mariadb)
mysql=$(rpm -qa|grep -c mysql) #写法2 grep mysql | wc -l
if [[ $mariadb != 0 ]];then
echo -e "\e[31m##########mysql安装环境监测不通过##########\e[0m"
echo "本系统有安装mariadb数据库,请先卸载,卸载方法如下实例所示:"
echo "可以使用rpm -qa|grep mariadb 查询"
echo "然后使用rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps进行卸载"
exit 1
fi
if [[ $mysql != 0 ]];then
echo -e "\e[31m##########mysql安装环境监测不通过##########\e[0m"
echo "本系统有安装mysql数据库,请先卸载相关的安装"
exit 1
fi
#<<COMMENT
#用户和组检查
yonghuzu=$(more /etc/group | grep -c mysql) #shell规范中不建议用cat,cat是一个工具来欺骗"猫"的文件.将AINGLE文件作为程序的输入被认为是《禁止酷刑和其他残忍、不人道或有辱人格的待遇或处罚公约》
yonghu=$(more /etc/passwd |grep -c mysql)
if [[ $yonghuzu == 0 ]];then
groupadd mysql
echo -e "\e[32m##########本系统没有mysql组,脚本已自动完成mysql组创建##########\e[0m"
else
echo -e "\e[32m##########本系统有mysql组,无需脚本创建用户组##########\e[0m"
fi
if [[ $yonghu == 0 ]];then
useradd -g mysql mysql
echo -e "\e[32m##########本系统没有mysql用户,脚本已自动完成mysql用户创建,并添加到mysql组##########\e[0m"
else
echo -e "\e[32m##########本系统有mysql用户,无需脚本创建用户##########\e[0m"
fi
if [[ $mariadb == 0 && $mysql == 0 ]];then
echo -e "\e[32m##########恭喜你mysql安装环境监测通过,开始安装mysql##########\e[0m"
fi
#COMMENT
#!/bin/bash
echo -e "\e[32m##########mysql安装环境开始检测##########\e[0m"
#软件安装环境检查
mariadb=$(rpm -qa|grep -c mariadb)
mysql=$(rpm -qa|grep -c mysql) #写法2 grep mysql | wc -l
if [[ $mariadb != 0 ]];then
    echo -e "\e[31m##########mysql安装环境监测不通过##########\e[0m"
    echo "本系统有安装mariadb数据库,请先卸载,卸载方法如下实例所示:"
    echo "可以使用rpm -qa|grep mariadb 查询"
    echo "然后使用rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps进行卸载"
    exit 1
fi

if [[ $mysql != 0 ]];then
    echo -e "\e[31m##########mysql安装环境监测不通过##########\e[0m"
    echo "本系统有安装mysql数据库,请先卸载相关的安装"
    exit 1
fi

#<<COMMENT
#用户和组检查
yonghuzu=$(more /etc/group | grep -c mysql) #shell规范中不建议用cat,cat是一个工具来欺骗"猫"的文件.将AINGLE文件作为程序的输入被认为是《禁止酷刑和其他残忍、不人道或有辱人格的待遇或处罚公约》
yonghu=$(more /etc/passwd |grep -c mysql)
if [[ $yonghuzu == 0 ]];then
    groupadd mysql
    echo -e "\e[32m##########本系统没有mysql组,脚本已自动完成mysql组创建##########\e[0m"
else
    echo -e "\e[32m##########本系统有mysql组,无需脚本创建用户组##########\e[0m"
fi

if [[ $yonghu == 0 ]];then
    useradd -g mysql mysql
    echo -e "\e[32m##########本系统没有mysql用户,脚本已自动完成mysql用户创建,并添加到mysql组##########\e[0m"
else
    echo -e "\e[32m##########本系统有mysql用户,无需脚本创建用户##########\e[0m"
fi

if [[ $mariadb == 0 && $mysql == 0 ]];then
    echo -e "\e[32m##########恭喜你mysql安装环境监测通过,开始安装mysql##########\e[0m"
fi
#COMMENT
#!/bin/bash echo -e "\e[32m##########mysql安装环境开始检测##########\e[0m" #软件安装环境检查 mariadb=$(rpm -qa|grep -c mariadb) mysql=$(rpm -qa|grep -c mysql) #写法2 grep mysql | wc -l if [[ $mariadb != 0 ]];then echo -e "\e[31m##########mysql安装环境监测不通过##########\e[0m" echo "本系统有安装mariadb数据库,请先卸载,卸载方法如下实例所示:" echo "可以使用rpm -qa|grep mariadb 查询" echo "然后使用rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps进行卸载" exit 1 fi if [[ $mysql != 0 ]];then echo -e "\e[31m##########mysql安装环境监测不通过##########\e[0m" echo "本系统有安装mysql数据库,请先卸载相关的安装" exit 1 fi #<<COMMENT #用户和组检查 yonghuzu=$(more /etc/group | grep -c mysql) #shell规范中不建议用cat,cat是一个工具来欺骗"猫"的文件.将AINGLE文件作为程序的输入被认为是《禁止酷刑和其他残忍、不人道或有辱人格的待遇或处罚公约》 yonghu=$(more /etc/passwd |grep -c mysql) if [[ $yonghuzu == 0 ]];then groupadd mysql echo -e "\e[32m##########本系统没有mysql组,脚本已自动完成mysql组创建##########\e[0m" else echo -e "\e[32m##########本系统有mysql组,无需脚本创建用户组##########\e[0m" fi if [[ $yonghu == 0 ]];then useradd -g mysql mysql echo -e "\e[32m##########本系统没有mysql用户,脚本已自动完成mysql用户创建,并添加到mysql组##########\e[0m" else echo -e "\e[32m##########本系统有mysql用户,无需脚本创建用户##########\e[0m" fi if [[ $mariadb == 0 && $mysql == 0 ]];then echo -e "\e[32m##########恭喜你mysql安装环境监测通过,开始安装mysql##########\e[0m" fi #COMMENT

判断软件是否安装

Packs=("curl" "tar" "gzip" "netstat" "ss" "docker" ); for pack in "${Packs[@]}"; do command -v "$pack" >/dev/null 2>&1 || echo -e "\033[31mError: $pack 命令不存在\033[0m"; done
Packs=("curl" "tar" "gzip" "netstat" "ss" "docker" ); for pack in "${Packs[@]}"; do command -v "$pack" >/dev/null 2>&1 || echo -e "\033[31mError: $pack 命令不存在\033[0m"; done
Packs=("curl" "tar" "gzip" "netstat" "ss" "docker" ); for pack in "${Packs[@]}"; do command -v "$pack" >/dev/null 2>&1 || echo -e "\033[31mError: $pack 命令不存在\033[0m"; done

统计IP出现的次数

tail -n 1000 wangjian.run.log | awk '{print $1}' |sort | uniq -c | sort -nr | head -n 10
tail -n 1000 wangjian.run.log | awk '{print $1}' |sort | uniq -c | sort -nr | head -n 10
tail -n 1000 wangjian.run.log | awk '{print $1}' |sort | uniq -c | sort -nr | head -n 10

查看日志

sed -n '/09\/Jan\/2025:09:21:00/,/09\/Jan\/2025:09:21:59/'p wangjian.run.log
sed -n '/09\/Jan\/2025:09:21:00/,/09\/Jan\/2025:09:21:59/'p wangjian.run.log
sed -n '/09\/Jan\/2025:09:21:00/,/09\/Jan\/2025:09:21:59/'p wangjian.run.log
sed -n '/2019-07-30 20:00:00/,/2019-07-30 21:30:00/'p gateway.log
sed -n '/2019-07-30 20:00:00/,/2019-07-30 21:30:00/'p gateway.log
sed -n '/2019-07-30 20:00:00/,/2019-07-30 21:30:00/'p gateway.log

多列输出所在行

[root@wjcentos7 ~]# more wjip.txt
300 192.168.1.3
100 192.168.1.4
150 192.168.1.5
2 192.168.1.6
5 192.168.1.7
50 192.168.1.8
[root@wjcentos7 ~]# more wjip.txt 
300 192.168.1.3
100 192.168.1.4
150 192.168.1.5
2 192.168.1.6
5 192.168.1.7
50 192.168.1.8
[root@wjcentos7 ~]# more wjip.txt 300 192.168.1.3 100 192.168.1.4 150 192.168.1.5 2 192.168.1.6 5 192.168.1.7 50 192.168.1.8
#!/bin/bash
file="wjip.txt"
#在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中
#然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完
if [ -f "$file" ];then
#count ip_address代表txt文件中的两个字段
while read count ip_address;do
#echo $ip_address
if [ $count -gt 200 ];then
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent
firewall-cmd --reload
echo "$ip_address:禁用成功"
fi
done < "$file"
else
echo "文件不存在"
fi
#!/bin/bash
file="wjip.txt"
#在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中
#然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完
if [ -f "$file" ];then
        #count ip_address代表txt文件中的两个字段
  while read count ip_address;do 
    #echo $ip_address
    if [ $count -gt 200 ];then
    firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent
                firewall-cmd --reload
                echo "$ip_address:禁用成功"    
    fi
  done < "$file"
else 
  echo "文件不存在"
fi
#!/bin/bash file="wjip.txt" #在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中 #然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完 if [ -f "$file" ];then #count ip_address代表txt文件中的两个字段 while read count ip_address;do #echo $ip_address if [ $count -gt 200 ];then firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent firewall-cmd --reload echo "$ip_address:禁用成功" fi done < "$file" else echo "文件不存在" fi

自动禁用启用IP

统计出现大于200的IP地址

tail -n 1000 wangjian.run.log | awk '{print $1}' |sort | uniq -c | awk '$1 > 200' | sort -nr | awk '{print $2}'| tr -d ' '
tail -n 1000 wangjian.run.log | awk '{print $1}' |sort | uniq -c | awk '$1 > 200' | sort -nr | awk '{print $2}'| tr -d ' '
tail -n 1000 wangjian.run.log | awk '{print $1}' |sort | uniq -c | awk '$1 > 200' | sort -nr | awk '{print $2}'| tr -d ' '

禁用脚本

[root@wjcentos7 ~]# more DisableIp.sh
#!/bin/bash
file="wjip.txt"
#在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中
#然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完
if [ -f "$file" ];then
while read ip_address;do
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent
firewall-cmd --reload
echo "$ip_address:禁用成功"
done < "$file"
else
echo "文件不存在"
fi
[root@wjcentos7 ~]# more DisableIp.sh 
#!/bin/bash
file="wjip.txt"
#在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中
#然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完
if [ -f "$file" ];then
  while read ip_address;do
    firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent
    firewall-cmd --reload
    echo "$ip_address:禁用成功"             
  done < "$file"
else
  echo "文件不存在"
fi
[root@wjcentos7 ~]# more DisableIp.sh #!/bin/bash file="wjip.txt" #在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中 #然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完 if [ -f "$file" ];then while read ip_address;do firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent firewall-cmd --reload echo "$ip_address:禁用成功" done < "$file" else echo "文件不存在" fi

启用脚本

[root@wjcentos7 ~]# more EnableIp.sh
#!/bin/bash
file="wjip.txt"
#在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中
#然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完
if [ -f "$file" ];then
while read ip_address;do
firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent
firewall-cmd --reload
echo "$ip_address:已取消禁用"
done < "$file"
else
echo "文件不存在"
fi
[root@wjcentos7 ~]# more EnableIp.sh 
#!/bin/bash
file="wjip.txt"
#在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中
#然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完
if [ -f "$file" ];then
  while read ip_address;do
    firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent
    firewall-cmd --reload
    echo "$ip_address:已取消禁用"             
  done < "$file"
else
  echo "文件不存在"
fi
[root@wjcentos7 ~]# more EnableIp.sh #!/bin/bash file="wjip.txt" #在这个脚本中,while read line循环会不断从文件(通过< "$file"重定向输入)中读取一行数据,将其存储到变量line中 #然后通过echo $line输出这一行数据,直到文件中的所有行都被读取完 if [ -f "$file" ];then while read ip_address;do firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="'$ip_address'" port port="22" protocol="tcp" reject' --permanent firewall-cmd --reload echo "$ip_address:已取消禁用" done < "$file" else echo "文件不存在" fi

定时任务

Everyone has its disadvantage just like the god bites the apple. the bigger disadvantage you have, the more the god appreciate it.
每个人都会有缺陷,就像被上帝咬过的苹果,有的人缺陷比较大,正是因为上帝特别喜欢他的芬芳
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容