This script (or exe if using web2py with """setup(windows=['locker2.py']))""") can be run as a task in windows every x minutes and will test for the presence of an internet connection and depending on whether it is found will set windows to lock after a given timeout without user activity.
This was made with help from random code snippets from around the web.
Tested only on windows 7.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | # Author: c8
# Date created: 9/7/12
# Purpose: Set windows to lock itself(upon timeout) with a screensaver
# if no internet connection found.
from _winreg import *
import urllib2, socket
debug = False
########################## TO RUN ####################################
# schedule to run every x mins
########################### DEF ######################################
def locker(set):
# make set in terms of 1/0
set = 1 if set else 0
subkey = r'Control Panel\Desktop'
# to ensure screensaver is set to 'none' (straight to lock screen)
deleteRegistryKey(HKEY_CURRENT_USER, subkey, r'SCRNSAVE.EXE')
data= [('ScreenSaverIsSecure', REG_SZ, str(set)),
('ScreenSaveTimeOut', REG_SZ, '420')]
for valueName, valueType, value in data:
modifyRegistry(HKEY_CURRENT_USER, subkey, valueName,
valueType, value)
if debug: message = 'changed to locked' if set else 'changed to unlocked'
if debug: print message
def modifyRegistry(key, sub_key, valueName, valueType, value):
"""
A simple function used to change values in
the Windows Registry.
"""
try:
key_handle = OpenKey(key, sub_key, 0, KEY_ALL_ACCESS)
except WindowsError:
key_handle = CreateKey(key, sub_key)
SetValueEx(key_handle, valueName, 0, valueType, value)
CloseKey(key_handle)
def deleteRegistryKey(key, sub_key, name):
"""
A simple function used to delete values in
the Windows Registry if present. Silently ignores failure
if value doesn't exist.
"""
try:
key_handle = OpenKey(key, sub_key, 0, KEY_ALL_ACCESS)
except WindowsError:
if debug: print 'No such key'
return
try:
DeleteValue(key_handle, name)
except WindowsError:
if debug: print "Value doesn't exist"
return
CloseKey(key_handle)
def internet_on():
# list of sites not likely to go down soon: google.com, microsoft.com etc
sites = ['173.194.79.94', '74.125.113.99', '64.4.11.20',
'173.194.33.21', '96.16.97.11']
for i in sites:
try:
site = 'http://%s' % (i)
response=urllib2.urlopen(site,timeout=1)
return True
# if urllib error, cant find site etc
except urllib2.URLError as err:
continue
# if timeout - occurs on some connections occasionally
except socket.timeout:
continue
return False
################################## CODE ######################################
# if connected to internet
if internet_on():
# set windows to unlocked
locker(False)
else:
# set windows to locked
locker(True)
|
I wrote this script as i frequently carry my laptop around with me, and against the eventuality of it being lost or stolen i recently encrypted my hard disk with Truecrypt. That would protect the computer info if it was turned off or restarted, but due to the inconvenience of having to enter my password every few minutes when i open my computer up (its an ultrabook with fast return from sleep times, so it gets closed more than most), i do not require a password then. This would allow someone finding it to access my info (until they restarted anyway).
So i figured i should secure it a bit further, going on the premise that when connected to wireless i am generally at a friends/home, while when not im out and about. Consequently if i set the computer to lock then after a a few minutes inactivity i get the best middle ground while ensuring that someone finding the computer can still see my password hint/info but would need to restart with a boot disc to try and circumvent my windows password - thus encountering truecrypt.
This all relies on the finder being unaware of the script - not too big an ask.
Helpful references: http://www.blog.pythonlibrary.org/2010/02/09/enabling-screen-locking-with-python/ http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/27/how-can-i-change-the-screensaver-timeout-value.aspx