Created
February 17, 2015 10:07
-
-
Save arsane/691b5e28e573bee6d898 to your computer and use it in GitHub Desktop.
Detect dynamic smart card insertion and removal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from smartcard.System import readers | |
from smartcard.CardType import AnyCardType | |
from smartcard.CardRequest import CardRequest | |
from smartcard.CardConnection import CardConnection | |
from smartcard.util import toHexString, toBytes | |
from smartcard.CardMonitoring import CardMonitor, CardObserver | |
from smartcard.util import * | |
from time import sleep | |
def check_reader(): | |
cardtype = AnyCardType() | |
cardrequest = CardRequest( timeout=1, cardType=cardtype) | |
try: | |
cardservice = cardrequest.waitforcard() | |
except: | |
print "No Card in any reader." | |
return | |
cardservice.connection.connect() | |
print "%s: %s" % (cardservice.connection.getReader(), toHexString( cardservice.connection.getATR())) | |
#apdu = "a0a4000002" | |
#response, sw1, sw2 = cardservice.connection.transmit( toBytes(apdu), CardConnection.T0_protocol ) | |
#print 'response: ', response, ' status words: ', "%x %x" % (sw1, sw2) | |
# a simple card observer that prints inserted/removed cards | |
class printobserver( CardObserver ): | |
"""A simple card observer that is notified | |
when cards are inserted/removed from the system and | |
prints the list of cards | |
""" | |
def update( self, observable, (addedcards, removedcards) ): | |
for card in addedcards: | |
print "+Inserted: ", toHexString( card.atr ) | |
for card in removedcards: | |
print "-Removed : ", toHexString( card.atr ) | |
def monitor_reader(): | |
try: | |
print "Insert or remove a smartcard in the system." | |
print "This program will exit in 10 seconds" | |
print "" | |
cardmonitor = CardMonitor() | |
cardobserver = printobserver() | |
cardmonitor.addObserver( cardobserver ) | |
except: | |
return | |
if __name__ == "__main__": | |
check_reader() | |
monitor_reader() | |
while 1: | |
sleep(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment