CentOS 7.x 用shell增加、删除端口

本文介绍了一个用于管理防火墙端口的bash脚本,包括添加、删除和列出端口的功能。通过简单的命令行操作,如port add 8080、port remove 8080和port list,用户可以轻松地控制公共区域的TCP端口。

一、在/usr/local/sbin/下创建port文件,不要扩展名,并给权限  chom 777 port

#!/bin/bash
num=$#
ok=0
if [ ${num} == 1 ]; then
	if [ $1 == 'list' ]; then
	    firewall-cmd --zone=public --list-ports
    		exit 0
	fi
fi

if [ ${num} != 2 ]; then
    echo 'error:you must input two parmas, first is add or remote, second is port number'
    exit 0
fi

case $1 in

add)
firewall-cmd --zone=public --add-port=$2/tcp --permanent
#firewall-cmd --zone=public --add-port=$2/udp --permanent
ok=1
;;

remove)
firewall-cmd --zone=public --remove-port=$2/tcp --permanent
#firewall-cmd --zone=public --remove-port=$2/udp --permanent
ok=1
;;

*)
echo 'first params must be "add" or "remove"' 
;;

esac
if [ ${ok} == 1 ]; then
firewall-cmd --reload
firewall-cmd --zone=public --list-all
fi
exit 0

  二、用法

  #port add 8080

  #port remove 8080

  #port list

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值