Operating System - HP-UX
1830213 Members
1447 Online
109999 Solutions
New Discussion

SMALL GIFT TO UNIX COMMUNITY: Perl script for add batch users

 
VK2COT
Honored Contributor

SMALL GIFT TO UNIX COMMUNITY: Perl script for add batch users

Hello,

Over the last 22 years I received lot of help
from known and unknown Unix admins all over
the world.

I also offered my advice whenever I could.
I cannot think of a friendlier community
(some exceptions apply on occasion :)).

Here is one of my attempts to help others.

Several days ago, someone asked me if I
had a script to add Unix acounts in bulk.

I sat down and wrote a Perl script that
works on HP-UX, Linux and Solaris servers.

It provides a summary of steps
required to generate the accounts (easy
to run them anyway).

http://www.circlingcycle.com.au/Unix-sources/add-batch-Unix-accounts.pl.txt

Best wishes from "down-under" (far away Australia)

In Australia, even the sky looks upside
down :)

Amateur Radio VK2COT
VK2COT - Dusan Baljevic
6 REPLIES 6
Fat Scrape
Honored Contributor

Re: SMALL GIFT TO UNIX COMMUNITY: Perl script for add batch users

Dusan,

Thank

Regards,

Fat Scrape
VK2COT
Honored Contributor

Re: SMALL GIFT TO UNIX COMMUNITY: Perl script for add batch users

Hello,

I added support for AIX as well.

Basically, the scritp now grew to the following options:

USAGE: $CMD [-h] [-f accfile] [-p] [-s] [-u startUID] [-x]
Tested operating systems: AIX, HP-UX, Linux, SunOS (Solaris)

-h Print this help message
-f accfile Text file with new login names
One per line, or more-detailed (colon-separated):
newuser:accpasswd:accuid:accgid:GECOS:acchome:accshell
-p User pasword authentication for SSH to remote server
-s Array of remote servers and passwords to execute commands
-u startUID Starting UID number for new accounts
-x Force password change at first login

Examples of entries in the file:

someuser
othacc::500::Jim Smith:/home/newuser:/bin/csh
oraadm:Realpasswd:::::/bin/bash
myacc:::130:::
nwuser5:accpasswd:700:100:Joe Bloggs:/home/nwuser5:/bin/ksh

NOTE: The remote execution is currently 80% ready
I need to think about best method to pass the
commands for remote execution (scp, socket, or...)

I also added support for SSH (password and key exchange).

Because Solaris versio on useradd(1) does
not have "-p" flag, I decided to use Expect module to set password for users:

# Force autoflush right away and after every
# write or print on the currently selected output channel.

$| = 1;
$Expect::Log_Stdout = 0;

# First we have to initialize STDIN in to an expect object.
my $stdin = Expect->exp_init( \*STDIN );
my $stdout = Expect->exp_init( \*STDOUT );

# Here are the prompts
#
my $Prompt = "New Password:";
my $Prompt2 = "Re-enter new Password:";
$Command = "passwd $newuser";

my $sh = Expect->spawn("$Command") or die "ERROR: Spawn failed: $?";
$sh->log_stdout(0); # Log to STDOUT
$sh->expect( 5, '-re', "$Prompt");
print $sh "$Password\n";
$sh->expect( 5, '-re', "$Prompt2");
print $sh "$Password\n";

# Close the command nicely
# No need to be rude and use hard_close()
#
$sh->soft_close();

# Reset autoflush
#
$| = 0;

I am closing the thread now.

Bst wishes,

VK2COT
VK2COT - Dusan Baljevic
Li Hao
Advisor

Re: SMALL GIFT TO UNIX COMMUNITY: Perl script for add batch users

Hi Dusan,

Thanks for sharing. I am looking for the similar tools, but with a slightly different usage. I would like to use it to change user password on remote servers. Can this perl script be used to change password instead of create user?

As for the remote, I try the ssh command line, when I issue passwd command with ssh, I always get pam_authtok: system error when I try it in HP UX. I have get the remote ssh working in Linux, how does hpux handle this?
VK2COT
Honored Contributor

Re: SMALL GIFT TO UNIX COMMUNITY: Perl script for add batch users

Hello,

Yes, slight modification ot the script could be used
just to change passwords.

For HP-UX part, simply remove
lines with useradd(1) statements.

It will then use "usermod.sam -p ..."
to change passwords.

About SSH error. HP-UX setup should
be similar to Linux. I am not sure
what your problem exactly is. It could
be even permissions on home directory
on the remote server. For example, if you use
authorized keys, then SSH by default
uses "StrictModes yes" and SSH sessions
fail if home directory is world-readable...

Cheers,

VK2COT


VK2COT - Dusan Baljevic
IanPD
New Member

Re: SMALL GIFT TO UNIX COMMUNITY: Perl script for add batch users

Can this script be used to change passwords for a list of usernames stored in a txt file?
Mustafa Gulercan
Respected Contributor

Re: SMALL GIFT TO UNIX COMMUNITY: Perl script for add batch users

hi Dusan,

thank you very much..

regards,
mustafa