Are you looking for a way to automatically run a Python script as soon as you insert a USB drive into a computer? This can be useful for a variety of purposes, such as running a script to backup data, or to launch a presentation when you plug in a USB drive with your slides. In this article, we will show you how to autorun a Python script from USB insertion on both Windows and Linux systems.
Prerequisites
Before we begin, there are a few things you need to have in place:
- A USB drive with enough space to store your Python script
- A Python script that you want to run automatically
- Administrative access to the computer where you want to autorun the script
Autorun a Python Script on Windows
To autorun a Python script on Windows, you need to create an autorun.inf file in the root directory of your USB drive. This file tells the Windows operating system to run a specific program or script when the USB drive is inserted. Here's how to create the autorun.inf file:
- Create a new text file on your USB drive and name it autorun.inf
- Open the autorun.inf file in a text editor, such as Notepad
- Add the following lines to the autorun.inf file:
[autorun] open=python.exe /path/to/your/script.py - Save the autorun.inf file and close the text editor
In the above code, replace python.exe with the path to the Python executable on the computer where you want to run the script. Replace /path/to/your/script.py with the path to your Python script on the USB drive. For example, if your Python script is located in a folder called scripts on the root of your USB drive, the line would look like this:
open=C:\Python39\python.exe E:\scripts\script.py
Note that the path to the Python executable and the path to your script may be different depending on your system configuration.
Once you have created the autorun.inf file, you can test it by inserting the USB drive into the computer. The Python script should automatically run without any user intervention. If the script does not run, make sure that the path to the Python executable and the path to your script are correct.
Autorun a Python Script on Linux
To autorun a Python script on Linux, you need to create an udev rule. Udev is a device manager for the Linux kernel that allows you to run scripts when a device is added or removed. Here's how to create the udev rule:
- Create a new file in the
/etc/udev/rules.ddirectory. You can use any text editor, such as nano or vim. For example, to create a file called99-usb-autorun.rules, you can use the following command:sudo nano /etc/udev/rules.d/99-usb-autorun.rules - Add the following lines to the udev rule file:
ACTION=="add", SUBSYSTEM=="block", ATTR{ro}=="0", DRIVERS=="usb-storage", RUN+="/usr/bin/python3 /path/to/your/script.py" - Save the udev rule file and close the text editor
In the above code, replace /usr/bin/python3 with the path to the Python executable on the Linux system where you want to run the script. Replace /path/to/your/script.py with the path to your Python script on the USB drive. For example, if your Python script is located in a folder called scripts on the root of your USB drive, the line would look like this:
ACTION=="add", SUBSYSTEM=="block", ATTR{ro}=="0", DRIVERS=="usb-storage", RUN+="/usr/bin/python3 /mnt/usb/scripts/script.py"
Note that the path to the Python executable and the path to your script may be different depending on your system configuration. Also, the udev rule assumes that the USB drive is mounted at /mnt/usb on the Linux system. You may need to modify this path depending on your system configuration.
Once you have created the udev rule, you can test it by inserting the USB drive into the Linux system. The Python script should automatically run without any user intervention. If the script does not run, make sure that the path to the Python executable and the path to your script are correct.
Security Considerations
Autorunning scripts from USB drives can be a security risk, as it allows untrusted code to be executed on your computer. Therefore, it is important to only autorun scripts from trusted sources. If you are unsure about the source of a script, it is better to be safe and not autorun it.
You can also limit the autorun feature to specific USB drives by adding a condition to the autorun.inf file or the udev rule. For example, you can specify a unique identifier for the USB drive, such as the serial number or the vendor ID and product ID. This way, only the specified USB drive will autorun the script.
In this article, we have shown you how to autorun a Python script from USB insertion on Windows and Linux systems. By following the steps outlined in this article, you can create an autorun.inf file or a udev rule that automatically runs your Python script when the USB drive is inserted. However, it is important to remember the security implications of autorunning scripts from USB drives, and to only autorun scripts from trusted sources.
References
| Title | URL |
|---|---|
| How to autorun a program on Windows when a USB drive is inserted | https://www.howtogeek.com/howto/windows-vista/usb-flash-drive-autorun-in-windows-vista/ |
| How to autorun a Python script on Linux using udev | https://www.linux.com/training-tutorials/how-autorun-python-script-linux-using-udev/ |
| How to find the serial number of a USB drive on Windows | https://www.howtogeek.com/howto/windows-vista/using-windows-command-prompt-to-find-the-serial-number-of-your-usb-drive/ |
| How to find the serial number of a USB drive on Linux | https://www.cyberciti.biz/faq/how-to-find-usb-storage-device-serial-number-in-linux/ |