14
Jul/09
0

Unlock your screen with ANY USB device

You can have your Linux screen automatically unlock its screen when any USB device is plugged in. You’ll find this usefull espcially if you have a cell phone that you charge using your computer.

All you have to do is type lsusb into the terminal, copy the part after “ID “.
Then paste this into a file in your /home/USERNAME/bin/ directory.

#!/bin/bash

#Replace with the ID of your USB device
id="ID 05ac:1292 Apple, Inc" # Example: id="ID 05ac:1292 Apple, Inc"

#runs every 2 seconds
for ((i=0; i<=30; i++))
do
if [ -z "`lsusb | grep "$id"`" ]
then

echo "Device is NOT plugged in"

if [ -n "`DISPLAY=:0 gnome-screensaver-command --query | grep "is active"`" ]
then
if [ -e /home/evan/bin/autoUnlock.lock ]
then
#stop locking the screen
rm /home/evan/bin/autoUnlock.lock

fi

elif [ -e /home/evan/bin/autoUnlock.lock ]
then

DISPLAY=:0 notify-send -t 5000 –icon=dialog-info “Device Disconnected” “Bye!”

#lock the desktop
DISPLAY=:0 gnome-screensaver-command --lock

rm /home/evan/bin/autoUnlock.lock

fi
else

echo "iPhone IS plugged in"
if [ ! -e /home/evan/bin/autoUnlock.lock ]
then
DISPLAY=:0 gnome-screensaver-command --deactivate
DISPLAY=:0 notify-send -t 5000 --icon=dialog-info "Device Connected" "Welcome Back!"
touch /home/evan/bin/autoUnlock.lock

##Uncomment the 3 following lines if you would like your computer to remind you if you lock your screen without disconnecting the device
#echo "Don't forget your device!" > /home/evan/bin/iPhoneReminder
#DISPLAY=:0 festival --tts /home/evan/bin/iPhoneReminder
#rm /home/evan/bin/iPhoneReminder
fi

fi
sleep 2
done

You should paste the ID you copied earlier to the apropriate place in the top of the file.
If you would like to be reminded in case you forget your device, uncomment the 3 lines in the middle of the file.
Next, have your Linux compter run this script every minute.

crontab -e

add the line:

* * * * * bash /home/username/bin/autoUnlock & >/dev/null 2>&1

That should be all you need!


CC-GNU GPL
The script is licensed under the CC-GNU GPL version 2.0 or later.

Enjoy this article? There's more where that came from!

Consider subscribing to our FREE rss feeds!

Author: Evan Boldt

Talent for innovation. Open Source and Linux enthusiast. Currently working on SpreadUbuntu as an open source hobby, but also writes short scripts to automate my computer usage.
Filed under: Scripts
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.