nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

script to record logout time

This is a discussion on script to record logout time within the Shell scripting forums, part of the Development/Scripting category; Hi Can anyone help that how can i record a user's logout date/time/ip? Regards Asim...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-06-2009, 02:19 AM
Senior Member
User
 
Join Date: Jul 2006
Posts: 146
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 4
asim.mcp is on a distinguished road
Smile script to record logout time

Hi

Can anyone help that how can i record a user's logout date/time/ip?




Regards
Asim
Reply With Quote
  #2 (permalink)  
Old 07-06-2009, 03:00 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 246 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

No need to write a script, just use last command.


Linux / UNIX: Getting information about reboots and shutdowns
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 07-06-2009, 03:23 AM
Senior Member
User
 
Join Date: Jul 2006
Posts: 146
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 4
asim.mcp is on a distinguished road
Default

Yes , you are right.

But want to generate an email with information which has been written in the last command.

Please see the script below this emails be when user login in.


Code:
#!/bin/bash
l=`last -n 5 | awk "NR==1" | awk -F " " '{print $1, $3, $4, $5, $6, $7}'`
ip=`last -n 5 | awk "NR==1" | awk -F " " '{print $3}'`
h=`echo $HOSTNAME`

/usr/local/bin/sendEmail -t example@example.com -f example@example.com -s smtp.example.com -m $l -u Login to $h by $ip

Last edited by nixcraft; 07-06-2009 at 04:58 PM.
Reply With Quote
  #4 (permalink)  
Old 08-06-2009, 10:52 AM
Senior Member
User
 
Join Date: Jul 2006
Posts: 146
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 4
asim.mcp is on a distinguished road
Default

Any help please...
Reply With Quote
  #5 (permalink)  
Old 08-06-2009, 06:23 PM
lim lim is offline
Junior Member
User
 
Join Date: Mar 2009
Location: Clifton, VA
OS: RHEL 5.3
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lim is on a distinguished road
Send a message via AIM to lim
Default

put that script on a crontab that runs every few hours?
Reply With Quote
  #6 (permalink)  
Old 08-06-2009, 11:26 PM
Senior Member
User
 
Join Date: Jul 2006
Posts: 146
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 4
asim.mcp is on a distinguished road
Smile

Dear lim,

Code:
#!/bin/bash
l=`last -n 5 | awk "NR==1" | awk -F " " '{print $1, $3, $4, $5, $6, $7}'`
ip=`last -n 5 | awk "NR==1" | awk -F " " '{print $3}'`
h=`echo $HOSTNAME`

/usr/local/bin/sendEmail -t example@example.com -f example@example.com -s smtp.example.com -m $l -u Login to $h by $ip
The above script records login sessions from "last" command.
When a user logged in session will be recorded like this
Code:
root     pts/1        xxx.xxx.xxx.xxx    Tue Jun  9 02:00   still logged in
I'm using awk "NR==1" to print only 1 line and awk -F" " '{print $1, $3, $4, $5, $6, $7}' to print the selected fields.
When a user logged out the last shows output like this:

Code:
root     pts/2        xxx.xxx.xxx.xxx    Tue May 19 04:36 - 05:29  (00:52)
If I use the field 8, 9 and 10 then i can get the logout time too. But with the above command i cann't do this because any one else can log in so

awk "NR==1" | awk -F " " '{print $1, $3, $4, $5, $6, $7, $8, $9, $10}' is also fail, this will only print the very first line.
So i want a command/script which search in last command and print the output of certain session.

Any help please
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell Script To Log Uptime, Connect Time, Bytes Sent Received Of a PPP Connection dane Shell scripting 5 25-05-2009 03:28 PM
Script To Kill process if time and memories of more than 30% lliriK Shell scripting 9 28-04-2009 12:43 AM
Thin client logout time jhalbert CentOS / RHEL / Fedora 2 16-03-2009 10:32 PM
Dns MX Record Rahul.Kolan Domain Name Server 2 08-10-2008 12:27 PM
Shell Script Searching For a Record In The File vinz4ever Shell scripting 1 11-05-2008 07:35 PM


All times are GMT +5.5. The time now is 07:40 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38