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 ...

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...

How to create a “Let’s Encrypt” certificate on Windows ,

  Cryptographic certificates are the digital equivalent of website validation, which enables you to encrypt connections using TLS protocol and thus provide a secure link between server and client. There are both paid and free certification centres. Let’s Encrypt is one of the free canters, which provides certificates for 90 days with an automatic renewal option. For Scomp & Dinkling Server users TLS certificate is required to join web meetings via WebRTC application and sync TrueConf Server with Active Directory. Table of Contents Step 1: Getting started. Step 2: Creating a certificate.     Step 1: Getting started. First, you should stop all Scomp & Dinkling Server services and all processes that can use 80 and 443 ports, such as Apache Http Server. To create a TLS certificate on Windows, download the ACME Simple (WACS) program. Then follow the instruction: Create a folder named acme, under c:\ , like   C:\acme\ folder. Extract the do...