⚠ Please read the code before using it because you can lock your USB mouse or keyboard. Add a whitelist of your known devices maybe before running it directly.
I found and modified this script base enough time ago. Maybe you could adapt it quite easily to Raspbian (Edit: I have do it for you finally) and your needs (Edit: but not that).
1) ~$ sudo nano /usr/local/bin/detect-usb.py
#!/usr/bin/python
# base script of lexsi , modified by Ephemeral.
import sys, os, time, re
import os.path
from subprocess import Popen, PIPE
import subprocess
logpath="/tmp/"
# Log information
def log(msg):
path="%slist.usb"%logpath
if os.path.exists(path):
# protect log from group and other RWX
process = Popen(["chmod", "go-rwx", path], stdout=PIPE, stderr=PIPE).communicate()
with open(path,"a") as logfile:
date=time.strftime("%d-%m-%Y %H:%M:%S")
logfile.write("# %s # %s \n" % (date, msg))
# Block a specific device
def block(kernel):
os.system("echo '0' | sudo tee '/sys/bus/usb/devices/%s/power/autosuspend_delay_ms'"%kernel)
os.system("echo 'auto' | sudo tee '/sys/bus/usb/devices/%s/power/control'"%kernel)
# Display pop-up message
def popup(msg):
proc=Popen(msg, shell=True)
proc.communicate()
return proc.returncode
if __name__ == '__main__':
# Get arguments from Udev rules
kernel=re.sub(r"[^0-9-]+", '', sys.argv[1])
devname=re.sub(r"[^A-Za-z0-9/]+", '', sys.argv[2])
manufacturer=re.sub(r"[^A-Za-z0-9]+", '', sys.argv[3])
zenity_msg = "zenity --question --title 'New device detected' --text 'A new device has been plugged in \n\n <b>Informations</b> \n\n <b>Manufacturer:</b>%s\n <b>Devname:</b> %s\n\nShould I accept it?' --display=:0.0" % (manufacturer,devname)
#zenity_msg = "zenity --entry --title 'New device detected' --text 'A new device has been plugged in \n\n <b>Informations</b> \n\n <b>Manufacturer:</b>%s\n <b>Devname:</b> %s\n\nPlease enter the device name' --display=:0.0" % (manufacturer,devname)
# Popup window - ask user for a choice
if popup(zenity_msg):
# Block device
block(kernel)
log("Device: %s - %s was blocked" % (manufacturer, devname))
else:
# Ask for confirmation
zenity_msg = "zenity --question 'Confirmation' --title 'Are you sure?' --display=:0.0"
if popup(zenity_msg) == 0:
log("Device: %s - %s was allowed" % (manufacturer, devname))
else:
# Block
block(kernel)
log("Device: %s - %s was blocked" % (manufacturer, devname))
sudo chmod +x /usr/local/bin/detect-usb.py
2) ~$ sudo nano /etc/udev/rules.d/99-detectusb.rules
ACTION=="add",ENV{DEVTYPE}=="usb_device",SUBSYSTEM=="usb",RUN+="/usr/local/bin/detect-usb.py %k '%E{DEVNAME}' $attr{manufacturer}"
and then:
~$ sudo /etc/init.d/udev reload
3) ~$ nano ~/.bashrc
xhost local:{username}>/dev/null
You can show zenity --entry for an input. Check in the script this commented line and play with that:

zenity_msg = "zenity --entry --title 'New device detected' --text 'A new device has been plugged in \n\n <b>Informations</b> \n\n <b>Manufacturer:</b>%s\n <b>Devname:</b> %s\n\nPlease enter the device name' --display=:0.0" % (manufacturer,devname)
Also, you can find log events in
/tmp/list.usb