3 # iptables-apply -- a safer way to update iptables remotely
5 # Copyright © Martin F. Krafft <madduck@madduck.net>
6 # Released under the terms of the Artistic Licence 2.0
14 DEFAULT_FILE
=/etc
/network
/iptables
19 $PROGNAME $VERSION -- a safer way to update iptables remotely
26 $PROGNAME is C Martin F. Krafft <madduck@madduck.net>.
28 The program has been published under the terms of the Artistic Licence 2.0
42 Usage: $PROGNAME [options] ruleset
44 The script will try to apply a new ruleset (as output by iptables-save/read
45 by iptables-restore) to iptables, then prompt the user whether the changes
46 are okay. If the new ruleset cut the existing connection, the user will not
47 be able to answer affirmatively. In this case, the script rolls back to the
50 The following options may be specified, using standard conventions:
52 -t | --timeout Specify the timeout in seconds (default: $TIMEOUT)
53 -V | --version Display version information
54 -h | --help Display this help text
59 LONGOPTS
="timeout:,version,help";
61 OPTS
=$
(getopt
-s bash
-o "$SHORTOPTS" -l "$LONGOPTS" -n "$PROGNAME" -- "$@") ||
exit $?
64 (-*) unset OPT_STATE
;;
66 case "${OPT_STATE:-}" in
72 echo "E: non-numeric timeout value." >&2
82 (-h|
--help) usage
>&2; exit 0;;
83 (-V|
--version) about
>&2; exit 0;;
84 (-t|
--timeout) OPT_STATE
=SET_TIMEOUT
;;
90 FILE
="${1:-$DEFAULT_FILE}";
92 if [[ -z "$FILE" ]]; then
93 echo "E: missing file argument." >&2
97 if [[ ! -r "$FILE" ]]; then
98 echo "E: cannot read $FILE" >&2
105 RESTORE
=ip6tables-restore
109 RESTORE
=iptables-restore
113 COMMANDS
=(tempfile
"$SAVE" "$RESTORE")
115 for cmd
in "${COMMANDS[@]}"; do
116 if ! command -v $cmd >/dev
/null
; then
117 echo "E: command not found: $cmd" >&2
124 TMPFILE
=$
(tempfile
-p iptap
)
125 trap "rm -f $TMPFILE" EXIT
1 2 3 4 5 6 7 8 10 11 12 13 14 15
127 if ! "$SAVE" >"$TMPFILE"; then
128 if ! grep -q ipt
/proc
/modules
2>/dev
/null
; then
129 echo "E: iptables support lacking from the kernel." >&2
132 echo "E: unknown error saving current iptables ruleset." >&2
137 [ -x /etc
/init.d
/fail2ban
] && /etc
/init.d
/fail2ban stop
139 echo -n "Applying new ruleset... "
140 if ! "$RESTORE" <"$FILE"; then
142 echo "E: unknown error applying new iptables ruleset." >&2
148 echo -n "Can you establish NEW connections to the machine? (y/N) "
150 read -n1 -t "${TIMEOUT:-15}" ret
2>&1 ||
:
154 echo ...
then my job is
done. See you next
time.
157 if [[ -z "${ret:-}" ]]; then
158 echo "apparently not..."
162 echo "Timeout. Something happened (or did not). Better play it safe..."
163 echo -n "Reverting to old ruleset... "
164 "$RESTORE" <"$TMPFILE";
170 [ -x /etc
/init.d
/fail2ban
] && /etc
/init.d
/fail2ban start