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

  • Inilah Bocoran Harga dan Spesifikasi Redmi K90 Max, HP Gaming Dimensity 9500 Termurah yang Segera Dirilis!
  • Inilah RedMagic Gaming Tablet 5 Pro, Monster Gaming dengan Layar 185Hz dan Chipset Snapdragon 8 Elite yang Bikin Melongo!
  • Inilah Kenapa IGRS Trending di X, Bocorkan Spoiler Penting Game 007 First Light Hingga Ending!
  • Inilah 5 HP Murah Terbaru dengan Baterai Jumbo yang Siap Masuk Indonesia 2026, Ada yang Sampai 7.800 mAh!
  • Inilah Alasan Kenapa Samsung Galaxy S27 Bakal Pakai 50 Persen Chipset Exynos, Qualcomm Mulai Ditinggalkan?
  • Inilah Vivo Y31d Pro, Smartphone “Badak” dengan Baterai 7000mAh dan Ketahanan Standar Militer
  • Inilah 11 Game Penghasil Saldo DANA Tercepat 2026, Cara Asyik Cari Cuan Sambil Rebahan!
  • Inilah Motorola Edge 70 Pro, HP Midrange Gahar dengan Baterai 6500 mAh yang Siap Meluncur!
  • Inilah Kode Redeem FF Max 15 April 2026, Buru Skin Undersea Splatter dan Item Eksklusif Bawah Laut Sekarang!
  • Inilah Bocoran Vivo X500 Series dan Kamera Vlogging Saku yang Bakal Bikin DJI Ketar-ketir!
  • Inilah 6 Rekomendasi HP Gaming Paling Murah April 2026, Main Game Berat Nggak Pake Lag!
  • Inilah Caranya Manfaatin SPayLater di Aplikasi ShopeePay Biar Keuangan Tetap Aman Saat Tanggal Tua
  • Inilah Perbedaan Durasi Reels IG dan FB Terbaru 2026, Ternyata Bisa Sampai 20 Menit Lho!
  • Inilah Cara Mengurus Surat Keterangan Bebas Narkoba (SKBN), Syarat Lengkap, Biaya, dan Lokasi Pembuatannya
  • Inilah Cara Pakai Panel Zurra v2 FF Aim Lock Headshot, Simak Fitur dan Risikonya Sebelum Pakai!
  • Inilah Alasan Kenapa Sesi Akhir UTBK 2026 Nggak Lebih Sulit dan Penjelasan Lengkap Sistem Penilaiannya
  • Inilah Kenapa Tidak Bisa Login WA Web dan Solusi Paling Ampuh Biar Langsung Connect Lagi
  • Inilah Cara Mengatasi MyTelkomsel Oops Terjadi Kesalahan, Jangan Panik Dulu!
  • Inilah Cara Mengisi SKP Satpol PP dan Pranata Trantibum 2026 di e-Kinerja My ASN, Dijamin Anti Ribet!
  • Inilah Cara Akses IPTV Playlist M3U Indonesia Terbaru 2026, Nonton Ribuan Channel Tanpa Biaya!
  • Tutorial SEO Anchor Link: Cara Dapat Ranking di Google Lewat Strategi Link Building yang Aman
  • Inilah Huawei Pura 90 Pro, HP Flagship dengan Desain Kamera Segitiga Unik dan Performa Kirin yang Makin Gahar
  • Inilah 5 Rekomendasi Kipas Tangan Portable Cas Tahan Lama Biar Kalian Tetap Adem Saat Cuaca Panas Ekstrem
  • Inilah MacBook Neo, Laptop Termurah Apple yang Ternyata Punya Performa Gaming Gila!
  • Inilah Daftar Tablet SIM Card 5G dengan Keyboard Terbaik 2026 untuk Kerja Remote
  • Inilah Daftar Game Penghasil Pulsa 2026, Cara Cerdas Dapat Kuota Gratis Tanpa Keluar Duit Sepeserpun!
  • Inilah Caranya Klaim Bonus Kuota Lokal Smartfren 2026, Jangan Sampai Bonus Kalian Hangus Sia-sia!
  • Inilah Cara Mengatasi Rockstar Games Launcher Not Responding dan Macet Saat Connecting
  • Inilah Cara Cek HP Masih Kredit atau Sudah Lunas Biar Kalian Nggak Ketipu Pas Beli Bekas
  • Inilah Cara Pakai Astute FF Beta Apk 2026, Rahasia Unlock Server 2.0 Tanpa Verifikasi!
  • Is it Time to Replace Nano? Discover Fresh, the Terminal Text Editor You Actually Want to Use
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to Fine-Tune Gemma 4 Model: A Beginner Tutorial on Using Unsloth for Faster Training
  • How to Run Your Entire Business with an AI Agency: A Complete Tutorial on SureThing 2.0
  • How to Build Your Own Digital Workforce with Factory AI Droids
  • MiniMax M2.7 Tutorial: Mastering the 229B Parameter MoE Model for Advanced AI Development
  • Complete Tutorial on Gemma 4: Build and Automate Anything Locally for Free with Google’s New AI
  • 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