Scripting User Account Removal


 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Scripting User Account Removal
# 15  
Old 03-01-2012
Ah! Sorry. The quoting thing gets me periodically. When to quote/not to quote, which quote method... You can probably remove the quotes from within that particular if statement and it would probably still work. :P

A time stamp operation would necessarily require a rework of the logic behind the logout script. Instead of using the current user ($USER) you would have to loop through every user profile EXCEPT the current user.

It can be done, but...

Here is an example of a loop for that sort of thing:

Code:
for i in `ls /Users`
do
	if [ $i = ".localized" ] || [ $i = "Shared" ] || [ $i = $USER ]; then
		# go to next folder in the loop
		continue
	else
		# do something with the folder, for instance:
		# rm -R /Users/"$i"
		echo "$i removed"
	fi
done

You would insert your timestamp check and deletion routine after the "else", presumably by nesting an if statement there. Failing the timestamp check would require continuing the loop, a la "continue" as seen above.

The time stamp check value would be the current day and time minus 1 day.
This should help:
https://www.unix.com/answers-frequent...rithmetic.html

Where you get the time stamp from on the user's home folder? My own home folder does show my login time, as do a couple of preference files in my home folder's Library/Preferences. You will need to kajigger the date info to work correctly in a comparison. You'll also want to verify the specific time stamp file as the valid choice across a few different logins, both admin and non admin, just to be sure.

Let us know if you achieve nirvana. Smilie
(I vaguely recall seeing something along these lines in some long ago Enterprise or Edu list...)

---------- Post updated at 04:59 PM ---------- Previous update was at 01:56 PM ----------

Or!

find /Users -maxdepth 1 \! -mtime -1d

Will return all folders that have not been modified in the last 24 hours (again, testing is important.)

Code:
find /Users \( \! -name Users -and \! -name .localized -and \! -name Shared -and \! -name $USER \) -maxdepth 1 \! -mtime -1d

Apparently (within the limits of maximum command line characters per line) you can keep adding "-and \! -name username" entries till you're satisfied.

So:
Code:
find /Users \( \! -name Users -and \! -name .localized -and \! -name Shared -and \! -name $USER \) -maxdepth 1 \! -mtime -1d -exec rm -R {} \;

Note the first exclusion "Users". Very important!
The find command output without exclusions looks like this:
$ find /Users -maxdepth 1 \! -mtime -1d
/Users
/Users/.localized
/Users/Shared

You probably do not want to recursively delete the /Users directory. Smilie

---------- Post updated at 05:13 PM ---------- Previous update was at 04:59 PM ----------

P.S.
I found this nugget at
CLI Fun: Delete files older than x days

Last edited by [MA]Flying_Meat; 03-01-2012 at 06:02 PM..
This User Gave Thanks to [MA]Flying_Meat For This Post:
# 16  
Old 03-02-2012
Kurt made me chuckle

Hehe, the nirvana comment had me loling.

Ok, so the string:

Code:
find /Users \( \! -name Users -and \! -name .localized -and \! -name Shared -and \! -name $USER \) -maxdepth 1 \! -mtime -1d -exec rm -R {} \;

I'm trying to understand, actually if you can recommend a great book that describes all this stuff I'd love to know of it. Like I said, I can usually pick apart and fix stuff that already exists, but making my own is beyond me.

So if I wanted to implement that line to find and remove anything after 1 day would it look like this?

Code:
#!/bin/bash
# Logout script to remove nonadmin accounts from Users folder
# If user is an admin, exit script 
if [ $USER = "JoeUser" ]; then
echo "LOGOUT: admin folders will not be deleted."
exit 1
fi 

# If home directory exists, delete 
find /Users \( \! -name JoeUser -and \! -name .localized -and \! -name Shared -and \! -name $USER \) -maxdepth 1 \! -mtime -1d -exec rm -R {} \;
dscl . -delete /Users/"$USER"
fi 
exit 0

So then that would make it skip the user "JoeUser", and the "Shared" folder?
# 17  
Old 03-02-2012
Your find line:
Don't forget \! -name Users

Otherwise, yes It would not delete JoeUser, .localized, Shared, nor $USER.

You definitely want to exclude Users, as that would get deleted if it's time stamp is older than a day. Bad, since every home folder you wanted to keep would get deleted too.

You could remove the "exit 1" line from the script so that you still get notification that your admin user account/s will not be deleted. Your first if statement would then just for the notification, proceeding on to the find and delete operation. It should look like this:
Code:
# If home directory exists, delete 
find /Users \( \! -name Users -and \! -name JoeUser -and \! -name .localized -and \! -name Shared -and \! -name $USER \) -maxdepth 1 \! -mtime -1d -exec rm -R {} \;
dscl . -delete /Users/"$USER"
exit 0

