Skip to content

Instantly share code, notes, and snippets.

@aucampia
Created July 18, 2023 19:22
Show Gist options
  • Save aucampia/1a7b8b1f717b3ef3118677d52014d53f to your computer and use it in GitHub Desktop.
Save aucampia/1a7b8b1f717b3ef3118677d52014d53f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
OLD_FILE="$(mktemp iptables-save-old.XXXXXXXXXX)"
NEW_FILE="$(mktemp iptables-edit-new.XXXXXXXXXX)"
cleanup() {
for file in "${OLD_FILE}" "${NEW_FILE}"
do
if [[ -e "${file}" ]]
then
rm -v "${file}"
fi
done
}
# trap cleanup EXIT
iptables-save -f "${OLD_FILE}"
cp -v "${OLD_FILE}" "${NEW_FILE}"
"${EDITOR:-vim}" "${NEW_FILE}"
if ! diff -u "${OLD_FILE}" "${NEW_FILE}"
then
iptables-apply "${NEW_FILE}"
else
1>&2 echo "No changes made."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment