Advertisement
BrandonSpendlove

TACACS+ - Get total sessions and time logged in (AAA - User)

Oct 9th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import time
  4. import sys
  5.  
  6. def getUserTime(username):
  7.     #Default location for accounting file below. It won't be created if AAA accounting is not
  8.     #configured on the device...
  9.     accFile = open('/var/log/tac_plus.acct','r')
  10.     splitFile = accFile.readlines()
  11.  
  12.     #2 lines should involve a session (Connect + Disconnect)
  13.     sessions = len(splitFile) / 2
  14.  
  15.     print 'Total Sessions in accounting file: {0}\n'.format(str(sessions))
  16.  
  17.     #Variables for splitting the file + counters
  18.     session_index = 0
  19.     session_list = []
  20.  
  21.     count = 0
  22.     user_sessions = 0
  23.     user_time_index = 0
  24.     user_time = 0
  25.     #--------------------------------------#   
  26.  
  27.     while session_index < sessions:
  28.         #Check if username exist in accounting file
  29.         if any(username in s for s in splitFile[count:count+2]):
  30.             #Increment user sessions, since more than 1 connect/disconnect have been found
  31.             user_sessions = user_sessions + 1
  32.             #Print below is for debugging/seeing output when running the script
  33.             print 'Found session for user: {0} ({1})...'.format(username, user_sessions)
  34.             #Append connect/disconnect session from splitFile list to a new list of lists...
  35.             if any("elapsed_time=" in t for t in splitFile[count:count+2]):
  36.                 session_list.append(splitFile[count:count+2])
  37.         #Counter is incremented by 2 because of session is made from: connect and disconnect
  38.         count = count + 2
  39.         #Session_index tells us what session we are on in the file...Stops the while loop
  40.         session_index = session_index + 1
  41.  
  42.     print '\nTotal sessions for user **{0}** = {1}'.format(username, user_sessions)
  43.  
  44.     #Reset incase I use count again...
  45.     count = 0
  46.  
  47.     #For each 'session' (which we have filtered 'elapsed_time=' to a username
  48.     for session in session_list:
  49.         user_time_index = session[1].split() #Split all variables in disconnect session
  50.         #Index 14 SHOULD be 'elapsed_time=', replace it so now we have the variable in 'user_time'
  51.         user_time = user_time + int(user_time_index[14].replace('elapsed_time=',''))
  52.  
  53.     #using 'time' module, to output the format
  54.     user_time = time.strftime('%H:%M:%S', time.gmtime(user_time))
  55.     print 'Total time logged in: ' + str(user_time)
  56.  
  57.     #Write to temp_time so a PHP script in /var/html/www can pull the total time for the user...
  58.     new_file = open('/var/www/html/temp_time.txt','w')
  59.     new_file.write(user_time)
  60.     new_file.close()
  61.  
  62. getUserTime(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement