OSX: utility script (bash scripts) series-22-User Logon history

Source: Internet
Author: User

I have shared the code for a long time. I found one today. This is quite good. You can display the username and time of user login/logout in the log, this is an online copy of the time/who used the computer for help:


#!/usr/bin/python# extracts the login attemps and successes from the audit logs.# v.0.1# Noel B.A. contact@nbalonso.comfrom optparse import OptionParserimport osimport subprocessfrom xml.dom import minidom#import getopt, sys, ConfigParser, cStringIO, time, traceback, datetime# import subprocess# import plistlib# import datetime# import sys# import statclass BSMProcessor(object):  '''description'''  def __init__(self):    self.logPath = '/var/audit/'    self.auditreduceScript = '/usr/sbin/auditreduce'    self.prauditScript = '/usr/sbin/praudit'  def getLogs(self):    """ Read the logs using praudit """    #try:    for auditfile in os.listdir(self.logPath):      # the 'current' file is processed as .not_terminated      # filter it out to avoid duplicates      if auditfile != 'current':        # execute praudit and store the output in content        sp = subprocess.Popen([self.prauditScript, "-x", self.logPath + auditfile], stdout=subprocess.PIPE)        content, err = sp.communicate()        # parse the content with minidom and store it in xmldoc        xmldoc = minidom.parseString(content)        # loop within each <record> </record>        itemlist = xmldoc.getElementsByTagName('record')        for key in itemlist :            if key.attributes['event'].value == 'loginwindow login' or key.attributes['event'].value =='logout - local':              print key.attributes['event'].value,              print '\t\t',              #Print information within <subject></subject>              subject=key.getElementsByTagName('subject')              for skey in subject:                print skey.attributes['audit-uid'].value,                print '\t\t',                print key.attributes['time'].value              #Print information within <text></text>              # text=key.getElementsByTagName('text')              # for tkey in subject:              #   print tkey.attributes['text'].Nodevalue    #except OSError:    #  print 'Access denied. This program needs sudo access'def main():    '''Main'''    bsmProc = BSMProcessor()    parser = OptionParser()    parser.add_option("-f", "--file", dest="filename",                      help="Write the result to FILE.", metavar="FILE")    # parser.add_option("-x",    #                   help="Print the records in xml format.")    options, args = parser.parse_args()    # print 'Arguments:', args    # print 'Options:', options    # print '=============='    # Do the work....    bsmProc.getLogs()if __name__ == '__main__':    main()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.