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:

 
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.

What was done in case of Solaris what were the errors ?

On Solaris I have tried:

1) redirecting the input as follows:

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

echo $NEW_PASSWD >> tempfile
echo $NEW_PASSWD >> tempfile

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

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:

passwd $NEW_USER_ID < tempfile

but again I get prompted to enter the password interactively:

New Password:

3) write a mini shell script:

sleep 1
echo $NEW_PASSWD
sleep 1
echo $NEW_PASSWD

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

./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.

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.

Thanks scottn. Appreciate the answer. I'll use chpasswd on AIX and think up an alternative solution on Solaris.