I have 3 RedHat 5.3 boxes, and I would like to create a user on box1 and automatically create users on box2 and 3. This is what I’m thinking:
1. keep a copy of passwd (passwd.bac)
2. find new users using diff passwd passwd.bac
3. get password from shadow file
4. make useradd.sh with only new users
5. if file exist, ftp useradd.sh to box2 and 3 using cron (maybe hourly)
6. execute useradd.sh on b2 & b3 using cron
7. delete all useradd.sh scripts so cron won’t pick them up.
8. cp passwd passwd.bac
Does this sound like a good approach?
Has anyone done anything like this? If so, how?
How can I handle a user being deleted on box 1? Because I believe the diff command will
Cause my code to add the existing user to 2 & 3, which will fail and may be okay. I don’t know how to say, “Tell me what’s in file A that’s not in file B”, but not vice versa. Can the diff command do this?
1. keep a copy of passwd (passwd.bac)
2. find new users using diff passwd passwd.bac
3. get password from shadow file
4. make useradd.sh with only new users
5. if file exist, ftp useradd.sh to box2 and 3 using cron (maybe hourly)
6. execute useradd.sh on b2 & b3 using cron
7. delete all useradd.sh scripts so cron won’t pick them up.
8. cp passwd passwd.bac
Does this sound like a good approach?
Has anyone done anything like this? If so, how?
How can I handle a user being deleted on box 1? Because I believe the diff command will
Cause my code to add the existing user to 2 & 3, which will fail and may be okay. I don’t know how to say, “Tell me what’s in file A that’s not in file B”, but not vice versa. Can the diff command do this?
Code:
#!/usr/bin/perl
use strict;
my $passBac = '/etc/passwd.bac';
my $passwd = '/etc/passwd';
my $shadow = 'shadow';
my $ofh = 'useradd.sh';
open(PASSWD, "$passwd") or die "can't open $passwd\n";
open(PASSBAC,"$passBac") or die "can't open $passBac\n";
open(SHADOW, "$shadow") or die "can't open $shadow\n";
my (@record) = split("\n",`diff $passwd $passBac`);
my %usersAdd;
foreach (@record) {
next unless /\:/;
s/^\W+//;
my ($user,$x,$uid,$x,$comment) = split(':',$_);
my $sp = ' ' x (8 - length($user));
$usersAdd{$user} = "useradd $user$sp : -u $uid -c \"$comment\" -m";
}
close PASSWD;
close PASSBAC;
my @shadow = <SHADOW>;
close SHADOW;
foreach ( @shadow ) {
my ($user,$passwd) = split(':',$_);
$usersAdd{$user} =~ s/:/-p '$passwd'/ if $usersAdd{$user};
}
if ( keys %usersAdd ) {
open(OFH,">$ofh");
#system("cp $passwd $passBac");
}
foreach ( sort values %usersAdd ) {
if ( /:/ ) {
print "* PW ERROR: $_\n";
}else{
print OFH "$_\n";
}
}