Created
June 5, 2024 03:20
-
-
Save adinata-id/685bac32117aa3429693f5ba37cab6a6 to your computer and use it in GitHub Desktop.
Add, Delete dan list firewall Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Cek apakah firewall-cmd tersedia | |
if ! command -v firewall-cmd &> /dev/null; then | |
echo "firewall-cmd tidak ditemukan. Pastikan firewalld terpasang dan aktif." | |
exit 1 | |
fi | |
# Array untuk menyimpan port yang ditambahkan dan dihapus | |
added_ports=() | |
removed_ports=() | |
# Fungsi untuk menambah port | |
add_port() { | |
sudo firewall-cmd --permanent --zone=public --add-port=${port}/${protocol} | |
sudo firewall-cmd --reload | |
if [ $? -eq 0 ]; then | |
echo "Port ${port}/${protocol} berhasil ditambahkan dan firewall di-reload." | |
added_ports+=("${port}/${protocol}") | |
else | |
echo "Gagal menambahkan port ${port}/${protocol}." | |
fi | |
} | |
# Fungsi untuk menghapus port | |
remove_port() { | |
sudo firewall-cmd --permanent --zone=public --remove-port=${port}/${protocol} | |
sudo firewall-cmd --reload | |
if [ $? -eq 0 ]; then | |
echo "Port ${port}/${protocol} berhasil dihapus dan firewall di-reload." | |
removed_ports+=("${port}/${protocol}") | |
else | |
echo "Gagal menghapus port ${port}/${protocol}." | |
fi | |
} | |
# Fungsi untuk menampilkan semua port yang saat ini ditambahkan ke firewalld | |
list_ports() { | |
echo "Port yang saat ini ditambahkan di firewalld:" | |
sudo firewall-cmd --list-ports | |
} | |
# Prompt pilihan pengguna | |
read -p "Pilih aksi (add/remove/list): " action | |
# Validasi pilihan | |
if [[ "$action" != "add" && "$action" != "remove" && "$action" != "list" ]]; then | |
echo "Aksi tidak valid. Pilih 'add', 'remove', atau 'list'." | |
exit 1 | |
fi | |
# Jika user memilih 'list', tampilkan port dan keluar | |
if [ "$action" == "list" ]; then | |
list_ports | |
exit 0 | |
fi | |
# Prompt pengguna untuk nomor port | |
read -p "Masukkan nomor port: " port | |
# Cek apakah port adalah angka dan dalam rentang yang valid | |
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 0 ] || [ "$port" -gt 65535 ]; then | |
echo "Port tidak valid. Masukkan angka antara 0 dan 65535." | |
exit 1 | |
fi | |
# Prompt pengguna untuk protokol | |
read -p "Masukkan protokol [tcp/udp]: " protocol | |
# Cek apakah protokol diberikan | |
if [[ -z "$protocol" ]]; then | |
echo "Protokol harus diberikan." | |
exit 1 | |
fi | |
# Validasi input protokol | |
if [[ "$protocol" != "tcp" && "$protocol" != "udp" ]]; then | |
echo "Protokol tidak valid. Masukkan 'tcp' atau 'udp'." | |
exit 1 | |
fi | |
# Eksekusi action berdasarkan pilihan pengguna | |
if [ "$action" == "add" ]; then | |
add_port | |
elif [ "$action" == "remove" ]; then | |
remove_port | |
fi | |
# Menampilkan daftar port yang ditambahkan dan dihapus | |
echo "----------------------------------------" | |
echo "Port yang berhasil ditambahkan:" | |
for p in "${added_ports[@]}"; do | |
echo "$p" | |
done | |
echo "----------------------------------------" | |
echo "Port yang berhasil dihapus:" | |
for p in "${removed_ports[@]}"; do | |
echo "$p" | |
done | |
# Menampilkan semua port yang saat ini ditambahkan ke firewalld | |
echo "----------------------------------------" | |
echo "Port yang saat ini ditambahkan di firewalld:" | |
sudo firewall-cmd --list-ports |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment