Use ykman/scripting.py utilities
mainThe ykman.scripting module (commonly imported as s) provides helper functions for interacting with YubiKeys during script execution.
s.single()
Connects to a single YubiKey. Useful for scripts targeting one specific device.
s.multi(allow_initial=True)
An iterator that watches for connected YubiKeys. It allows you to perform actions on multiple devices or wait for a specific device to be plugged in.
allow_initial: If set toTrue, the function will include YubiKeys that are already connected when the call is made. IfFalse(the default), the call will fail if YubiKeys are already connected, which acts as a safety measure to prevent accidental programming of the wrong device.
from ykman import scripting as s
# Connect to the first available YubiKey
device = s.single()
print("Found a YubiKey:", device)
# Iterate through YubiKeys as they are connected
for device in s.multi(allow_initial=True):
print("New device detected:", device)