[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[K12OSN] Bulk add using Perl script



I have a perl script that was written for me and I
can't get it to work.  The directions I have are to
run the command:

bulkadd.pl <sourcefile.csv> results.csv
(What I put was: bulkadd.pl maddata.csv madtdata.csv)
What I got was a:    > sign and nothing else.

Unfortunately, I don't know how to use Perl so I need
some help. 

1.  Is my data in the correct format which uses csv
and is comma deliminated.
2.  Will the results be put into my password, shadow,
and group files?

Perl Script sent to me.

#!/usr/bin/perl

$shared="/home/shared";
# Lets do a little prelimanary work here.
# does the shared directory exist?
if ( ! -e $shared ) {
  mkdir ( $shared ) || die "You want me to do
what?!?!\n$!\n";
  system ( "chmod 2750 $shared");
  chown ( 0, 100, $shared );
}

# does the shared directory exist?
if ( ! -e "$shared/public" ) {
  mkdir ( "$shared/public" ) || die "Whoa, check out
the binary Carrion!\n$!\n";
  system ( "chmod 2770 $shared/public");
  chown ( 0, 100, "$shared/public" );
}

#Lets gather the names of our groups into and array to
check against
open (GROUP,"</etc/group") || die "Cannot open group
this is REALLY bad.\n$!\n";
while (<GROUP>) {
  chomp;
  s/:.*$//;
  push @groups, $_
}
close GROUP;

#Lets gather the names of our groups into and array to
check against
open (USER,"</etc/passwd") || die "Cannot open passwd
this is REALLY bad.\n$!\n";
while (<USER>) {
  chomp;
  s/:.*$//;
  push @users, $_
}
close USER;

while (<STDIN>) {
  #Grab a line and break it up into it's fields
  chomp $_ ;
#  s/(\S\S*),\s*(\S\S*),\s*(\S*).*//;
#  $lastname = $1; $firstname = $2; $group = $3;
  ($lastname,$firstname,$group) = split (",",$_);
  #clear extra spaces from begining and end of line
  $lastname =~ s/^\s*(\S.*\S)\s*$/\1/;
  $firstname =~ s/^\s*(\S.*\S)\s*$/\1/;
  $group =~ s/^\s*(\S.*\S)\s*$/\1/;
  #Start mangling our data into a username
  #First initial and Last name
  ( $username = $firstname.":".$lastname) =~
s/^(.).*:/\1/;
  $username =~ tr/[A-Z]/[a-z]/; #LowerCase
  $username =~ s/(........).*/\1/; # Trim to Eight
Characters or less
  $username =~ s/[ -].*$//; #Remove any secondary last
names
  $username =~ s/[ ,'.]//g; #Remove any special
characters
  $group =~ tr/[A-Z]/[a-z]/; #LowerCase the group name
as well
  #If the username we created is not unique reitterate
it until it is
  while ( ! $unique ) {
    $unique = 1 ;
    foreach $uid ( @users ) {
      if ( $username eq $uid ) {
        $unique = 0 ;
      }
    }
    if ( ! $unique ) { # if not unique try changing
the username a little
      if ( $username =~ /.*\d/ ) { # if there is an
itteration number
        $username++ ; #increment an already existing
number
      } else {
        if ( (length ($username)) <= 7 ) { #or add a
number at the end
          $username .= "0";
        } else {
          $username =~ s/.$/0/;
        }
      }
    }
    #And try the new username again
  } # If we ever get out of this loop we should now
have a unique username

  push (@users, $username) ; #Add it to our list of
usernames to check against
  $unique = 0; #Reset our unique variable

  # Check to see if we need a group created for this
person
  # Set a default group is there is none listed
  if ( $group =~ /^\s*$/ ) { $group = "users" };
  $grpexists = 0;
  foreach $gid ( @groups ) {
    if ( $group eq $gid ) { $grpexists = 1 } ;
  }
  if ( ! $grpexists ) {
    system ("groupadd $group");
  }
  push ( @groups , $group ); # Add this group to our
list

  #Does this group already have an shared directory?
  # does the shared directory exist?
  if ( ! -e "$shared/$group" && $group ne "users" ) {
    mkdir ( "$shared/$group" ) || die "This would've
worked better with /dev/null!\n$!\n";
    system ( "chmod 2770 $shared/$group");
    @grp = getgrnam ($group);
    $grid = $grp[2];
    chown ( 0, $grid, "$shared/$group" );
  }


  $password = int(rand(900)+100) . ".";
  for ($i = 1 ; $i <= 3 ; $i++) {
    $password .= sprintf ( "%c", int (rand (26)+97) );
  }
  system ( "useradd -m -c \"$firstname $lastname\" -g
users -d /home/$username $username");
  if ( $group ne "users" ) {  #Don't need to add
people to the default group
    open ( GROUP,"</etc/group" ) || die "Cannot open
/etc/group. This is bad.\n $!\n";
    open ( GROUPT,">/etc/group.tmp" ) || die "Cannot
open /etc/group.tmp This is bad.\n $!\n";
    while (<GROUP>) {
      chomp;
      if ( $_ =~ /^$group:/ ) {
        $_ .= ",$username" ;
      } 
      print GROUPT "$_\n" ;
    }
    close (GROUP);
    close (GROUPT);
    rename ("/etc/group.tmp","/etc/group");
  }

  open ( PSWD, "|passwd --stdin $username > /dev/null
2>&1 " );
  print PSWD "$password\n";
  close (PSWD);
  print
"$lastname,$firstname,$group,$username,$password\n";
  # HACK to add samba passwords
  `echo -e $password\n$password\n | smbpasswd -a -S
$username`;
}


Thank you for your help.

Jennifer Waters
Volunteer
Madison High School
Portland Public Schools
Portland, Oregon 



__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site
http://webhosting.yahoo.com





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]