Create new user account and password in shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Create new user account and password in shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-18-2010
killuane killuane is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 10 May 2010, 5:10 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Create new user account and password in shell script

I am trying to create a shell script that will:
  1. check if a specific user already exists
  2. if not, create a specific group and create the user in that group
  3. assign a password to that user, where the password is passed in as a parameter to the script
The problem that I need help with is 3 on Solaris and AIX.

I can do 1 (using the id command) and 2 (using mkgroup or groupadd and useradd commands), but setting the password is where I am having problems. I have got it to work on Linux by redirecting input to the passwd command but I can not find how to get the same functionality to work on AIX and Solaris. This is what I have so far:



Code:
 
PLATFORM=`uname`
id $NEW_USER_ID > /dev/null 2>&1
if [ "$?" != "0" ]
then
   echo "$NEW_USER_ID user does not exist. Attempting to create user and group"
 
   if [ "$PLATFORM" = "AIX" ]
   then
      mkgroup $NEW_GROUP
   else
      groupadd $NEW_GROUP
   fi
   if [ "$?" != "0" ]
   then
      echo "Failed to create group"
   fi
 
   useradd -g $NEW_GROUP -s /usr/bin/ksh $NEW_USER_ID 
   if [ "$?" != "0" ]
   then
      echo "Failed to create $NEW_USER_ID user"
   fi
 
   if [ "$PLATFORM" = "Linux" ]
   then
      passwd --stdin $NEW_USER_ID <<EOF
$NEW_PASSWD
$NEW_PASSWD
EOF
 
      if [ "$?" != "0" ]
      then
         echo "Failed to set $NEW_USER_ID password"
      fi
   fi
fi

I've searched the forums and found some similar postings but nothing seems to answer exactly what I'm looking for.


Some additional information that might preempt some questions:
  • the script will only be run by the root user
  • there is not an issue with storing the password unencrypted in a flat file because it will be entered by the user as a parameter at run-time
  • the script has to be executable on Linux X86, Linux s390x, Solaris 10, and AIX 6
  • the script can not use any command, utility, or any other software that is not installed on the OS by default, so an answer along the lines of "download and install this..." is no good to me.
  • I can not use an answer that involves using perl
  • I can not use an answer that involves using the expect command
If it can't be done, because of something like OS security restrictions and limitations, then please let me know and I'll stop trying to script it!

Thanks in advance for any help or pointers that anyone can provide.
Sponsored Links
    #2  
Old 01-18-2010
dinjo_jo dinjo_jo is offline
Registered User
 
Join Date: May 2008
Last Activity: 13 December 2011, 4:12 AM EST
Posts: 232
Thanks: 1
Thanked 6 Times in 5 Posts
What was done in case of Solaris what were the errors ?
Sponsored Links
    #3  
Old 01-19-2010
killuane killuane is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 10 May 2010, 5:10 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
On Solaris I have tried:

1) redirecting the input as follows:


Code:
passwd $NEW_USER_ID <<EOF
> $NEW_PASSWD
> $NEW_PASSWD
> EOF

but I get prompted to enter the password interactively:
New Password:
2) putting the passwords into a file


Code:
echo $NEW_PASSWD >> tempfile
echo $NEW_PASSWD >> tempfile

then tried to redirect the contents of that file into the passwd command:


Code:
passwd $NEW_USER_ID < `cat tempfile`

from which I get the error:
ksh: $NEW_PASSWD^J$NEW_PASSWD: cannot open
(the error actually shows the real password not the environment variable)

or:


Code:
passwd $NEW_USER_ID < tempfile

but again I get prompted to enter the password interactively:
New Password:
3) write a mini shell script:


Code:
sleep 1
echo $NEW_PASSWD
sleep 1
echo $NEW_PASSWD

then tried to execute that script and pipe the output to the passwd command:


Code:
./script | passwd $NEW_USER_ID

but again I get prompted to enter the password interactively:
New Password:
So, it looks to me like my attempts to redirect stdin to the passwd command are failing but I don't know any other way to do it.
    #4  
Old 01-19-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 July 2014, 8:40 AM EDT
Location: Zürich
Posts: 8,421
Thanks: 328
Thanked 1,028 Times in 899 Posts
Hi.

There is no way to do what you want on Solaris using the passwd command. You could download and compile a tool like chpasswd, but you don't want to do that.

One option is to change the password on one server, and then use information in the shadow file to update the other servers.

For AIX, the tool chpasswd should already be available. It's easy enough to use - check the man page.
Sponsored Links
    #5  
Old 01-20-2010
killuane killuane is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 10 May 2010, 5:10 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks scottn. Appreciate the answer. I'll use chpasswd on AIX and think up an alternative solution on Solaris.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need a Shell script to create Multiple User Accounts rksubash Shell Programming and Scripting 1 06-14-2008 08:15 AM
script to create multiple instances of a user account across LPAR's kcampbell Filesystems, Disks and Memory 1 10-19-2006 10:42 PM
How to create a new ftp user account with limited access..? Jeremy3 Solaris 6 09-01-2006 05:52 AM
how can I change user name and password , of account ? umen Solaris 5 12-22-2005 06:10 PM
create or modify user account to have same access as root xadamz23 UNIX for Dummies Questions & Answers 6 08-07-2003 07:59 PM



All times are GMT -4. The time now is 09:56 AM.