-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirewall_rules.sh
77 lines (71 loc) · 2.09 KB
/
firewall_rules.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
function list_rules() {
echo -e "${GREEN}Current iptables rules:${NC}"
echo "---------------------------------------------"
iptables -L --line-numbers -n -v | while IFS= read -r line; do
if [[ $line =~ ^Chain ]]; then
echo -e "${RED}$line${NC}"
echo "---------------------------------------------"
else
echo "$line"
fi
done
echo "---------------------------------------------"
echo ""
}
function add_rule() {
read -p "Enter the chain (INPUT, FORWARD, OUTPUT): " chain
read -p "Enter the protocol (tcp, udp, icmp, all): " protocol
read -p "Enter the port number: " port
read -p "Enter the action (ACCEPT, DROP, REJECT): " action
if ! [[ $port =~ ^[0-9]+$ ]]; then
echo -e "${RED}Error: Port must be a number.${NC}"
return
fi
iptables -A "$chain" -p "$protocol" --dport "$port" -j "$action"
echo -e "${GREEN}Rule added successfully.${NC}"
}
function delete_rule() {
read -p "Enter the chain (INPUT, FORWARD, OUTPUT) from which to delete the rule: " chain
read -p "Enter the rule number to delete: " rule_number
if ! [[ $rule_number =~ ^[0-9]+$ ]]; then
echo -e "${RED}Error: Rule number must be a number.${NC}"
return
fi
iptables -D "$chain" "$rule_number"
echo -e "${GREEN}Rule deleted successfully.${NC}"
}
function main_menu() {
clear
echo -e "${GREEN}Firewall Rule Management Script${NC}"
echo "1. List current rules"
echo "2. Add a new rule"
echo "3. Delete a rule"
echo "4. Exit"
read -p "Enter your choice [1-4]: " choice
case $choice in
1)
list_rules
;;
2)
add_rule
;;
3)
delete_rule
;;
4)
echo "Exiting script."
exit 0
;;
*)
echo -e "${RED}Invalid option, please choose a number between 1 and 4.${NC}"
;;
esac
}
while true; do
main_menu
read -p "Press Enter to continue..."
done