script for creating aix user


 
Thread Tools Search this Thread
Operating Systems AIX script for creating aix user
# 8  
Old 04-11-2008
Have a look at the /usr/lib/security/mkuser.sys script (which is executed by AIX when creating a new user). This is a good starting point for your own script. It can be extended easily, e.g. copying of .profile and/or .kshrc, setting an initial password, creating NIS maps, set disk quotas, sending an email to root just to name a few.
# 9  
Old 04-11-2008
Sorry, took me a bit longer to post it as i had a busy schedule yesterday evening. Here is the promised script:

Code:
# ------------------------------------------------------------------------------
# f_CheckUser                                                   check UNIX user
# ------------------------------------------------------------------------------
# Author.....: Wolf Machowitsch
# last update: 2007 05 23    by: Wolf Machowitsch
# ------------------------------------------------------------------------------
# Revision Log:
# - 0.99   2007 05 23   Original Creation
#                       users are being checked if they exist (return 0)
#                       and possible problems in creating the user (name
#                       and or UID taken, both taken by different users,
#                       all these conditions lead to non-zero return values)
#                       are propagated back.
#                       As of now only AIX and Linux are explicitly supported.
#
# ------------------------------------------------------------------------------
# Usage:
#
#     f_CheckUser char UserName int UserID
#
#     checks if a user named <UserName> and with UID <UserID> does exist. 
#
#     Example:  f_CheckUser foo 999          # checks if a user named foo
#                                            # with UID 999 does exist
#
# Prerequisites:
# -   to use this function, the FPATH variable must be set
#
# ------------------------------------------------------------------------------
# Documentation:
#     f_CheckUser() tests for the existence of UNIX-useraccounts.
#     If the user exists and is correct (correct name and UID, other
#     properties are not checked) 0 is returned.
#     If the user does not exist (meaning neither a user with that name
#     nor a user with that UID does exist) 1 is being returned. If either
#     the user name or the UID is taken by an existing user account, but the
#     respective other ingredient is not, then 2 or 3 respectively is being
#     returned. A return value of 4 is being caused by a resource conflict:
#     both name and UID are taken, but by different accounts.
#     
#     Example:
#          existing users | UID
#          foo            | 20
#          bar            | 30
#
#          f_CheckUser foo  20        # yields 0
#          f_CheckUser kuno 25        # yields 1
#          f_CheckUser foo  25        # yields 2
#          f_CheckUser kuno 20        # yields 3
#          f_CheckUser bar  20        # yields 4
#          f_CheckUser foo            # yields 5 (no second parameter)
#          
#
#     Parameters: char UserName      name of the user
#                 int  UID           ID of the user
#
#     returns:    0: user exists
#                 1: user does not exist
#                 2: user name exists but with wrong UID
#                 3: UID exists but with wrong user name
#                 4: resource conflict: UserName with different UID AND
#                                       UID with different user name
#                 5: parameter/other/internal error
#
# ------------------------------------------------------------------------------
# known bugs:
#
# -  none
# ------------------------------------------------------------------------------
# .....................(C) 2007 Wolf Machowitsch ...............................
# ------------------------------------------------------------------------------

f_CheckUser ()
{

$chFullDebug
                                                 # internal variables
typeset -i iRetVal=0                             # return value (see docu)
typeset    chUserName="$1"                       # user name
typeset -i iUID="$2"                             # UID

typeset    chNameTest=""                         # buffer f. test by name
typeset    chIDTest=""                           # buffer f. test by ID
typeset -i iErrLvl=0                             # return code buffer

if [ -n "$2" ] ; then                            # UID given ?
     iRetVal=5
fi
if [ -n "$1" ] ; then                            # name given ?
     iRetVal=5
fi

if [ $iRetVal -gt 0 ] ; then
     return $iRetVal
fi


case $OS in
     AIX)
	  chNameTest="$( \
			 lsuser -a id $chUserName 2>/dev/null |\
			 sed 's/[ 	]*id=/:/' \
		       )"
	  chIDTest="$( \
			 lsuser -a id ALL |\
			 sed -n '/id='"${iUID}"'[ 	]*$/ {
					s/[ 	]*id=/:/p
					}' \
		     )"
	  ;;

     Linux)
	  chNameTest="$( \
			  grep "^${chUserName}:" /etc/passwd |\
			  cut -d':' -f1,3 \
		       )"
	  chIDTest="$( \
			  grep "^\([^:]*:\)\{2\}${iUID}:" /etc/passwd |\
			  cut -d':' -f1,3 \
		     )"
	  ;;

     *)
	  f_CmdWarning "no explicit rule to check $OS for users"
	  chNameTest="$( \
			  grep "^${chUserName}:" /etc/passwd |\
			  cut -d':' -f1,3 \
		       )"
	  chIDTest="$( \
			  grep "^\([^:]*:\)\{2\}${iUID}:" /etc/passwd |\
			  cut -d':' -f1,3 \
		     )"
	  ;;

