Shell编程----------两种方法关闭防火墙(shell脚本)---------关闭核心防护(shell脚本)

1.shell脚本关闭防火墙1

[root@localhost ~]# vi firewalld.sh
#!/bin/bash
STATUS=$(systemctl status firewalld | grep "Active:" | awk '{print $2}')
case "$1" in
start)
  if [ $STATUS = active ]
  then
  echo "防火墙处于开启状态,无需操作!"
  else
  echo "防火墙正在开启中..."
  systemctl start firewalld &>/dev/null
  echo "防火墙已经开启!"
  fi
  ;;
stop)
  if [ $STATUS = inactive ]
  then
  echo "防火墙处于关闭状态,无需操作!"
  else
  echo "防火墙正在关闭中..."
  systemctl stop firewalld &>/dev/null
  echo "防火墙已关闭!"
  fi
 ;;
status)
  if [ $STATUS = active ]
  then
  systemctl status firewalld &>/dev/null
  echo "防火墙处于开启状态!"
  else
  systemctl status firewalld &>/dev/null
  echo "防火墙处于关闭状态!"
  fi
  ;;
*)
  echo "请输入正确格式:start/stop/status"
esac
[root@localhost ~]# chmod +x firewalld.sh
[root@localhost ~]# ./firewalld.sh start
防火墙正在开启中...
防火墙已经开启!
[root@localhost ~]# ./firewalld.sh status
防火墙处于开启状态!
[root@localhost ~]# ./firewalld.sh stop
防火墙正在关闭中...
防火墙已关闭!
[root@localhost ~]# ./firewalld.sh status
防火墙处于关闭状态!
[root@localhost ~]# ./firewalld.sh reload
请输入正确格式:start/stop/status

2. 关闭防火墙脚本2

[root@localhost ~]# vi firewalld1.sh
#!/bin/bash
STATUS=$(systemctl status firewalld | grep "running")
case "$1" in
start)
  if [ $? = 0 ]
  then
  echo "防火墙处于开启状态,无需操作!"
  else
  echo "防火墙正在开启中..."
  systemctl start firewalld &>/dev/null
  echo "防火墙已经开启!"
  fi
  ;;
stop)
  if [ $? != 0 ]
  then
  echo "防火墙处于关闭状态,无需操作!"
  else
  echo "防火墙正在关闭中..."
  systemctl stop firewalld &>/dev/null
  echo "防火墙已关闭!"
  fi
 ;;
status)
  if [ $? = 0 ]
  then
  systemctl status firewalld &>/dev/null
  echo "防火墙处于开启状态!"
  else
  systemctl status firewalld &>/dev/null
  echo "防火墙处于关闭状态!"
  fi
  ;;
*)
  echo "请输入正确格式:start/stop/status"
esac
[root@localhost ~]# chmod +x firewalld1.sh
[root@localhost ~]# ./firewalld1.sh start
防火墙正在开启中...
防火墙已经开启!
[root@localhost ~]# ./firewalld1.sh status
防火墙处于开启状态!
[root@localhost ~]# ./firewalld1.sh stop
防火墙正在关闭中...
防火墙已关闭!
[root@localhost ~]# ./firewalld1.sh status
防火墙处于关闭状态!
[root@localhost ~]# ./firewalld1.sh reload
请输入正确格式:start/stop/status

3.关闭核心防护

[root@localhost ~]# vi selinux.sh
#!/bin/bash
se_cnf="/etc/selinux/config"
find_key="SELINUX="
setenforce 0 &>/dev/null
sed -ri "/^$find_key/c${find_key}disabled" $se_cnf
result="`getenforce`"
if [ $result = Enforing ];then
  echo "selinux关闭失败!"
  exit 10
else
  echo "selinux关闭成功!"
fi
[root@localhost ~]# chmod +x selinux.sh
[root@localhost ~]# ./selinux.sh 
selinux关闭成功!
[root@localhost ~]# setenforce ?
setenforce: SELinux is disabled
    原文作者:博博的博客
    原文地址: https://blog.csdn.net/weixin_48191138/article/details/108058394
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