Skip to main content

Bash Scripting Tutorial – Linux Shell Script and Command Line for nmap automate

 Ever wondered how Linux professionals automate tasks so effortlessly?

The secret lies in Bash scripting — one of the most powerful skills you can learn as a beginner in Linux.

💡 What you’ll learn in this tutorial:
✔️ Basics of Linux command line
✔️ How to create and run your first shell script
✔️ Understanding variables, loops, and conditions
✔️ Automating daily repetitive tasks
✔️ Real-world examples to build confidence

🧠 Why Bash scripting?
Because it transforms you from someone who uses commands…
into someone who controls the system.

Whether you're stepping into IT, cybersecurity, or DevOps — this is your foundation.

⚡ No heavy theory. No confusion.
Just clear, practical learning for beginners.

👉 Start now and take your first step toward mastering Linux.



🛠️ Automated Nmap Scan Script (Beginner → Practical)


This is the command , for bash script .


It require the scan type



Final result will be on the same drive.


Now the script is below .



#!/bin/bash


# ==============================

# Nmap Automation Script

# ==============================


# Create output directory with timestamp

timestamp=$(date +"%Y-%m-%d_%H-%M-%S")

output_dir="nmap_scan_$timestamp"


mkdir -p "$output_dir"


echo "=============================="

echo " Nmap Automation Script Start "

echo "=============================="


# Ask user for target

echo "Enter target IP or subnet (e.g. 192.168.1.1 or 192.168.1.0/24):"

read target


# Ask scan type

echo "Select Scan Type:"

echo "1) Quick Scan"

echo "2) Full Port Scan"

echo "3) Stealth Scan"

read choice


# Define scan based on user choice

case $choice in

    1)

        scan_type="-T4 -F"

        scan_name="quick_scan"

        ;;

    2)

        scan_type="-p- -T4"

        scan_name="full_scan"

        ;;

    3)

        scan_type="-sS -T4"

        scan_name="stealth_scan"

        ;;

    *)

        echo "Invalid choice. Exiting..."

        exit 1

        ;;

esac


# Run Nmap scan

echo "Running Nmap scan on $target ..."

nmap $scan_type $target -oN "$output_dir/${scan_name}.txt" \

                       -oX "$output_dir/${scan_name}.xml" \

                       -oG "$output_dir/${scan_name}.gnmap"


# Check if scan was successful

if [ $? -eq 0 ]; then

    echo "Scan completed successfully!"

else

    echo "Scan failed!"

    exit 1

fi


# Extract open ports

echo "Extracting open ports..."

grep "open" "$output_dir/${scan_name}.gnmap" > "$output_dir/open_ports.txt"


# Summary

echo "=============================="

echo " Scan Completed "

echo "Results saved in: $output_dir"

echo "Open ports saved in: open_ports.txt"

echo "=============================="

▶️ How to Use

chmod +x nmap_auto.sh
./nmap_auto.sh

🧠 What This Script Actually Does (Important)

This is not just a script — it mimics real-world security workflow:

✔️ Creates timestamped scan folders

→ Like SOC evidence storage

✔️ Supports multiple scan types

→ Quick / Full / Stealth

✔️ Saves output in 3 formats

  • .txt → human readable
  • .xml → for tools (ELK/Splunk integration)
  • .gnmap → grep-friendly

✔️ Extracts open ports automatically

→ Useful for alerting & correlation

Comments

Popular posts from this blog

How to Reset Forgotten Password on Kali Linux

          Kali Linux is a Linux distribution used in the Cybersecurity domain. It is maintained and funded by Offensive Security. Kali Linux is Debian based and it uses the Debian repository for most of its packages. This Linux distribution is designed for digital forensics and penetration testing. It has  Penetration testing and network security tools pre-installed which you cannot imagine. It is completely free and open source. So you can use it for free and even contribute to its development.         Now forgetting login credentials is an annoying thing in the case of any operating system. Resetting forgotten passwords often comes with the risk of data loss and requires a lot of effort if you are not a technology enthusiast. This article will be a simple step-by-step guide on resetting forgotten passwords on Kali Linux. How to Reset Forgotten Password on Kali Linux?           In this section, we will ...

How to Start Bluetooth Services in Linux & Kali Linux (Step-by-Step Guide/Fix Bluetooth Not Working)

       If your Bluetooth is not working in Kali Linux or any Linux system, this guide will help you start Bluetooth service in Linux using simple command line steps…              Bluetooth is an essential feature in modern Linux systems—whether you're connecting wireless headphones, transferring files, or pairing devices for pentesting labs in Kali Linux. But unlike Windows, Bluetooth in Linux doesn’t always “just work” out of the box. If you’ve ever faced issues where Bluetooth won’t start, devices aren’t visible, or services seem inactive—this guide is exactly what you need. 🚀 Why Bluetooth Fails to Start in Linux? Before jumping into commands, let’s understand the common causes: Bluetooth service is disabled or inactive Required packages are not installed Systemd services are not running Hardware is blocked (rfkill) Kernel modules are not loaded 🛠️ Step 1: Check Bluetooth Service Status First, verify whether the Bluetooth servi...

Office 365: How to force users to change their password /O365

First we will see how to do it for one account then we will see how to do it for multiple accounts. 1. Install Prerequisites:       We will need MSOnline module to connect to Office 365 with PowerShell.         Open a PowerShell prompt with administrator rights : 2. Install MSOnline module :               It will Also install NuGet Provider , accept and yes to continue.   3.Connect to Office 365     Use Connect-MsolService  to open the Microsoft Office 365 sign in window :       Now a Pop Up windows will open For Microsoft Oath2 Security , Name and password of Administrative user or Global admin must be given here for making any changes.     4.Reset user password :           4A.Force new password :                        With this command we can set a password for a...