It's due to the different amount of whitespace used. You can find that out using the following command:
diff -u <(iptables -L) - <<EOF | cat -A
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
EOF
Output:
--- /dev/fd/63^I2016-04-11 08:59:52.663962140 +0200$
+++ -^I2016-04-11 08:59:52.666667769 +0200$
@@ -1,8 +1,8 @@$
Chain INPUT (policy ACCEPT)$
-target prot opt source destination $
+target prot opt source destination$
$
Chain FORWARD (policy ACCEPT)$
-target prot opt source destination $
+target prot opt source destination$
$
Chain OUTPUT (policy ACCEPT)$
-target prot opt source destination $
+target prot opt source destination$
You see, the iptables -L
command appends whitespace after destination
.
To remove that whitespace, you can use sed:
iptables -L | sed 's/[[:space:]]*$//'
If you also fix the syntax error discovered by Jonathan Leffler, your code should work.
Let me add, that the way you are checking if "the firewall is not active" might be to weak. (a) you see, the iptables -L
command is not really meant to be used for text processing. (b) somebody might have added a custom chain but no rules in it. This would let your check fail.
I don't have really an idea how to do it better. Probably changing firewall rules is meant to be a task executed by the administrator himself rather than by programs. :)