esac

if [ -z "$chNameTest" -a -z "$chIDTest" ] ; then
     iRetVal=1                                   # name AND UID not taken
elif [ -n "$chNameTest" -a -z "$chIDTest" ] ; then
     iRetVal=2                                   # name taken, UID not taken
elif [ -z "$chNameTest" -a -n "$chIDTest" ] ; then
     iRetVal=3                                   # name not taken, UID taken
else
					         # both name and UID taken
     if [ "$chNameTest" == "$chIDTest" ] ; then
	  iRetVal=0                              # by the same user -> ok
     else
	  iRetVal=4                              # by different users -> shit
     fi
fi

return $iRetVal

}
# --- EOF f_CheckUser

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Prevent user from creating new user from his login

Hi Experts, Need your support Redhat 6.5 I want to create a user with all(read, write, execute) privileges except that user should not be able to create any new user from his login to perform any task. (10 Replies)
Discussion started by: as7951
10 Replies

2. Shell Programming and Scripting

Creating a script requiring a pause for user input

Hi I'm trying to create a basic script that pauses for user input to verify a file name before generating the output. I have numerous SSL certificate files which I am trying to determine the expiry date so what I'm trying to do is write a script so that is pauses to request the name of the .pem... (9 Replies)
Discussion started by: Buddyluv
9 Replies

3. Shell Programming and Scripting

How to write bash script for creating user on multiple Linux hosts?

I wonder whether someone can help me with what I'm trying to achieve Basically, the objective is one script to create new user on more than 70 linux hosts if required. Everything works apart from the highlighted part. It gave me an output passwd: Unknown user name ''. when try to set... (35 Replies)
Discussion started by: fugeulu
35 Replies

4. AIX

AIX How to run a Shell Script by changing the User

Hi All, Currently our application is running on the server having AIX 5.3 OS. What we intend to do is to run a shell script owned by another user and needs to be run as that particular user. I was trying to create a shell script using the su command before running the actual script (which... (4 Replies)
Discussion started by: acoomer
4 Replies

5. Shell Programming and Scripting

Hp-UX, SUSE, and AIX LDAP User Script Help

Hi, I have been asked to create a ksh script that will search against an LDAP directory from various HP-UX, SUSE, and AIX 5.3 and 6.1 machines. The objective is to verify the boxes are successfully authenticating users from the LDAP store. This is something I've never done, and I could use... (0 Replies)
Discussion started by: tekster2
0 Replies

6. Shell Programming and Scripting

Creating oracle user and giving him grants using shell script

Hi , I want to write a shell script that can create oracle database user and grants permission to this user. Thanks & Regards, Deepak (4 Replies)
Discussion started by: Deepakjha
4 Replies

7. Shell Programming and Scripting

Creating and Executing a script of aliases in AIX

hi, I am trying to create a script in AIX (5.3 I think), then run it. here's what I have: /home/me $ vi first.aliases ... alias cdblah='cd /blah' alias cdho='cd /ho' alias ssr='sudo su - random' ~ ...end of first.aliases /home/me $ ./first.aliases /home/me $ ssr ksh: ssr: not found.... (2 Replies)
Discussion started by: heetertc
2 Replies

8. UNIX for Dummies Questions & Answers

AIX:creating user with username more than 8 chars

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (0 Replies)
Discussion started by: anuafs84
0 Replies

9. AIX

Limiting length of user in while creating user

Hi all, I am a newbe to aix 5.2. I want to specify the characters used by users while creating user in aix like specifying the length of the password should i use some sript for that if it is then please let me know how to do this if yes give me the link for the scripts. Thanks in advance ... (2 Replies)
Discussion started by: Satya Mishra
2 Replies

10. UNIX for Advanced & Expert Users

creating user accounts in AIX

Hello all: I am new to UNIX and I am given the responsibility of administering a UNIX machine recently. The system is a IBM AIX 3.1. As a part of my duties I recently created some user accounts using "smit". It looked as if everything went well. But, after creating the account, I logged into... (3 Replies)
Discussion started by: pdepa
3 Replies
Login or Register to Ask a Question