Skip to content

Instantly share code, notes, and snippets.

@z4yx
Created April 30, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z4yx/ba7b6671084e8744ea34b6181c9872ab to your computer and use it in GitHub Desktop.
Save z4yx/ba7b6671084e8744ea34b6181c9872ab to your computer and use it in GitHub Desktop.
Yubikey 没拔! From: https://cfp.vim-cn.com/cbf05?py
import sys
import subprocess
import notify2
import dbus
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop
import yubico
def check(bus, msg):
if msg.get_path() != "/ScreenSaver":
return
args = msg.get_args_list()
if args[0]:
print("Screen locked", file=sys.stderr)
else:
print("Screen unlocked", file=sys.stderr)
return
try:
_ = yubico.find_yubikey(debug=False)
except yubico.yubico_exception.YubicoError:
return
else:
print("Found yubikey", file=sys.stderr)
n = notify2.Notification("Yubikey", "Yubikey 没拔!", "dialog-warning")
n.show()
subprocess.run(["loginctl", "unlock-session"])
if __name__ == '__main__':
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string(
"type='signal'," +
"path='/ScreenSaver'," +
"interface='org.freedesktop.ScreenSaver'," +
"member='ActiveChanged'")
bus.add_message_filter(check)
notify2.init(sys.argv[0])
mainloop = GLib.MainLoop()
mainloop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment