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
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值