Created
April 8, 2009 21:04
-
-
Save antonlindstrom/92036 to your computer and use it in GitHub Desktop.
School assignment #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# | |
# Batch adding users. | |
# | |
# Opens files users.csv and passwd, adding users from .csv file. | |
# The program processes these and is using a letter first to differ | |
# users between existing then year when user is created and three | |
# letters from name and two from lastname. | |
# Full name and ssn in comment. | |
# Subroutine for opening file. | |
sub openfile { | |
# Input is file. | |
($file) = (@_); | |
# Open file and put it in array @filecontents. | |
open(FILE, $file) || die("Could not open $file\n$!"); | |
@filecontents = <FILE>; | |
# Close file. | |
close(FILE); | |
# Return file content. | |
return @filecontents; | |
} | |
# Assign users.csv content to @lines. | |
@lines = &openfile("users.csv"); | |
@existingusers = &openfile("passwd"); | |
# Select year. $year is ex. 09. | |
$year = substr(localtime, 22, 23); | |
# Check if user exists in passwd. | |
foreach (@existingusers) { | |
# Split row by colon. | |
@exuservals = split(/:/, $_); | |
if($exuservals[0] =~ /[a-z]{1}[0-9]{2}[a-z]+/) { | |
# Split value 5 (comment field); | |
@expnr = reverse(split(' ', $exuservals[4])); | |
# Put values in arrays, username ($exuservals[0]), $expnr[2] is pnr. | |
push(@allusers, $exuservals[0]); | |
push(@expnrs, $expnr[0]); | |
} | |
} | |
# Columns with userdata are assigned to $lastname, $name and $pnr. | |
foreach $line(@lines) { | |
# Split line into lastname, name and pnr (ssn). | |
($lastname, $name, $pnr) = split(/,/, $line); | |
# Replace non-ascii with z. | |
$lastname =~ s/([^[:ascii:]])+/z/g; | |
$name =~ s/([^[:ascii:]])+/z/g; | |
# Parts of first and lastnames. | |
$npart = substr($name, 0, 3); | |
$lpart = substr($lastname, 0, 2); | |
# Lowercase string. | |
$thisuser = lc($year.$npart.$lpart); | |
$comment = "$name $lastname $pnr"; | |
# Remove newline chars. | |
chomp($comment); | |
chomp($pnr); | |
# If user exists, set skip to 1. | |
if ( grep(/$pnr/, @expnrs) ) { | |
print "$name $lastname ($pnr) exists, skipping..\n"; | |
$skip = 1; | |
} else { | |
$skip = ""; | |
} | |
# If $skip is true, jump to next iteration. | |
next if ($skip); | |
# Set prefixarray to alphabetic. | |
@prefix = ('a'..'z'); | |
$i = 0; | |
#$newuserok = 0; | |
# Check if username with prefix a-z exists | |
foreach (@allusers) { | |
# If line is equal to ex. a-z09antli | |
if ($_ eq $prefix[$i].$thisuser) { | |
$i++; | |
} | |
} | |
# Set username prefix. $prefix[a-z]. | |
$newuser = $prefix[$i].$thisuser; | |
# Push $newuser to array @allusers, for checking existing users. | |
push(@allusers, $newuser); | |
# Add new user and comment to hash. Password is pnr in %newpass. | |
$newusers{$newuser} = $comment; | |
$newpass{$newuser} = reverse($pnr); | |
} | |
print "ADDING:\n"; | |
# Put all userinfo into var $add, print and exec. | |
foreach $finalname( keys %newusers) { | |
$addpass = $newpass{$finalname}; | |
$addcomment = $newusers{$finalname}; | |
# Unix command for adding user. | |
$add = "useradd -p $addpass -d /home/$finalname -m -c $addcomment $finalname"; | |
print "$add\n"; | |
#system($add); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment