pacofvf
March 2nd, 2011, 12:09 AM
Hello, I made this script, It counts the ocurrences of the logged users, and gives you how much time a user has been logged in. The output of "last" command is somthing like this:
user1 :13 Tue Mar 1 08:17:08 2011 - down (06:21)
user1 pts/5 :20.0 Tue Mar 1 08:07:09 2011 - Tue Mar 1 08:50:39 2011 (00:43)
user2 :20 Tue Mar 1 08:04:50 2011 - Tue Mar 1 08:56:01 2011 (00:51)
user1 :20 Tue Mar 1 08:04:49 2011 - down (06:34)
and the output of my script is something like this:
Username Session Time(dd/hh/MM)
user1 0 : 15 : 8
user2 0 : 14 : 55
and here is my script:
#! /usr/bin/env python
import re
import subprocess
import operator
p = subprocess.Popen('last', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
entries = re.split('\n+', p.stdout.read())
entries = [re.split('\\(|\\)| ', entry) for entry in entries][0:-2]
li = []
for entry in entries: #we keep only the username and the session length
if len(entry) > 6:
if len(entry[-5]) > 0:
li.append([entry[0] , entry[-5]])
else:
li.append([entry[0] , entry[-6]])
d = dict() # starts an empty dictionary
for entry in li:#sum all ocurrencies
t = 0
tS = re.split('\+',entry[1])#
u = entry[0]
if len(tS) > 1:
t = t + int(tS[0]) * 60 * 24
tS2 = tS[1]
else:
tS2 = tS[0]
tS = re.split('\:',tS2)
if len(tS) > 1:
t = t + int(tS[1]) + int(tS[0])*60
if u in d:
d[u]= t+d[u]
else:
d[u] = t
sorted_x = sorted(d.iteritems(), key=operator.itemgetter(1), reverse=True)
print 'Username\t\tSession Time(dd/hh/MM)'
for i in sorted_x:
h=int(i[1]/60)
m=int(i[1]%60)
d=int(h/24)
h=int(h%24)
print i[0],'\t\t',d,':',h,':',m
I'm new to python, I think that this could be coded in a more simple and elegant way. I dare you
:O
user1 :13 Tue Mar 1 08:17:08 2011 - down (06:21)
user1 pts/5 :20.0 Tue Mar 1 08:07:09 2011 - Tue Mar 1 08:50:39 2011 (00:43)
user2 :20 Tue Mar 1 08:04:50 2011 - Tue Mar 1 08:56:01 2011 (00:51)
user1 :20 Tue Mar 1 08:04:49 2011 - down (06:34)
and the output of my script is something like this:
Username Session Time(dd/hh/MM)
user1 0 : 15 : 8
user2 0 : 14 : 55
and here is my script:
#! /usr/bin/env python
import re
import subprocess
import operator
p = subprocess.Popen('last', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
entries = re.split('\n+', p.stdout.read())
entries = [re.split('\\(|\\)| ', entry) for entry in entries][0:-2]
li = []
for entry in entries: #we keep only the username and the session length
if len(entry) > 6:
if len(entry[-5]) > 0:
li.append([entry[0] , entry[-5]])
else:
li.append([entry[0] , entry[-6]])
d = dict() # starts an empty dictionary
for entry in li:#sum all ocurrencies
t = 0
tS = re.split('\+',entry[1])#
u = entry[0]
if len(tS) > 1:
t = t + int(tS[0]) * 60 * 24
tS2 = tS[1]
else:
tS2 = tS[0]
tS = re.split('\:',tS2)
if len(tS) > 1:
t = t + int(tS[1]) + int(tS[0])*60
if u in d:
d[u]= t+d[u]
else:
d[u] = t
sorted_x = sorted(d.iteritems(), key=operator.itemgetter(1), reverse=True)
print 'Username\t\tSession Time(dd/hh/MM)'
for i in sorted_x:
h=int(i[1]/60)
m=int(i[1]%60)
d=int(h/24)
h=int(h%24)
print i[0],'\t\t',d,':',h,':',m
I'm new to python, I think that this could be coded in a more simple and elegant way. I dare you
:O