script to write the last login date and time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers script to write the last login date and time
# 1  
Old 12-17-2008
script to write the last login date and time

I want to make a script to write me in a file the date(format 16-12-2008) and hour(format 15:12:21) of the last login in the system; what can i add to the <last> command in order to be suitable?
# 2  
Old 12-17-2008
Here is a perl script that uses the lsuser command to generate the login id and date. It then writes out the login id and date (mmddyyyy) and time (hour:minute:seconds). the lsuser command as constructed list all users so if you want to do it for a particular user then you will have to modify the lsuser command to list only one. Also this runs on AIX 5.2. Good Luck.
Code:
#!/usr/bin/perl                                         
# generate user list with date last logged.
system("lsuser -a time_last_login ALL | sed 's/\ time_last_login=/:/g' > /lsf1/hurdata/userlogins");
# Open input file generated from above line
open (lastlog,"</lsf1/hurdata/userlogins") or die ("Cannot Open lastlog file");
# Open output file that will contain the report of the user and last date and time logged in
open (lastuser,">/lsf1/hurdata/login_date_time.txt") or die ("Cannot open lastlogin.txt file");
while (<lastlog>) {
# Split the two values on one line to separate variables
  ($logid,$lastdate) = split ':', $_;
# If is not empty then convert to readable date and print login, date and time
  if ($lastdate!=" ") {
     ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($lastdate);
     $year=$year+1900;
     $mon=$mon+1;
     if ($mon<10) {$mon="0".$mon;}
     if ($mday<10) {$mday="0".$mday;}
     if ($yday<100) {$yday="0".$yday;}
     if ($sec<10) {$sec="0".$sec;}
     if ($min<10) {$min="0".$min;}
     if ($hour<10) {$hour="0".$hour;} 
     $logdate=$mon.$mday.$year;
     print lastuser "$logid $logdate $hour:$min:$sec\n";
    }
# If date is empty then print login and message
  else {
        chop $logid;  # get rid of line feed
        print lastuser "$logid Never logged in\n";
       }
}
close (lastlog) ;
close (lastuser) ;

# 3  
Old 12-17-2008
thank u,i forgot to mention that the script i want is in bash:P
i will try to 'translate' it
# 4  
Old 12-17-2008
Don't you have Perl installed?
# 5  
Old 12-18-2008
No. And i have to do it in bash
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

• Write a shell script that upon invocation shows the time and date and lists all the logged-in user

help me (1 Reply)
Discussion started by: sonu pandey
1 Replies

2. UNIX for Dummies Questions & Answers

How to write a shell script to Run it the from Date A to Date B?

Hi , How would i write a shell script to run the code from one date to another date EXample 2014-01-01 to 2014-02-28, can i any provide some clue on this (4 Replies)
Discussion started by: vikatakavi
4 Replies

3. Shell Programming and Scripting

write script for previous date

Hi all, I need to write a unix shell script that will return the previous date of date entered and i have to consider leap year also. For eg:- if i entered 2009-08-18 then the script should return 2009-08-17 can you please help me to write this... (12 Replies)
Discussion started by: prasson_ibm
12 Replies

4. UNIX for Dummies Questions & Answers

How to do a login time and date list of users never logged on?

Hello, I'm trying to do a list of user that never connected to a couple of servers. I want to do a diff between the servers lists, and print out only the users that never has logged on each server. Here my first step : SERVER01: # finger `egrep -v -e "^\s*#" /etc/passwd | awk '{ print $1 }' |... (4 Replies)
Discussion started by: gogol_bordello
4 Replies

5. Shell Programming and Scripting

how to write a shell script to login to a system which is interactive.

Can anybody help me to write a shell script to login interactive system once u open a connection using telnet it will ask for USERCODE: PASSWORD: DOMAIN: (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

6. UNIX for Dummies Questions & Answers

Script to Extract time from log files and write to a excel

Can someone help me with writing a unix script for following requirement 1) I have a log file in which we have start time and end time (format: hh:mm:ss) Example: starting script on Thu Jun 5 20:50:52 --------- Thu Jun 5 21:55:33 - Script Completed 2) I want to extract... (4 Replies)
Discussion started by: santosham
4 Replies

7. Shell Programming and Scripting

Script to Extract time from log files and write to a excel

Can someone help me with writing a unix script for following requirement 1) I have a log file in which we have start time and end time (format: hh:mm:ss) Example: starting script on Thu Jun 5 20:50:52 Thu Jun 5 21:55:33 - Script Completed 2) I want to extract start time and end time of... (0 Replies)
Discussion started by: santosham
0 Replies

8. UNIX for Advanced & Expert Users

Script to Extract time from log files and write to a excel

Can someone help me with writing a unix script for following requirement 1) I have a log file in which we have start time and end time (format: hh:mm:ss) Example: starting script on Thu Jun 5 20:50:52 Thu Jun 5 21:55:33 - Script Completed 2) I want to extract start time and end time of... (0 Replies)
Discussion started by: santosham
0 Replies

9. UNIX for Dummies Questions & Answers

last login date and time for all users

I need a command that will list all the users and their last login date & time. I was trying the last command and the who command, but can't get exactly what I need. I just need the output to be user name and last login date . Thanks for your help! (3 Replies)
Discussion started by: igidttam
3 Replies

10. UNIX for Dummies Questions & Answers

Script to display last login date/time?

I was wondering if anyone had a script that would display the last time a user logged into a particular machine. I know about the "last" command, but it gives too much info.... I just wanted to know the last time a user used his/her id. ANy help would be greatly appreciated. Ryan (3 Replies)
Discussion started by: ryaneverett5
3 Replies
Login or Register to Ask a Question