Simple shell script to enable or disable the port to public by iptables

Due to the reason that I need to change the SSH Port for my vps to gain more security.
I need to change my ssh port and disable the default ssh port 22 on many new VPS.
So, I'm try to set me free from this work by using shell script by one line.
I share my one key script with you now.

You also can download this script from the url below:

wget http://down.vps.la/shell/f.sh

Code:

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH


clear
echo "+------------------------------------------------------------------------+"
echo "|    One key to Enable or diable iptables Firewall, Written by John      |"
echo "+------------------------------------------------------------------------+"
echo "|        A tool to Enable or diable iptables Firewall in one line        |"
echo "+------------------------------------------------------------------------+"
echo "|            For more information please visit http://vps.la             |"
echo "+------------------------------------------------------------------------+"
echo "|              Enable the port 1313 by run:./f.sh add 1314               |"
echo "+------------------------------------------------------------------------+"
echo "|             Disable the port 1313 by run:./f.sh del 1314               |"
echo "+------------------------------------------------------------------------+"


# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi

echo "Firewall state:"
firewall-cmd --list-all

echo "The port below is allow in the firewall(Before Change):"
firewall-cmd --list-ports

params1=$1
params2=$2
if [ "${params1}" == "l" ]; then
	exit 1
fi
if [[ "${params1}" != "add" && "${params1}" != "del" ]] || [[ "${params2}" == "" ]]; then
    #params1="add"
    echo "params error, usage:"
    echo "f.sh add 1234"
    echo "f.sh del 1234"
    exit 1
fi

if [ "${params1}" == "add" ]; then
    #params1="add"
    echo "enable "${params2}" from the public"
	firewall-cmd --zone=public --add-port=${params2}/tcp --permanent
fi

if [ "${params1}" == "del" ]; then
    #params1=$1
    echo "disable "${params2}" from the public"
	firewall-cmd --zone=public --remove-port=${params2}/tcp --permanent
fi

firewall-cmd --reload


echo "The port below is allow in the firewall(After Change):"
firewall-cmd --list-ports

 

You can use this by:
enable 1314 to the public

f.sh add 1314

disable 1314 to the public

f.sh del 1314

 

阅读剩余
THE END