[python-win32] Track-session-events

Rodriguez Orjuela, Jose Luis jrodriguezorjuela at luc.edu
Wed Jun 8 11:51:12 EDT 2016


Hello,

I am writing a python script to track laptop usage. So far I am able to do simple stuffs username, machine, logon time, ip address, etc. Now I am trying to capture user logoff and sessions changes (screen lock). Related to user sessions I found a great blog ( http://timgolden.me.uk/python/win32_how_do_i/track-session-events.html#isenslogon<http://timgolden.me.uk/python/win32_how_do_i/track-session-events.html%23isenslogon> ) that explores some of this things but I haven't been able to identify the key parts that I can use in my case. I am looking to capture logoff datetime as well as screen lock and store it in a remote DB. Here is a fragment of the code in the blog that I am trying to use to accomplish this. It would be great if someone can guide me in the right direction. Thank you!

    def SvcOtherEx(self, control, event_type, data):
        if control == win32service.SERVICE_CONTROL_SESSIONCHANGE:
            sess_id = data[0]
            if event_type == 5: # logon
                msg = "Logon event: type=%s, sessionid=%s\n" % (event_type, sess_id)
                user_token = win32ts.WTSQueryUserToken(int(sess_id))
            elif event_type == 6: # logoff
                msg = "Logoff event: type=%s, sessionid=%s\n" % (event_type, sess_id)
            else:
                msg = "Other session event: type=%s, sessionid=%s\n" % (event_type, sess_id)
            try:
                for key, val in self.GetUserInfo(sess_id).items():
                    msg += '%s : %s\n'%(key, val)
            except Exception, e:
                msg += '%s'%e
            logevent(msg)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20160608/c3093d0a/attachment.html>


More information about the python-win32 mailing list