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

  • Daftar Sekarang! Beasiswa S2 di Italia dari IYT Scholarship 2026 Sudah Dibuka
  • Sejarah Hantavirus dan Perkembangannya Sampai ke Indonesia
  • Kementerian Pendidikan: Mapel Bahasa Inggris Wajib di SD Mulai 2027!
  • Ketua Fraksi PKB MPR-RI: Kemenag Respon Cepat Pendidikan Santri Ndolo Kusumo Pati yang Terdampak
  • Viral Video Sejoli Di Balai Kota Panggul Trenggalek, Satpol PP Janji Usut
  • Video Viral Wakil Wali Kota Batam Tegur Keras Pasir Ilegal
  • LPDP Buka Peluang Beasiswa S3 Prancis 2026, Simak Syaratnya!
  • Inilah Panduan Lengkap dan Aturan Main Seleksi Penerimaan Murid Baru (SPMB) SMA dan SMK Negeri Jawa Tengah Tahun 2026
  • Inilah Syarat dan Cara Daftar MOFA Taiwan Fellowship 2027
  • RESMI! Inilah Macam Jalur di SPMB Sekolah Tahun Ajaran 2026
  • Ini Loh Rute Terbaru TransJOGJA Per Mei 2026, Jangan Salah Naik!
  • Inilah Jadwal Operasional MRT Jakarta Per Mei 2026, Berubah Dimana?
  • Inilah Syarat dan Mekanisme Seleksi Siswa Unggul ITB Jalur Talenta (OSN, Seniman, Hafidz, Atlet dll) 2026/2027
  • Inilah Daftar Saham Farmasi di BEI Per Mei 2026, Pilih Mana?
  • Kesehatan Mental Itu Penting: Inilah Isi Chat Terakhir Karyawan Minimarket Sukabumi Bundir
  • Inilah Kampus Swasta Terbaik Jurusan Farmasi di Area Malang Raya
  • Cara Login EMIS 4.0 Kemenag Terbaru 2026 Pakai Akun Lembaga dan PTK Guru Madrasah Aktivasi
  • Survei Parpol Terbaru: Gerindra Unggul, PDIP Ketiga, PKB 5%
  • PKB Resmi Jalin Kerjasama dengan Institut Teknologi & Sains NU Kalimantan
  • Inilah Urutan Terbaru Pangkat TNI Angkatan Darat! (Update 2026)
  • Inilah Panduan Lengkap Operator Sekolah Mengelola SPTJM e-Ijazah dan Menghindari Kesalahan Fatal Data Kelulusan
  • Inilah Syarat dan Penilaian Seleksi Siswa Unggul ITB Jalur UTBK
  • Download Video Viral Guru Bahasa Inggris? Awas Berisi Virus!
  • PKB Minta Kasus C4bul Pendiri Ponpes Pati Tidak Ada Ampunan & Tuntutan Maksimal
  • Inilah Kronologi Video Viral Preman vs Sopir Di Sumedang
  • Ini Alasan UKP Pariwisata Disindir Konten Kreator Drone Gunung Rinjani
  • Inilah Kronologi Viral Video Dugaan Asusila Pegawai Disdik Pasuruan di Mobil Dinas
  • Polisi Polda Sumut Resmi Dipecat: Dari Video Viral Sampai Sidang Etik Ini Kronologinya
  • ASUS ExpertBook Ultra: Produk Flagship yang Cerminkan Kepemimpinan ASUS di Pasar Global
  • Inilah Tahapan dan Syarat Pendaftaran Beasiswa Garuda 2026 Gelombang II (25 Mei – 25 Juni 2026)
  • How to build a high-performance private photo cloud with Immich and TrueNAS SCALE
  • How to Build an Endgame Local AI Agent Setup Using an 8-Node NVIDIA Cluster with 1TB Memory
  • How to Master Windows Event Logs to Level Up Your Cybersecurity Investigations and SOC Career
  • How to Build Ultra-Resilient Databases with Amazon Aurora Global Database and RDS Proxy for Maximum Uptime and Performance
  • How to Build Real-Time Personalization Systems Using AWS Agentic AI to Make Every User Feel Special
  • How to Optimize Your AI Agent Using Compiled Knowledge Layers to Replace Traditional RAG Systems
  • How to Build Your Own AI SEO Tool for Free and Audit Websites Like a Professional Marketing Agency
  • How to Use Tinyfish.ai to Give Your AI Agents Real-Time Web Access for Free
  • How to Use Anthropic’s New AI Agents to Automate Your Financial Workflows Like a Pro
  • How to Secure Brand Deals Easily Using AI-Powered Professional Pitch Estimates in Just a Few Clicks
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025

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