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

Cent=`cat /etc/issue | awk '{print $1}' | sed '/^$/d' | grep Kernel`
OtherOs=`cat /etc/issue | awk '{print $1}' | sed '/^$/d'`


centos6(){
remote_port={$remote_port}
sed -i "s/#Port 22/Port $remote_port/g" /etc/ssh/sshd_config
service sshd restart
iptables -A INPUT -p tcp --dport {$remote_port} -j ACCEPT
iptables -L
service network restart
}

centos7(){
remote_port={$remote_port}
sed -i "s/#Port 22/Port $remote_port/g" /etc/ssh/sshd_config
service sshd restart
systemctl start firewalld
firewall-cmd --zone=public --add-port=$remote_port/tcp --permanent
firewall-cmd --reload
systemctl start firewalld
systemctl restart firewalld
}

debian(){
remote_port={$remote_port}
sed -i "s/#Port 22/Port $remote_port/g" /etc/ssh/sshd_config
service sshd restart
iptables -A INPUT -p tcp --dport {$remote_port} -j ACCEPT
iptables -L
systemctl restart networking
}

ubuntu(){
remote_port={$remote_port}
sed -i "s/#Port 22/Port $remote_port/g" /etc/ssh/sshd_config
service sshd restart
sudo ufw allow $remote_port/tcp
echo "yes" | sudo ufw enable
sudo ufw reload
}


if [ "$Cent" = "Kernel" ];then
    checkOsVer=`rpm --eval '%{centos_ver}'`
    case $checkOsVer in
    6)
    centos6
    exit;;

    7)
    centos7
    exit;;

    8)
    centos7
    exit;;

    esac

elif [ "$OtherOs" = "Debian" ];then
    debian
    exit;

elif [ "$OtherOs" = "Ubuntu" ];then
    ubuntu
    exit;
else
    echo "No supported OS is found!!!"
fi
