Last active
August 29, 2015 14:28
-
-
Save deltafactory/ff90a945d8bd6ebced02 to your computer and use it in GitHub Desktop.
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
#-------------------------------------------------------------------------------------------------------------------# | |
# Version 1.00 | |
# | |
# This schript is to take a csv file and convert it to ldif format | |
# dependant on the requirement please ensure the csv is in the format as shown in code below else output will | |
# be wrong, current format of csv is: | |
# - First field: FullName | |
# - Second field: Mobile number | |
# - Third field: Email address | |
# - Fourth field: UserName | |
# - Fith field: Password | |
# - Sixth field: Dealer Code | |
# | |
# If the csv is not in the exact same order as shown above the output will be incorrect and code must be adapted | |
# to accomodate the new format. | |
#-------------------------------------------------------------------------------------------------------------------# | |
use FileHandle; #use the filehandle class to handle external files for processing. | |
my(@details);#Declare the arrays | |
# User will be asked for what file to be converted | |
$csvFile = &promptUser("Name of file to be converted from CSV to LDIF 'Add' File"); | |
$inputfile = new FileHandle($csvFile) || | |
die "Can't open the import file!\n"; | |
$outfile = new FileHandle(">$csvFile.ldif") || | |
die "Can't create or open the output file!\n"; | |
$logfile = new FileHandle(">AddData.log") || | |
die "Can't create or open the log file!\n"; | |
print $outfile "version: 1\n\n"; #First entry will be version 1 of ldif | |
# Loop to run through the entire file of enteries by line to convert them to ldif format | |
my $linecount = 0; | |
while(my $line = <$inputfile>) { | |
$linecount++; | |
chomp($line); | |
@details= split(',', $line); #Breakup the csv line to store values in an array | |
# The below is taking the array and storing it in variables to be used as the loop executes | |
$fullName = $details[0]; | |
$mobile = $details[1]; | |
$email = $details[2]; | |
$userName = $details[3]; | |
$password = $details[4]; | |
$dealerCode = $details[5]; | |
$cn = $userName; | |
my @sn= split ('_', $fullName); | |
#The actual format of how the ldif output for each entery will look like in the exported file | |
print $outfile "dn: cn=$cn,cn=thirdPartyUsers,dc=enterprise,dc=mtn,dc=co,dc=za\n"; | |
print $outfile "objectclass: person\n"; | |
print $outfile "objectclass: top\n"; | |
print $outfile "objectclass: organizationalPerson\n"; | |
print $outfile "objectclass: orclUserV2\n"; | |
print $outfile "objectclass: inetOrgPerson\n"; | |
print $outfile "cn: $cn\n"; | |
print $outfile "sn: $sn[1] $sn[2] $sn[3]\n"; | |
print $outfile "givenname: $sn[0]\n"; | |
print $outfile "mail: $email\n"; | |
print $outfile "mobile: $mobile\n"; | |
print $outfile "uid: $userName\n"; | |
print $outfile "userpassword: $password\n"; | |
print $outfile "\n"; | |
} | |
sub promptUser { | |
#-----------------------------------------------------------------# | |
# two possible input arguments - $promptString, and $defaultValue # | |
# make the input arguments local variables. # | |
#-----------------------------------------------------------------# | |
local($promptString,$defaultValue) =@_; | |
#-----------------------------------------------------------------# | |
# if there is a default value, use the first print statement; if # | |
# no default is provided, print the second string. # | |
#-----------------------------------------------------------------# | |
if ($defaultValue) { | |
print $promptString, "[", $defaultValue, "]: "; | |
} else { | |
print $promptString, ": "; | |
} | |
$| = 1; # force a flush after our print | |
$_ = <STDIN>; # get the input from STDIN (presumably the keyboard) | |
#----------------------------------------------------------------# | |
# remove the newline character from the end of the input the user # | |
# gave us. # | |
#----------------------------------------------------------------# | |
chomp; | |
#---------------------------------------------------------------# | |
# if we had a $default value, and the user gave us input, then # | |
# return the input; if we had a default, and they gave us no # | |
# no input, return the $defaultValue. # | |
# # | |
# if we did not have a default value, then just return whatever # | |
# the user gave us. if they just hit the <enter> key, # | |
# the calling routine will have to deal with that. # | |
#---------------------------------------------------------------# | |
if ("$defaultValue") { | |
return $_ ? $_ : $defaultValue; # return $_ if it has a value | |
} else { | |
return $_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment