Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

Belajar Bash: for Loop

Posted on November 13, 2012

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.

Create a file with known zombie networks. This file is actually maintained by Spamhaus.org (http://www.spamhaus.org/xbl/). This is a short sample.

banned file with IPs
24.190.78.101
38.101.148.126
41.206.45.202
58.0.0.0/8
59.107.0.0/17
59.108.0.0/15
59.110.0.0/15
59.151.0.0/17
59.155.0.0/16
59.172.0.0/15

Test the script output with :
iptables -L -n

Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all – 0.0.0.0/0 0.0.0.0/0
DROP tcp – 24.190.78.101 0.0.0.0/0
DROP tcp – 38.101.148.126 0.0.0.0/0
DROP tcp – 41.206.45.202 0.0.0.0/0
DROP tcp – 58.0.0.0/8 0.0.0.0/0
DROP tcp – 59.107.0.0/17 0.0.0.0/0
DROP tcp – 59.108.0.0/15 0.0.0.0/0
DROP tcp – 59.110.0.0/15 0.0.0.0/0
DROP tcp – 59.151.0.0/17 0.0.0.0/0
DROP tcp – 59.155.0.0/16 0.0.0.0/0
DROP tcp – 59.172.0.0/15 0.0.0.0/0

2. List executable files in a directory, create a file called dir.sh
[sourcecode language=”bash”]
#!/bin/bash
for i in *
do
if [ -f "$i" -a -x "$i" ]
then
echo "Executable file $i "
fi
done
[/sourcecode]

This script will list executable files in a directory. Note the for loop will loop through all files and only print those which are files “-f” and “-a” are executable “-x”.

Terbaru

  • Google Bikin Kejutan! Pixel 10 Diskon Gila-gilaan di YouTube Premium
  • Apa Itu Google CC? Ini Pengertian Agen Produktivitas AI Eksperimental Terbaru
  • Apa Itu Ultras Seblak di eSport? Pengertian dan Fenomena Baru Suporter eSport
  • Android 16: Animasi Folder Baru yang Mengubah Cara Kita Berinteraksi!
  • Android 16: Notifikasi Lokasi ‘Blue Dot’ – Fitur Baru yang Perlu Kalian Ketahui!
  • Apa Itu Risiko Auto Click di Event Spongebob Mobile Legends? Ini Penjelasannya
  • Apa Itu Fitur Eksperimental Windows? Ini Pengertian dan Cara Menonaktifkannya
  • Apa Itu Android 16 Beta 1? Ini Pengertian dan Fitur Terbarunya
  • Belum Tahu? Ini Trik Supaya Bisa Dapat Skin Patrick Mobile Legends dengan Harga Murah
  • Pixel Desember 2025: Update Besar Siap Meluncur, Apa yang Baru?
  • Apa Itu HYFE XL Prioritas? Ini Pengertian, FUP, dan Realita Kecepatannya
  • Pengertian Render dan Convert: Apa Bedanya dalam Video Editing?
  • Cara Mengatasi Aplikasi Office yang Terus Muncul dan Menerapkan Perubahan Pengaturan Privasi
  • Pixel Launcher Mendapatkan Sentuhan Google Search Baru!
  • Penyebab Aplikasi Wondr BNI Tidak Bisa Dibuka
  • Kode 0425 Daerah Mana? Ini Pengertian dan Fakta Sebenarnya
  • Apa Itu SSS CapCut? Pengertian Downloader Video Tanpa Watermark yang Wajib Kalian Tahu
  • Apa Itu Paket GamesMAX Telkomsel? Ini Pengertian dan Fungsinya Bagi Gamers
  • Apa Itu Menu Plus di Google Search? Ini Pengertian dan Fungsinya
  • Apa Itu Lepas Kolpri? Ini Pengertian dan Fenomenanya di Dunia Gaming
  • Pixel Buds Pro Dapat Update Software dengan Dukungan ANC Adaptif dan Peningkatan Audio
  • Mous Pixel Watch 4 Akan Hadir dengan Charger Baru dan Fitur-Fitur Terbaru
  • Hati-hati, Video Asli Botol Golda Viral Season 4 Full 6.30 Menit, Cek Link dan Faktanya disini!
  • Google Docs Dapat Update Material 3 dan Desain Ekspresif Baru, Fokus pada Kreativitas dan Kolaborasi
  • Belum Tahu? Ini Trik Hitung Cost Per Gigabyte Supaya Gak Boncos Saat Beli Paket Internet
  • Apple TV dan Google Cast Akan Terintegrasi Lebih Dalam, Tawarkan Pengalaman Streaming yang Lebih Baik
  • Lagi Rame Botol Golda Viral 19 Detik? APA ITU? Jangan Asal Klik Link Sembarangan, Ini Bahayanya!
  • Apa itu Regedit FF Auto Headshot 100 No Password 2024? Simak Definisi dan Cara Kerjanya
  • Apa Itu KPH dan MPH? Pengertian dan Perbedaannya di Game Balap
  • Belum Tahu? Inilah Cara Aman Quotex Login dan Solusi Kalau Error!
  • Apa Itu Platform Modular Intel Alder Lake N (N100)? Ini Pengertian dan Spesifikasinya
  • Apa Itu Armbian Imager? Pengertian Utilitas Flashing Resmi untuk Perangkat ARM Kalian
  • Apa Itu OpenShot 3.4? Pengertian dan Fitur LUT Terbaru untuk Grading Warna
  • Flatpak 1.16.2: Sandbox Baru untuk GPU Intel Xe dan VA-API
  • Apa Itu EmmaUbuntu Debian 6? Pengertian Distro Ringan Berbasis Trixie untuk PC Lawas
  • Apa Itu Nemotron-3 Nano? Pengertian Model Bahasa Ringkas dan Hasil Uji Cobanya
  • Prompt AI Dapur Aestetik
  • Prompt AI Suami Istri Bawa Terong
  • Prompt AI Touring Motor di Stadion GBK
  • Prompt AI Foto Jadul Kebaya Merantau Belanda
  • Apa Itu Surat Panggilan Polisi yang Sah? Ini Ciri-Ciri dan Contohnya
  • Apa Itu Serangan Kredensial IAM (IAM Credential Attack)? Ini Pengertian dan Risiko Fatalnya
  • Apa Itu Serangan Malware Kloning Aplikasi? Ini Pengertian dan Cara Kerjanya
  • Serangan Siber Rusia Targetkan Industri Energi: Sandworm Mengintai
  • Apa Itu Video PT Pabrik Brebes Viral? Ini Pengertian dan Fakta Sebenarnya
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

©2025 emka.web.id | Design: Newspaperly WordPress Theme