|
9 | 9 | # protocol.
|
10 | 10 | #
|
11 | 11 | # Usage:
|
12 |
| -# ./firewall-port {open|close} port protocol |
| 12 | +# ./firewall-port [-6] {open|close} port protocol |
13 | 13 | #
|
14 | 14 | #################################################
|
15 | 15 |
|
| 16 | +usage() { |
| 17 | + echo $"Usage: $0 [-6] {open|close|check} {port} {protocol}" 1>&2 |
| 18 | +} |
| 19 | + |
| 20 | +BINARY=iptables |
| 21 | +while getopts ":6" option; do |
| 22 | + case $option in |
| 23 | + 6) |
| 24 | + BINARY=ip6tables |
| 25 | + ;; |
| 26 | + \?) |
| 27 | + usage |
| 28 | + exit 1 |
| 29 | + ;; |
| 30 | + esac |
| 31 | + shift |
| 32 | +done |
| 33 | + |
16 | 34 | OP="$1"
|
17 | 35 | PORT="$2"
|
18 | 36 | PROTOCOL="${3:-tcp}"
|
|
29 | 47 |
|
30 | 48 | case "${OP}" in
|
31 | 49 | open)
|
32 |
| - if ! iptables -C $CHAIN $RULE 2>/dev/null |
| 50 | + if ! $BINARY -C $CHAIN $RULE 2>/dev/null |
33 | 51 | then # first ensure chain exists
|
34 |
| - if iptables -N "${CHAIN}" 2>/dev/null |
| 52 | + if $BINARY -N "${CHAIN}" 2>/dev/null |
35 | 53 | then #chain did not exist but does now
|
36 |
| - iptables -A "${CHAIN}" -j RETURN |
37 |
| - iptables -I INPUT -j "${CHAIN}" |
38 |
| - fi # asuume chain is used if it exists |
39 |
| - iptables -I "${CHAIN}" $RULE |
40 |
| - /usr/libexec/iptables/iptables.init save |
| 54 | + $BINARY -A "${CHAIN}" -j RETURN |
| 55 | + $BINARY -I INPUT -j "${CHAIN}" |
| 56 | + fi # assume chain is used if it exists |
| 57 | + $BINARY -I "${CHAIN}" $RULE |
| 58 | + /usr/libexec/iptables/"$BINARY".init save |
41 | 59 | fi
|
42 | 60 | ;;
|
43 | 61 | close)
|
44 |
| - if iptables -C $CHAIN $RULE 2>/dev/null |
| 62 | + if $BINARY -C $CHAIN $RULE 2>/dev/null |
45 | 63 | then # close port if it was opened
|
46 |
| - iptables -D $CHAIN $RULE |
47 |
| - /usr/libexec/iptables/iptables.init save |
| 64 | + $BINARY -D $CHAIN $RULE |
| 65 | + /usr/libexec/iptables/"$BINARY".init save |
48 | 66 | fi
|
49 | 67 | ;;
|
50 | 68 | check)
|
51 |
| - if [[ -z `iptables -S $CHAIN | grep " $PORT "` ]] |
| 69 | + if [[ -z `$BINARY -S $CHAIN | grep " $PORT "` ]] |
52 | 70 | then
|
53 | 71 | echo "Port $PORT open: true"
|
54 | 72 | else
|
55 | 73 | echo "Port $PORT open: false"
|
56 | 74 | fi
|
57 | 75 | ;;
|
58 | 76 | *)
|
59 |
| - echo $"Usage: $0 {open|close|check} {port} {protocol}" 1>&2 |
| 77 | + usage |
60 | 78 | exit 1
|
61 | 79 | ;;
|
62 | 80 | esac
|
63 | 81 |
|
64 | 82 | exit 0
|
65 |
| - |
|
0 commit comments