Shell script to add users on solaris

Hi admins,
I am trying to run a script to add users on solaris with password:

I am using crypt for passwords:

The part of my scripts is as below:

 
if [ $? -eq 0 ]; then
                echo "$username exists!"
                exit 1
        else
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
                useradd -m -p $password $username
                [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"

After running the script:
Enter username:
Enter password:

Then I got error:

Enter password : UX: useradd: ERROR: project pytprt does not exist.  Choose another.
Failed to add a user!

I also did
1.

perl -e 'print crypt("password", "salt"),"\n"
password="1YelloDog@"
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)

But still getting the error:

Please help me out

The -p parameter to the useradd command is not used to specify a password, it's used for the project.

ETA: You may want to do it in two commands - add the user, and then use passwd to change the user's password.

But any other way for passwds in shell script for solaris.
It will be hectic to use passwd command for all...

Suggest.

Yes there are,

  • It is a bit tricky one which involves directly messing up with the shadow file.
    what needs to be done is, after the creation of an ID using useradd command, to place the encrypted passwd in the password field in the row for the ID in shadow file.

  • This is more safe, we need to work with spawn and expect to place the password for the ID which has been created.

Cheers,
Vishal

It is not possible to directly set a specific password for a unix account with standard unix commands.

Your earlier posts suggest that you may know the correct value for the password "salt" without stating that value. Afaik the "crypt" program has nothing to do with passwords. Afaikn you would need knowledge of the correct "salt" value and a custom "C" language program to produce a valid encrypted password and then (as "vishalaswani" notes) some skilled editing to modify the shadow file.

I have a "C" program to set a unix password to a given value but there is no way that I would publish such a program on the Internet.

As "dangral" suggests, consider using the "passwd" command.

you can also use expect to pass the password to the passwd command.