Skip to content

Instantly share code, notes, and snippets.

@zerok zerok/
Created Jan 15, 2018

What would you like to do?
YubiKey locking if unplugged
#!/usr/bin/env python3
import time
import subprocess
from datetime import datetime
from datetime import timedelta
def lock():
subprocess.check_call("""osascript -e 'tell application "System Events" to start current screen saver'""", shell=True, stderr=subprocess.STDOUT)
def check():
subprocess.check_call('ioreg -p IOUSB | grep Yubikey > /dev/null', shell=True)
return True
return False
def main():
locked_before = None
plugged_in_before = None
threshold = timedelta(seconds=60)
while True:
now =
plugged_in_now = check()
if plugged_in_before is None:
plugged_in_before = plugged_in_now
if plugged_in_before and not plugged_in_now:
if locked_before is None or now - locked_before > threshold:
locked_before = now
if now - locked_before >= threshold:
plugged_in_before = plugged_in_now
if __name__ == '__main__':
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN >
<plist version="1.0">
<true />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.