This Python script (driveid.py) is designed to monitor physical disk drives on a Windows machine in real-time. It detects when a USB drive or other removable storage device is plugged in or unplugged, and for newly connected drives, it attempts to list the files and folders in its root directory.
- Real-Time Monitoring: Continuously checks for changes in connected physical disk drives.
- Plug/Unplug Detection: Clearly notifies the user when a device is plugged in or unplugged.
- Drive Content Listing (for new drives): Attempts to list the root-level files and folders of newly detected drives.
- Robust Error Handling: Gracefully handles common issues like permission denied errors, unformatted drives, or inaccessible paths during file listing, preventing script crashes.
- Efficient WMI Usage: Optimizes WMI (Windows Management Instrumentation) calls by initializing the client once and reusing it, reducing system overhead.
- Security Auditing: Keep track of unauthorized drive connections.
- System Diagnostics: Monitor drive activity and detect changes in storage devices.
- Automation Trigger: Can be extended to trigger other actions upon drive connection/disconnection.
-
Clone the Repository:
git clone <repository-url> cd drive-id
-
Install Dependencies: This script relies on the
wmiPython package. Install it using pip:pip install -r requirements.txt
(This will install the
wmipackage, as specified inrequirements.txt). -
Run the Script: Execute the script from your terminal or command prompt:
python src/driveid.py
-
Monitor Output: The script will start monitoring immediately, displaying information about any drive changes in real-time. Press
Ctrl+Cto stop the script.
Press 'Ctrl+C' to quit the program at any time.
Scanning initial drives...
Monitoring for changes...
Device \\.\PHYSICALDRIVE1: was PLUGGED IN
Contents of drive D:\
[Folder] MyPhotos
[File] document.txt
Device \\.\PHYSICALDRIVE1 (Drive D:): was UNPLUGGEDThis project is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! Feel free to open an issue or submit a pull request with your improvements or bug fixes.