Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
User creation and password setting in Perl code

Posted by shamim_99 (shamim_99), 23 July 2004
Could you please  any technique in Perl which can help me create multiple users and respective passwords automatically in Unix environment.  I would like to  create a Perl script which will automate the process of user creation and password setting without human interaction.


Posted by admin (Graham Ellis), 23 July 2004
Are you looking for Unix passwords or application passwords?  Are you using a service such as LDAP, NIS or NIS+?   Sorry to answer a question with a question ... but the answer and pointers I give will differ!

Posted by shamim_99 (shamim_99), 23 July 2004
I am creating Unix user by "adduser" and subsequently password setting. I am using Perl script with System() function.


Posted by admin (Graham Ellis), 23 July 2004
Ah yes ... your problem will come when you try to set the password automatically as it reads its input from /dev/tty rather than from stdin.   You might like to have a look at the Perl / Expect module on the CPAN which will let you start up commands such as adduser and passwd from within your Perl program and talk to them as is intermdiate inputs were being made at the keyboard.   I don't have an example to hand (although I'm familiar with one under Tcl), so when you get it working I would be very interested to see the password changing code.

Posted by shamim_99 (shamim_99), 24 July 2004

##expect script to reset/set password

spawn passwd [lindex $argv 0]
set password [lindex $argv 1]
expect "password:"
sleep 2
send "$password\r"
expect "password:"
sleep 2
send "$password\r"
expect eof

This is expect script I am using for password. But I want to do it without Expect . I want to do it by only using Perl. Is this possible?

Posted by admin (Graham Ellis), 24 July 2004
Yes, it's possible.

In my previous post, I recommended that you use Perl's Expect module from the CPAN - it's at and using it will
a) Avoid you having to write a lot of code that someone's already written and
b) Save you having to use both Tcl and Perl for your task.

I'm not clear whether you object to using Perl's expect, preferring to "roll you own" (why??), or whether my previous post wasn't clear and you thought my advise was to use Don Libes' original Expect that sits on top of Tcl. If that's the case, I can understand that you don't want to have an application that has Perl bits and Tcl bits bolted together in that case.

Posted by shamim_99 (shamim_99), 24 July 2004
I purpose is not to use any module external module then available with Perl intepreter. If I use any extra module  then  I need to analys the vulnerability of that module and anything used extra.  Saying in other way it is not encouraged to  use any extra module, may be it is Perl module or TCL.

Please help me.

Thanks Graham, I am really glad to know  your  response and it is so quick and very informative.


Posted by admin (Graham Ellis), 24 July 2004
Go ahead, then ... write your own code in Perl; I can undertand you wanting code that you totally understand, so are fully aware of the security impllications.

If you want a sample of how to do it, down load Perl / Expect from the URL in my previous post - it comes as source code so it should provide you with a good example    

Posted by mark_weaver (mark_weaver), 12 August 2004
Yesterday, while I was working on a script to do just what this thread is about I happened on this thread. I knew it could be done easily, but I was missing certain peices of the puzzle. Though this might be late in coming as an answer to the original question posted my shamim_99 I thought I'd post it anyway for anyone else that may need this.

Below is the code that I came up with. Its been tested and found to work well on Linux Mandrake 8.2. I haven't yet tested it on a RedHat platform, but I have every reason to believe that it will work just as well there. That is where its final destination will be as we're getting ready to migrate from an older machine/operating system to a new box and new version of RedHat Linux.

The program can be viewed and downloaded at this address.

Posted by admin (Graham Ellis), 12 August 2004
Great answer / solution there, Mark ... and welcome to the board.  You leave me thinking "I really SHOULD have come up with that" ... but then YOU did, and I didn't, and I take my (Red?) Hat off to you.  

Posted by mark_weaver (mark_weaver), 12 August 2004
 Why thank you kind sir. It was a pleasure to share it, and thankyou for the welcome.

Posted by shamim_99 (shamim_99), 16 August 2004
The code  for Password setting in Perl is

use Expect;

my $exp=Expect->spawn("passwd <userName>");

             [ qr/Password:/ => sub { my $exp = shift;

                                    exp_continue; } ],
              [ qr/Password:/ => sub{ my $exp=shift;
                                       } ],

 # if no longer needed, do a soft_close to nicely shut down the command

 # or be less patient with

Should be install before running the above code. Both the package available at

Posted by shamim_99 (shamim_99), 16 August 2004
Hi mark_weaver,

Thanks for the code for password change in perl, but I would like to know whether this code will work on Solaris, HP-Unix, and AIX flavour of Unix. Or this is only for Linux(May be Mandrake or RedHat).
Or what changes it requires to work on  other flavour of Unix.

Please suggest.

Posted by mark_weaver (mark_weaver), 16 August 2004
Hi shamim,

Since I don't have access to any Solaris or HP_Unix machines such as you've mentioned I can't say for certain whether it will work as is or not. foundationally speaking it will work, however certain changes will have to be made to the program such as the location of the /etc/shadow password file. On most major linux distros the shadow password file is located in /etc. To date I've tested this program on Redhat 9 and mandrake 8.2 thru 10 because those are the machines I personally have access to. If you'd like to test the program on those other platforms I'd be more then happy to work with you on getting everything situated so that the program works as well on those platforms as well.

for instance...on line 79 you find this statement:

my $PWD_BACKUP = "/etc/shadow" . "_new" . "$ETC_TIME";

The first part of that string is the path to the shadow password file. Since I have no experience with those platforms you've mentioned I'm not even sure how they store password information or whether they even use the same c libraries to handle encryption although since Linux has its roots in Unix I can't imagine it would be too much different.

As far as I know some common adjustments you would have to make in the program deal with exactly where the password file is located and how the system handles encryption. Test it out and see how it goes with a dummy list of three or four users to test things out.

Posted by shamim_99 (shamim_99), 17 August 2004
Hi mark_weaver,

I am unable to locate on Internet. could you please send to me at is required to run, which has been developed by you.

Thanks in advance

Posted by mark_weaver (mark_weaver), 17 August 2004
Sure can....

its on its way. Look for a file attachment named "dbLib.tgz"

it can also be downloaded here:

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho