Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import time
- import sys
- def getUserTime(username):
- #Default location for accounting file below. It won't be created if AAA accounting is not
- #configured on the device...
- accFile = open('/var/log/tac_plus.acct','r')
- splitFile = accFile.readlines()
- #2 lines should involve a session (Connect + Disconnect)
- sessions = len(splitFile) / 2
- print 'Total Sessions in accounting file: {0}\n'.format(str(sessions))
- #Variables for splitting the file + counters
- session_index = 0
- session_list = []
- count = 0
- user_sessions = 0
- user_time_index = 0
- user_time = 0
- #--------------------------------------#
- while session_index < sessions:
- #Check if username exist in accounting file
- if any(username in s for s in splitFile[count:count+2]):
- #Increment user sessions, since more than 1 connect/disconnect have been found
- user_sessions = user_sessions + 1
- #Print below is for debugging/seeing output when running the script
- print 'Found session for user: {0} ({1})...'.format(username, user_sessions)
- #Append connect/disconnect session from splitFile list to a new list of lists...
- if any("elapsed_time=" in t for t in splitFile[count:count+2]):
- session_list.append(splitFile[count:count+2])
- #Counter is incremented by 2 because of session is made from: connect and disconnect
- count = count + 2
- #Session_index tells us what session we are on in the file...Stops the while loop
- session_index = session_index + 1
- print '\nTotal sessions for user **{0}** = {1}'.format(username, user_sessions)
- #Reset incase I use count again...
- count = 0
- #For each 'session' (which we have filtered 'elapsed_time=' to a username
- for session in session_list:
- user_time_index = session[1].split() #Split all variables in disconnect session
- #Index 14 SHOULD be 'elapsed_time=', replace it so now we have the variable in 'user_time'
- user_time = user_time + int(user_time_index[14].replace('elapsed_time=',''))
- #using 'time' module, to output the format
- user_time = time.strftime('%H:%M:%S', time.gmtime(user_time))
- print 'Total time logged in: ' + str(user_time)
- #Write to temp_time so a PHP script in /var/html/www can pull the total time for the user...
- new_file = open('/var/www/html/temp_time.txt','w')
- new_file.write(user_time)
- new_file.close()
- getUserTime(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement