Reconnaissance and Enumeration · Topic 6 of 6

Script Modification for Recon

Learn to read and adapt simple Bash, Python and PowerShell scripts for recon: variables, loops, conditions, data structures and safe testing habits.

  • 5 min
In a nutshell
  • Know the language each shell belongs to: Bash on Linux and macOS, PowerShell on Windows, Python as a cross-platform language.
  • Be able to identify variables, conditionals, loops, functions and error handling in a short script.
  • Know common formats for data and settings: JSON, CSV and plain text lists.

What it is

Testers often start from an existing script and change it to fit the job: new targets, a different port, another output format. The exam does not ask you to write large programs. It asks you to read a short script in Bash, Python or PowerShell, understand its logic and spot what to change.

This is a code reading skill. Always test a modified script on a lab or a single in-scope host first.

Existing scriptfrom a trusted sourceRead and understandlogic and inputsModifytargets, options, outputTest safelylab or one host

How it works

Core building blocks

Every language in scope uses the same ideas.

ConceptMeaningExample idea
VariableA named valueA target address stored once
ConditionalRun code only if a test is trueIf a port answers, print it
LoopRepeat for each itemFor each host in a list
FunctionReusable named blockA routine that checks one host
Error handlingDeal with failuresSkip a host that times out
Input and outputRead files, print or save resultsRead hosts.txt, write results.csv
Library or moduleReady-made codeA networking or parsing library

Common data structures are the list (an ordered set of items), the dictionary (named key and value pairs), and the array. Data is often exchanged as JavaScript Object Notation (JSON) or comma-separated values (CSV).

Operators

Scripts combine and compare values with operators, and the three languages use slightly different syntax.

Operator typePurposeExample idea
BooleanCombine conditions with and, or and notAct only if a host is up and a port is open
StringJoin, compare, match or split textBuild a URL, or split a line into fields
ArithmeticCalculate and countIncrement a counter, or compute a port range

For example, Bash uses && for and, Python uses the word and, and PowerShell uses -and. A combined condition might act only when a port is open and the host is in scope.

Bash example

Bash is the shell on most Linux systems. This harmless loop checks a short list of in-scope hosts with ping.

#!/bin/bash
# Linux shell: check whether listed hosts answer one ping
while read -r host; do
  if ping -c 1 -W 1 "$host" > /dev/null 2>&1; then
    echo "$host is up"
  else
    echo "$host did not answer"
  fi
done < hosts.txt

The while read loop takes one host per line from hosts.txt. The if tests the result of ping, and > /dev/null hides its normal output. To adapt it, you could change the input file, the wait time -W, or write results to a file with >>.

Python example

Python is popular for tools because of its libraries. This script reads a JSON file of hosts and tries one TCP connection to each.

# Python 3: check whether a TCP port accepts connections
import json
import socket

with open("targets.json") as f:
    targets = json.load(f)       # e.g. {"hosts": ["192.0.2.10"], "port": 443}

for host in targets["hosts"]:
    try:
        with socket.create_connection((host, targets["port"]), timeout=2):
            print(host, "port open")
    except OSError:
        print(host, "no connection")

Here import loads libraries, a dictionary holds the settings, and try and except handle failures so one dead host does not stop the loop. Changing the port or the timeout means editing the JSON file or the timeout value.

PowerShell example

PowerShell is the Windows automation shell, and it works with objects rather than plain text. The cmdlet Test-NetConnection checks a port.

PowerShell:
Get-Content .\hosts.txt | ForEach-Object {
    Test-NetConnection -ComputerName $_ -Port 445 -WarningAction SilentlyContinue |
        Select-Object ComputerName, TcpTestSucceeded
}

The pipe sends each line of the file to the loop, and $_ stands for the current item. The output is a small table showing whether the connection worked.

What to check before running a modified script

  • Does it only touch addresses from the approved scope?
  • How many requests per second will it send, and is there a delay?
  • Does it write credentials or sensitive data to a file or the screen?
  • What happens when a target does not respond (error handling)?
  • Does it need administrator rights, and what could it change?
Watch out

Scripts from the internet can contain harmful code, so read every line and run it in a lab before using it on a client network.

What to remember for the exam

  • Know the language each shell belongs to: Bash on Linux and macOS, PowerShell on Windows, Python as a cross-platform language.
  • Be able to identify variables, conditionals, loops, functions and error handling in a short script.
  • Know common formats for data and settings: JSON, CSV and plain text lists.
  • A loop that reads from a file is the standard way to run a task against many hosts.
  • Try and except (Python) or similar blocks stop one failure from ending the whole run.
  • Libraries and modules supply ready-made features such as network sockets and parsing.
  • Change targets, ports, timeouts and output files, and test in a lab first.
  • Scope and rate limits matter just as much for scripts as for tools.

Common mistakes

Running a script without reading it. Hidden commands can delete files, open backdoors or send data away.

Hard-coding addresses. A script that includes the wrong range may touch out-of-scope hosts, so read targets from a reviewed input file.

Skipping timeouts. A missing timeout makes a script hang on the first unresponsive host.

Mixing language syntax. A PowerShell variable starts with $, while a Python variable has no prefix, and tabs or indentation matter in Python.

Saving secrets in plain text. Output files holding passwords must be protected or avoided.

Check yourself

  • Which structure repeats an action for each host in a list?

    A loop.

  • Why use error handling in a recon script?

    So that one failed or unresponsive host does not stop the whole run.

  • Which shell is native to Windows automation?

    PowerShell.

Topic 6 of 6 in Reconnaissance and Enumeration

Test yourself on Reconnaissance and Enumeration

The full CompTIA PenTest+ PT0-003 question bank has 653 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by CompTIA. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.