---------- Post updated at 05:55 PM ---------- Previous update was at 05:32 PM ----------

As far as creating your own goes:
Advanced Bash-Scripting Guide

Verrrry handy.

Other than that, there's a "Learn Shell Scripting in 24 Hours" which even if it takes 24 days still isn't bad. Also web search, maybe even a quick and dirty Unix class at a local Community College?

It should be said that there is usually more than one way to accomplish the same task. Some might make use almost exclusively of awk, while others might never get much outside of python. Still others might scoff at not using ruby.

I've found that these Unix forums provide real examples of tasks one might need to perform, particularly Shell Programming and Scripting, Unix for Dummies... and Unix for Advanced. I have spent a few hours since finding these forums trying code out to see what it does. If the commands are not exclusive to a particular version of Unix, then it will be informative.
I find shell scripting (bash and/or sh) to be adequate, if admittedly sometimes clunky, for my needs. Learning something else is almost always limited to finding I cannot complete the task in the shell by itself. My most recent tenuous forays into the unknown have been simple awk line parsing with it's marvelous print function.
Pretty darned rare, but then I'm a simple kinda guy.
This User Gave Thanks to [MA]Flying_Meat For This Post:
# 18  
Old 03-12-2012
Computer Nirvana?

That find on the "remove after x days" was a sweet score! Smilie

I actually set it to 3 days to give the inevitable student that forgot to save to a flash drive the opportunity to save face. Though more often than not it's the Grad students that haven't learned this lesson yet and they lose months of work.

That did it. I've got it running on all our units without flaw. So now what?

Thanks so much for all the help [MA]Flying_Meat. And for the tips on what to read. I actually started teaching myself some Objective-C and even the little I've picked up so far in that helped me understand more some of the code you'd written.

I think the problem is finding and using on a regular basis practical uses for creating scripts. I'm not in an environment that requires much of it and being the only one doing it it makes it more of a challenge.

Any idea how scripting for Windows differs?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

User account

I need to check actual date a user was disabled on my HP-UX server. Audit is claiming the user account was active during the last audit exercise. (7 Replies)
Discussion started by: cyriac_N
7 Replies

2. Linux

User Account Policy

Hi, i have the following config in the system-auth files auth required /lib/security/$ISA/pam_env.so auth sufficient /lib/security/$ISA/pam_unix.so likeauth nullok auth required /lib/security/$ISA/pam_deny.so account required ... (2 Replies)
Discussion started by: yprudent
2 Replies

3. Red Hat

User Account Sync

Hi All, I want to know is there any way where if i add a user in a centos machine the can be replicated to another centos automatically. As i have setup DRBD with heartbeat for apache webserver everything is working fine but the only thing im stuck in is about system account for ftp. Can any... (3 Replies)
Discussion started by: search4u2003
3 Replies

4. Cybersecurity

Please help identify these user account

Please help me identify these user accounts.. bin, lp, nuucp, smbnull, mysql, tftp Can we remove these user or disable these users?We have to apply the security policy about the user identification.Since it was settup by our vendor long time ago. We do not have these informations about these... (3 Replies)
Discussion started by: rdstkg
3 Replies

5. Red Hat

RPM Updation & Keeping User Change files during removal

Hi All, I have a RPM for an Java based application. Currently it works fine. But recently I want to implement that when newer packages gets installed over the older one, the rpm should only update the older files with the newer one (I know this could be done by rpm -Uvh xxx.rpm), but it... (0 Replies)
Discussion started by: jw_amp
0 Replies

6. Shell Programming and Scripting

How to suspend a user account?

Hi, guys. I have two questions: I need to write a script, which can show all the non-suspended users on system, and suspend the selected user account. There are two things I am not sure: 1. How can I suspend user's account? What I think is: add a string to the encrypted password in shadow... (2 Replies)
Discussion started by: daikeyang
2 Replies

7. UNIX for Dummies Questions & Answers

Difference between : Locked User Account & Disabled User Accounts in Linux ?

Thanks AVKlinux (3 Replies)
Discussion started by: avklinux
3 Replies

8. Post Here to Contact Site Administrators and Moderators

user account

hi how to disable the useraccount in aix (should not remove). (1 Reply)
Discussion started by: chomca
1 Replies

9. UNIX for Dummies Questions & Answers

creatin user account

hi all, i m tryin to create a new account on the unix work station. do i use 'useradd' command? can u guyz advice on the usage of 'useradd' command as it can comes with 'useradd -D' or 'useradd -e' thanks :confused: (1 Reply)
Discussion started by: damian
1 Replies
Login or Register to Ask a Question