Auto-Locking PC when you’re away with Python
Full disclosure, this is not going to be the most secure way to lock you’re pc, but it is pretty cool. It is also designed on a windows pc, but with some tweaking will work fine on linux or osx.
The idea behind this is that we can choose any device as our ‘key’, and when this device is no longer connected to your home network, your pc will automatically lock. In this example I will be using my phone as the key, so if I leave the house but forget to lock my laptop, it will lock itself once my phone disconnects from my WiFi, pretty neat!
This little python project couldn’t be simpler and can be done in less than 20 lines of code! To start off with we only need to import two libraries, both of which are built into python, so no installing of external packages which is a bonus. The two packages we need are ‘subprocess’ and ‘ctypes’, which give python access to windows command prompt and system processes.
import subprocess
import ctypes
Next you need to decide which device you want to act as your key. Ideally it would be a device that you take out the house with you, so a phone works perfectly, or perhaps a smartwatch if you have one, but it is entirely up to you.
For whatever device you choose, you need to find out what it’s called on your network, and the best way is to log onto your router, and find the device you would like to use, then define it as a variable in our python script.
my_device = 'Toms-iPhone'
We also need to set the default state on start-up for the connection, which will be False.
connected = False
So now we need to get into the main function that makes this all possible, which we will call ‘check_connection’. In this function we will be using the ‘subprocess’ package to write shell commands which ping the local network for the device we want to find. The start of the function looks like this:
def check_connection():
check = subprocess.run(f'ping {my_device}',
stdout=subprocess.PIPE,shell=True) check = check.stdout.decode('utf-8')
state = check.split('\r')[2].split()
All this part of the code does is send a ping for any device that matches the name we defined earlier, and return the shell output. The shell output is one long string so we decode it, and split it into something more recognisable and manageable, and this can be done in many ways.
Now we need to check the output to see if it has found our device on the network, so we just need an IF statement that checks if the output says the device is ‘unreachable.’ at the end.
if state[-1]=='unreachable.':
connected = False
else:
connected = True
return connected
And that completes our network check! We now have a function that can be called at any time to scan the network for our ‘key’.
The final part to add, is to continuously check the connection status of our device, and make it lock the PC when the connection value is False. To do this we will simply use a WHILE loop.
while True:
device_state = check_connection()
print(f'Last Device Connection State: {device_state}')
To add the locking ability we use the ‘ctypes’ library, and an IF statement to check when the ‘device_state’ is False.
if device_state == False:
ctypes.windll.user32.LockWorkStation()
That’s all it takes! The full code looks like this:
import subprocess
import ctypesmy_device = 'Toms-iPhone'
connected = Falsedef check_connection():
check = subprocess.run(f'ping {my_device}',
stdout=subprocess.PIPE,shell=True)
check = check.stdout.decode('utf-8')
state = check.split('\r')[2].split()
if state[-1]=='unreachble.':
connected = False
else:
connected = True
return connectedwhile True:
device_state = check_connection()
print(f'Last Device Connection State: {device_state}')
if device_state = False:
ctypes.windll.user32.LockWorkStation()
Super easy! Just running this little script in the background will help keep your pc that little bit more secure if your prone to forgetting to lock your pc when you leave the house!
I hope you enjoyed this quick python tutorial, and this is by no means the only thing you can do with this concept. Maybe you don’t care about locking your pc, so you could get your pc to do any number of things when you leave the house!