Skip to content

Instantly share code, notes, and snippets.

@baughj
Created March 18, 2021 17:31
Show Gist options
  • Save baughj/21e14df5fa7c9bd9545790b8f86059eb to your computer and use it in GitHub Desktop.
Save baughj/21e14df5fa7c9bd9545790b8f86059eb to your computer and use it in GitHub Desktop.
Script to lock OSX on yubikey removal & -HUP yubikey agent
#!/bin/bash -u
prev_state="unknown"
prev_lock_state="unknown"
state="unknown"
lock_state="unknown"
function lock {
killall -HUP yubikey-agent
open -a ScreenSaverEngine.app
}
while true; do
prev_state=$state
prev_lock_state=$lock_state
if $(system_profiler SPUSBDataType 2>/dev/null | grep -q YubiKey); then
state="attached"
else
state="detached"
fi
if (ps ax | grep -q [S]creenSaverEngine); then
lock_state="locked"
else
lock_state="unlocked"
fi
echo $prev_state, $state, $prev_lock_state, $lock_state
if [ "$prev_state" = "attached" -a "$state" = "detached" ]; then
echo locking
lock
fi
if [ "$prev_lock_state" = "unlocked" -a "$lock_state" = "locked" ]; then
echo "flushing"
killall -HUP yubikey-agent
fi
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment