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

一、在/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

 

posted @ 2018-12-21 01:28  泡面 @ 幸福  阅读(305)  评论(0编辑  收藏  举报