Skip to content

Commit

Permalink
Optimization code
Browse files Browse the repository at this point in the history
  • Loading branch information
loyess committed Oct 1, 2023
1 parent 249bbb1 commit ba5f715
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions utils/firewalls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,28 @@ firewall_status(){
}

add_ssh_port(){
local sshdPort

if $(grep -qwE "^Port" /etc/ssh/sshd_config); then
sshdPort=$(cat /etc/ssh/sshd_config | grep -wE "^Port" | cut -d\ -f2)
else
sshdPort=22
fi
if [ "${FIREWALL_MANAGE_TOOL}" = 'firewall-cmd' ]; then
if firewall-cmd --list-ports --permanent 2>/dev/null | grep -qw "22/tcp"; then
if firewall-cmd --list-ports --permanent 2>/dev/null | grep -qw "${sshdPort}/tcp"; then
return
fi
add_firewall_rule "22" "tcp"
add_firewall_rule "${sshdPort}" "tcp"
elif [ "${FIREWALL_MANAGE_TOOL}" = 'ufw' ]; then
if ufw status 2>/dev/null | grep -qwE "OpenSSH|22/tcp"; then
if ufw status 2>/dev/null | grep -qwE "OpenSSH|${sshdPort}/tcp"; then
return
fi
add_firewall_rule "22" "tcp"
add_firewall_rule "${sshdPort}" "tcp"
elif [ "${FIREWALL_MANAGE_TOOL}" = 'iptables' ]; then
if iptables -L INPUT -n --line-numbers 2>/dev/null | grep -qwE "tcp dpt:ssh|tcp dpt:22"; then
if iptables -L INPUT -n --line-numbers 2>/dev/null | grep -qwE "tcp dpt:ssh|tcp dpt:${sshdPort}"; then
return
fi
add_firewall_rule "22" "tcp"
add_firewall_rule "${sshdPort}" "tcp"
fi
}

Expand Down

0 comments on commit ba5f715

Please sign in to comment.