Skip to content

Instantly share code, notes, and snippets.

@anoel2
Last active September 26, 2023 18:31
Show Gist options
  • Save anoel2/42ae130c768c39c538501c2de810ef84 to your computer and use it in GitHub Desktop.
Save anoel2/42ae130c768c39c538501c2de810ef84 to your computer and use it in GitHub Desktop.
Checks what firewall service is running, and opens SMTP ports accordingly. Just change it to your desired ports/services.
#!/bin/bash
# Check if firewalld is running
if systemctl is-active --quiet firewalld.service; then
# Open ports for mail traffic
firewall-cmd --add-port=25/tcp --permanent
firewall-cmd --add-port=587/tcp --permanent
firewall-cmd --add-port=465/tcp --permanent
# Reload FirewallD service
systemctl reload firewalld
# Check if UFW is running
elif ufw status | grep -q 'Status: active'; then
# Open ports for mail traffic
ufw allow 25/tcp
ufw allow 587/tcp
ufw allow 465/tcp
# Reload UFW service
systemctl reload ufw
# Check if IPTABLES service is running
elif systemctl is-active --quiet iptables.service; then
# Open ports for mail traffic
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --dport 587 -j ACCEPT
iptables -A INPUT -p tcp --dport 465 -j ACCEPT
# Save IPTABLES rules
service iptables save
# If no running firewall service is found, start and enable firewalld
else
# Start and enable Firewalld
systemctl start firewalld
systemctl enable firewalld
# Open ports for mail traffic
firewall-cmd --add-port=25/tcp --permanent
firewall-cmd --add-port=587/tcp --permanent
firewall-cmd --add-port=465/tcp --permanent
# Reload FirewallD service
systemctl reload firewalld
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment