Belajar Bash: for Loop

The purpose of this exercise is to write a number of for loops to be able to understand how to implement them effectively.

1. Create a script called defense.sh in /opt/scripts
The purpose of the script is to configure the firewall to drop known zombie networks.
[sourcecode language=”bash”]
#!/bin/bash
IP=/opt/scripts/banned

for i in $(awk ‘{print}’ < "$IP" )
do
echo $i
iptables -A INPUT -p tcp -s $i -j DROP
done
exit 0
[/sourcecode]

The script takes a list of IP Addresses in a file and uses awk to print each IP from the file into the variable $i. Then an iptables command employs the “$i” variable to drop each of the IP Addresses on the INPUT chain. The “-p tcp” limits the drop to TCP protocol and the “-s” indicates the source. The IP is dropped with the jump “-j” to DROP. (more…)

Continue ReadingBelajar Bash: for Loop