OSX: 實用指令碼程式(bash scripts)系列-22- 使用者登陸曆史

來源:互聯網
上載者:User

 好久木有分享代碼了,今天找到一個,這個挺好,可以從log裡面把使用者登陸/登出的使用者名稱和時間顯示出來,對於想看看電腦何時/誰使用了多長時間電腦有所協助,這個是網上抄過來的:


#!/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()


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.