by John Gagne (zerocool)
(Note: This article uses KDE, but it should be easily adapted for use under other DEs.)
My son is a 1st Lieutenant in the US Army. He was issued a smart ID card and reader which allows him to logon to secure sites. I thought that was pretty cool! The coolest thing I had on my military ID card was a QR code. I've seen other "smart cards," both chip and RFID, thumb drive keys, fingerprint scanners, even face recognition all used for access identification and unlocking privileged computing resources of some kind or another. But it was seeing my son's smart card that got me thinking how to automatically unlock my screen with (what else) my cell phone. Needless to say, I wanted my solution to be even cooler than my son's ID card. And in my humble and totally unbiased opinion, this is way cooler.
Basically, the script pings your local network for your smart phone's IP address and if it's found, unlocks the screen. If it's not found, it locks the screen, instead.
So, why a cell phone? It seems an obvious choice. We all have one and we keep them with us all the time. Where we go, so do our phones. Our phones have become ubiquitous with our immediate presence or absence, and smart enough to make that presence or absence "detectable." When I searched, I found an android app that accomplishes the unlock via bluetooth. I didn't try it for several reasons, not the least of which being that we generally don't keep bluetooth on because it kills the battery. But wifi, on the other hand, we use so much. It's always either on or controlled by the device in sleep mode, but even then it's always listening for the access points it knows (home, work, Starbucks,...) and when these are detected, it automatically connects. So, all we really have to do is teach our computers to accomplish this detection and unlock the screen for us.
So, why use a bash script? Again, It seems an obvious choice. Besides, practicing a little bash kung-fu is never a waste of time! It's extremely powerful and available on all Linux machines.
First we have to do some simple housekeeping on our phones. We need to set a static IP address. You can use any unused address that's outside the scope of your DHCP bank. For most standard home routers, DHCP addresses generally start at 192.168.1.100 and run through at least 192.168.1.150. Generally speaking, any IP between 192.168.1.20 - 192.168.1.90 should be good and if it's not -- odds are you already know what IP address to use. Once you have an IP address in mind, follow this guide to set the static IP address.
For android phones, go to: goo.gl/oMaVvx
For iPhone phones, go to: goo.gl/q1SUTj
Lastly, set your phone's wifi to "stay on when charging". Then when you're using the computer, plug the phone in to charge. That way, the wifi won't shut off in sleep mode and lock your computer screen.
Next, let's perform a little housekeeping on your computer. Create a folder called bash, and then inside the bash folder, create another folder called lock. Then open an editor and copy and paste the following code:
Edit <user_name> with your user name. In my case it would look like:
Edit <ip_address of your phone> with the static ip address you set your phone to. In my case it looks like this:
Notice it's inside quotes.
Edit <welcome message> with the system message to be sent when your phone is detected, in my case it looks like this:
MESS_TXT=" Welcome Cool "
Again, notice it's inside quotes and the starting and ending spaces. Save the file as ~/bash/lock/lock.sh
Next, we want an audible indication of recognition when the computer detects the presence of our phone. I created an mp3 file with an online text to speech engine and then downloaded the file from here:
Create your own or you can download mine here: goo.gl/uDK6Hv
Save the file as ~/bash/lock/welcome_home.mp3.
Last but not least, open your editor again and paste in the following text:
Edit <user_name> and save the file as ~bash/lock/lock.desktop. To manually start the script, navigate (via CLI or Dolphin) to ~/bash/lock/ and run lock.desktop. If you want the script to auto start on boot, then also save the file as ~/.kde4/Autostart/lock.desktop.
While it's certainly not Fort Knox with respect to security, if you're like me and didn't use a screen locker at all, this solution is more secure than none. Besides, it's kind of cool to walk in and be greeted by your computer.