Skip to content

Øvelse 11 - Forstå bash script + tilrette script

Forfatter: Mathias Greisen

Dato: 2026-10-05

Fag/modul: Introduktion til IT-Sikkerhed

Status: Færdig


1. Beskrivelse af øvelsen

At kunne læse, forstå, tilrette og oprette bash scripts.


2. Reproduktion trin for trin

Forudsætninger

  • linux med nmap installeret

Trin

  1. Opret nmapScans.sh

    nano nmapScans.sh
    
  2. Indtast script

    #!/bin/bash
    
    #Add color to the shell output https://techstop.github.io/bash-script-colors/
    RED='\033[0;31m'
    WHITE='\033[0;37m'
    NC='\033[0m' # no color
    
    echo -e "${RED}RUN THIS SCRIPT AS SUDO USER${WHITE}"
    
    if [ $# -eq 1 ]; then
        echo -e "Target ${RED}IP:$1${WHITE}"
    else
        echo "Your command line is incorrect"
        echo -e "Usage example: ${RED} sudo ./nmapScans.sh 10.10.10.121 ${WHITE}"
        exit 1
    fi
    
    #Get the date, will be used in filename
    DATE=`date +%Y-%m-%d`
    filename=$1_$DATE
    
    echo -e "Running a ${RED} quick scan ${WHITE}..."
    nmap -T4 -F $1 > quick_nmapscan_$filename.txt
    echo -e "Results -> ${RED}quick_nmapscan_$filename.txt${WHITE}"
    
    exit 0
    
  3. Rediger script til output af åbne porte til konsollen

    #!/bin/bash
    
    #Add color to the shell output https://techstop.github.io/bash-script-colors/
    RED='\033[0;31m'
    WHITE='\033[0;37m'
    NC='\033[0m' # no color
    
    echo -e "${RED}RUN THIS SCRIPT AS SUDO USER${WHITE}"
    
    if [ $# -eq 1 ]; then
        echo -e "Target ${RED}IP:$1${WHITE}"
    else
        echo "Your command line is incorrect"
        echo -e "Usage example: ${RED} sudo ./nmapScans.sh 10.10.10.121 ${WHITE}"
        exit 1
    fi
    
    #Get the date, will be used in filename
    
    echo -e "Running a ${RED} quick scan ${WHITE}..."
    
    echo ""
    
    nmap -T4 -F $1 | grep open
    
    exit 0
    
  4. Gem og afslut nano

    • Gem
      • CTRL + S
    • Afslut
      • CTRL + X
  5. Give tilladelse til at eksekvere

    chmod +x nmapScans.sh
    
  6. Kør script

    sudo bash nmapScans.sh
    

3. Problemer og løsninger

Jeg oplevede ikke problemer under denne øvelse.


4. Anvendte ressourcer


5. Opsummering af erfaringer

Jeg har fået praktisk erfaring med at læse, forstå, tilrette og oprette bash scripts, samt at kunne formatere output via pipeline og grep.