Skip to content

Automating Vulnerability Scans with Bash or PowerShell Scripts

  • by

Automating Vulnerability Scans

Vulnerability scanning is an essential part of cybersecurity, helping organizations identify and remediate security weaknesses before attackers can exploit them. While tools like Nessus, OpenVAS, and Qualys provide powerful scanning capabilities, automating these scans with Bash (Linux) or PowerShell (Windows) can streamline the process and improve efficiency. This guide explores how to automate vulnerability scans using scripts and schedule them for regular execution.


Why Automate Vulnerability Scans?

Manually running scans can be time-consuming and prone to errors. Automating scans helps with:

  • Consistency: Ensures regular security checks without manual intervention.
  • Efficiency: Saves time by automating scan execution and report generation.
  • Early Detection: Identifies vulnerabilities before they become threats.
  • Integration: Allows security teams to integrate scanning into CI/CD pipelines.

Automating Vulnerability Scans with Bash (Linux)

Using OpenVAS with Bash

OpenVAS provides a command-line interface for automation. We can use omp (OpenVAS Management Protocol) or gvm-cli to trigger scans from a Bash script.

Example Bash Script for OpenVAS Scanning

#!/bin/bash
# Automate OpenVAS vulnerability scanning
TARGET_IP="192.168.1.1"
TASK_NAME="Automated Vulnerability Scan"
REPORT_NAME="ScanReport_$(date +%F).xml"

# Authenticate with OpenVAS
OPENVAS_USER="admin"
OPENVAS_PASS="yourpassword"

gvm-cli tls --gmp-username $OPENVAS_USER --gmp-password $OPENVAS_PASS --xml '<create_target><name>Target</name><hosts>'$TARGET_IP'</hosts></create_target>'

echo "Starting vulnerability scan on $TARGET_IP..."

gvm-cli tls --gmp-username $OPENVAS_USER --gmp-password $OPENVAS_PASS --xml '<create_task><name>'$TASK_NAME'</name><target>Target</target></create_task>'

echo "Scan complete. Generating report..."

gvm-cli tls --gmp-username $OPENVAS_USER --gmp-password $OPENVAS_PASS --xml '<get_reports format="XML"><report_id>'$REPORT_NAME'</report_id></get_reports>'

echo "Report saved as $REPORT_NAME"

Scheduling the Script with Cron

To run the script automatically every Sunday at midnight:

crontab -e

Add the following line:

0 0 * * 0 /path/to/scan_script.sh

Automating Vulnerability Scans with PowerShell (Windows)

Using Nessus with PowerShell

Nessus provides an API for automating scans. We can use PowerShell to trigger scans and retrieve results.

Example PowerShell Script for Nessus Scanning

# Automate Nessus Vulnerability Scanning
$NessusURL = "https://localhost:8834"
$AccessKey = "your_access_key"
$SecretKey = "your_secret_key"
$Target = "192.168.1.1"
$ScanName = "Automated Scan - $(Get-Date -Format yyyy-MM-dd)"

# Start a new scan
$Headers = @{"X-ApiKeys" = "accessKey=$AccessKey; secretKey=$SecretKey"}
$ScanPayload = @{"uuid"="your_scan_template_uuid"; "settings"=@{"name"=$ScanName; "text_targets"=$Target}} | ConvertTo-Json -Depth 3
Invoke-RestMethod -Uri "$NessusURL/scans" -Method Post -Headers $Headers -Body $ScanPayload -ContentType "application/json"

Write-Output "Scan started for $Target"

Scheduling the PowerShell Script with Task Scheduler

To run the script automatically every Sunday at midnight:

  1. Open Task Scheduler.
  2. Create a New Basic Task.
  3. Set Trigger to run weekly on Sundays at 12:00 AM.
  4. Set Action to “Start a Program” and enter:powershell.exe -File "C:\Path\to\scan_script.ps1"

Comparing Bash vs. PowerShell for Scan Automation

FeatureBash (Linux)PowerShell (Windows)
Best forOpenVAS, QualysNessus, Windows Security Scans
Ease of UseModerateEasy (GUI + API Support)
SecurityStrong (UNIX-based)Secure with API Keys
SchedulingCron JobsTask Scheduler

Best Practices for Automated Vulnerability Scanning

  • Use API Keys & Secure Credentials: Never hardcode passwords in scripts.
  • Limit Scan Frequency: Avoid overloading networks with excessive scans.
  • Review & Act on Reports: Automating scans is useful only if vulnerabilities are remediated.
  • Test Before Deployment: Run scripts manually before scheduling them.

Final Thoughts on Automating Vulnerability Scans

Automating vulnerability scans with Bash (Linux) or PowerShell (Windows) enhances security by ensuring continuous assessments without manual intervention. Whether using OpenVAS, Nessus, or Qualys, integrating automation into your cybersecurity strategy helps proactively detect and mitigate risks.


If you’ve found this article helpful and enjoy learning about cybersecurity automation, consider supporting my work! Your contribution helps me create more free, high-quality content for the community and keeps the site ad-free. Every bit of support allows me to continue sharing knowledge and exploring the ever-evolving world of cybersecurity. If you’d like to support, you can Buy me a coffee. Thank you for your kindness and generosity!

Leave a Reply

Your email address will not be published. Required fields are marked *