ScriptSecuritymacOS

Detect macOS USB drives

Report mounted USB drives on a macOS device.

See what you’re importing.

Read-only preview

Bash Timeout 100s Runs as Local system
#!/bin/bash

# This resource is provided as a convenience for Level users. We cannot 
# guarantee it will work in all environments. Please test before deploying 
# to your production environment. We welcome contributions to our community 
# library

# Level Library
# https://level.io/library/script-macos-monitor-usb-drive

# Look for USB Drives
USB_DRIVE_PRESENT=$(system_profiler SPUSBDataType | grep "Mount Point")

if [[ ! -z "$USB_DRIVE_PRESENT" ]]; then
    # If USB drive is present, send console message for Level to alert on
    echo "ALERT"
fi

Before you run it.

  1. Test the script in a non-production environment to confirm expected output

  2. Use a script-based monitor in Level to generate immediate alerts on detection

  3. Schedule periodic checks with Level Automations for continuous oversight

  4. Consider pairing with a remediation script to disable or eject drives automatically

  5. Keep detailed logs of alerts to identify patterns or repeated policy violations

More about this resource.

What it does

This script uses the built-in system_profiler command to detect any mounted USB drives on a macOS endpoint. If it finds an active USB device, it prints “ALERT,” which can be captured and reported by Level. This enables immediate visibility into unauthorized or unexpected removable media usage.

You can configure a script-based monitor in Level to trigger on-demand checks whenever USB-related activity is suspected, or set it to run on a schedule via a Level Automation. By pairing it with a remediation action, you can automatically disable or eject suspicious drives to maintain tighter security.

When to use it
  • Monitoring high-security environments for unauthorized removable devices
  • Detecting unexpected USB usage in remote work setups
  • Enforcing data protection rules where external drives are restricted
  • Auditing USB device activity on shared or public systems
The problem it solves

Unregulated or unauthorized USB devices can expose macOS systems to data leaks, malware threats, and policy violations. Manually monitoring for USB connections is time-consuming and prone to oversight, leaving IT Professionals and MSPs vulnerable to unexpected security breaches.

Frequently asked questions.

How do I configure this script in Level?

Import it into Level and set up a script-based monitor or an Automation schedule, depending on your preferred usage.

Does the script block or eject USB drives?

No. It only detects them. Pair it with a separate remediation script if automatic removal is required.

Which macOS versions are compatible?

Most modern macOS versions that include system_profiler should work. Testing is advised to ensure compatibility.

Why is an alert triggered only if a USB drive is mounted?

Mounted drives present an immediate data access or transfer risk, which is why the script flags them.

Can I modify the alert message?

Yes. Update the echo statement within the script to suit your organization’s logging or notification needs.

Ready when you are.

No credit card. No sales call. Just sign up and start managing.