Archiving Users


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Archiving Users
# 1  
Old 04-07-2006
Archiving Users

Hello Gurus,
I have a requirement where I have to remove users that have not logged into our unix box last 90 days. I want to script this one and remove the users automatically now and then. Has any one ever done this before, if so please give me some sample code. I am not a good unix programmer and I would appreciate any help on this issue.

Thanks,
Sathish Kumar
suseesk
# 2  
Old 04-08-2006
Hi Sathish I'm listing below a script that should peform what u have desired.

Code:
#! /bin/bash
#lastlog|awk -F " " '{print $1" "$3" "$4" " $5" " $6" " $7" " $8}'|grep [1-31] > lastlogfile
lastlog|tr -s " " |cut -f 1,3-8 -d" "|grep [1-31] >lastlogfile
terminal=`tty`
backday=`date --date='90 days ago'`
backdaysec=`date --date="$backday" +%s`

exec<lastlogfile

while read line
do
#	lastdate=`echo $line| awk -F " " '{print $2" "$3" "$4" "$5" "$6" "$7}'`
	lastdate=`echo $line| cut -f 3-8 -d" "`
	lastdatesec=`date --date="$lastdate" +%s`
	if [ $lastdatesec -lt $backdaysec ]
	then
#		echo $line | awk -F " " '{print $1}'
		user=`echo $line | cut -f 1 -d" " | tr -d "\n"`
                         userdel -r $user
	fi
done
exec<$terminal
#rm -fr lastlogfile

This code whenever run will delete the users who haven't logged in for more than 90 days. Now u can either submit this script to be run as a cron job at the end of the day or put put the whole script in a infinte loop
(like
while 1
do
#put above script here
done
)
and run the above script in background (like nohup ./rmuser &). It is advisable to use crontab for this would not put unnecessary pressure on ur system. There is much of documentation on crontab (use info & man) in addition to these u can look at http://www.deluxnetwork.com/linux/guides/crons.php for help.

In the above script I've deliberately left some part commented. While codes involving awk present an alternative to code involving cut following it. The last command rm -fr is commented so that the file (lastlogfile) need not be created everytime but may be uncommented if that needs to be removed for some reason.
The code is quite easy to comprehend so I'm not giving much of description but in short it obtaines the last login time and corresp. login name of all users using command 'lastlog' which uses file /var/log/lastlog (if it doesn't exist create one) converts the last login time to seconds since epoch (which is 1970-01-01 00:00:00 UTC). In the similar fashion it converts the date 90 days ago (since the time the code is being executed) to seconds since epoch. Now it compares the two times and removes the user if his last login is older than 90 days.

Two caveats :
1) If a user is logged in continuously for 90 days (i.e. he logged in 90 days back and is still logged in) the code might attempt to delete his account (though it might not succeed for userdel might not allow it to do so).

2) I have used userdel -r. The option 'r' should not be used if u want his files to be left (home directory and files therein).

Hope this helps u

Regards
Rakesh Ranjan
# 3  
Old 04-10-2006
Thanks....

Rakaesh,
Thanks for your help. I really appreciate it. I will update the status soon.

Regards,
Sathish Kumar
suseesk
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Logs archiving

Hi , Might be the very basic question and most frequent one also.. wanted to archive the logs/files older than one month or older than 30 days to some particular location. File/log format is like below ABCD_EF_GHIJ_Defaulter_Report_(06-Jun-2014_11-50-20_AM) Source : /test/ABC... (3 Replies)
Discussion started by: Riverstone
3 Replies

2. Shell Programming and Scripting

Archiving the files

hi, Am trying to acrhive a bunch of files on some ftp site and somehow managed to come out with the below logic. I'm getting "syntax error: unexpected end of file" error. Interestingly this below snipeet works fine if run for the first time but the subsequent runs fail! Anybody has any idea... (3 Replies)
Discussion started by: Amee5
3 Replies

3. Shell Programming and Scripting

Help for backup and archiving

Hi, I am trying to develop a KSH script, to perform the following functions: 1. ZIP the previous day backup to ZIP Directory, while removing any ZIP file older than a week. 2. Perform the backup. 3. Send a confirmation mail with subject content of the size of file. Please let me... (0 Replies)
Discussion started by: johnprince1980
0 Replies

4. UNIX for Advanced & Expert Users

Help with archiving using find

Dear Unix Experts, I am trying to backup some legacy data based on date. I am pasting a small portion from the ls -al command from my session for reference. athena>ls -al ./nmr/exam1d_13C: total 32 drwxrwxrwx 3 root root 4096 May 1 2008 1/ drwxrwxrwx 3 root ... (1 Reply)
Discussion started by: jaison75
1 Replies

5. Shell Programming and Scripting

Archiving the files

Hi, Suppose I have 2 files of yesterday's. And today I have received 3 files. Before processing anything I want to archieve the 2 files of yesterday's into a different folder. How can this be done? Regards, Sunitha (1 Reply)
Discussion started by: Sunitha_edi82
1 Replies

6. Shell Programming and Scripting

Archiving by Time

Hi all. I am trying to set up archiving of directories, such that I keep every directory made in the past week, but just one directory per week beyond that. Using the find command, I can easily delete everything more than one week old, but can not figure out how to save one. Each directory... (4 Replies)
Discussion started by: aefskysa
4 Replies

7. UNIX for Dummies Questions & Answers

Archiving Problem

Hi friends I work on UNIX Operating system and I have many servers. ADM server make archiving for special data every day inside XYZ directory at specific time,the next time SPERADM server will take that archiving data to put them in same directory (inside SPERADM server). Now archiving... (2 Replies)
Discussion started by: bintaleb
2 Replies

8. Shell Programming and Scripting

Archiving

Hi, I want to archive below directory ex: /home/oracle/dd0 sub-directories in dd0 /home/oracle/dd0/backup/backup1 /home/oracle/dd0/backup/backup2 /home/oracle/dd0/dmp ....etc I want a command(tar) which will let me archive the above directory excluding *.dmp(dump files), *.log(log... (1 Reply)
Discussion started by: dreams5617
1 Replies

9. UNIX for Dummies Questions & Answers

Archiving

I am looking for some advice. I have some files on a Unix server, which contain symbolic links. I need to copy these over to a nfts environment, but wish to keep the symbolic links intact. Any ideas? (7 Replies)
Discussion started by: rmasonuk
7 Replies
Login or Register to Ask a Question