Submit to DiggSubmit to FacebookSubmit to Google PlusSubmit to StumbleuponSubmit to Twitter

Perl

Perl will save your life.

Perl is an awesomely open-ended scripting language with powers that obviously cannot be extolled enough in this simple crappy page.

Here's some helpful perl scriptage:

Perl one-liners

Replace pattern in a large text file with another one using substitution.

perl -p -i.bak -e 's/pattern1/pattern2/g' sourcefile

Replacing a pattern with a random number between 0 and 500, entire file.

perl -i.bak -pe "s/pattern1/int rand(500)/e" sourcefile

Moving perl files to another computer, you want to update the perl interpreter path.

perl -i -ple 'print q{#!/usr/bin/perl} if $. == 1; close ARGV if eof' *.pl

Delete first 10 lines of a file.

perl -i.bak -ne 'print unless 1 .. 10' source.txt

Reverses the contents of a file.

perl -e 'print reverse <>' source.txt 

Shift and Unshift with perl

Like pop and push that's available in most languages, Perl lets you do the same thing but on the other side of the array using shift and unshift.

Actually, many languages don't let you do that on an array at all, you have to use a stack or something.

@array = qw#monkey cheese ralph#;
$a = shift @array; #gets monkey
$b = shift @array; #gets cheese
$c = shift @array; #gets ralph
$d = shift @array; #gets undef

This is the easiest way to get command-line arguments in Perl. If someone runs:

./foobar.pl monkey cheese ralph

Then you can get the arguments by shifting. The arguments end up in @ARGV:

$a = shift @ARGV;  	#gets monkey
$b = shift @ARGV; #gets cheese
$c = shift @ARGV; #gets ralph
$d = shift @ARGV; #gets undef

But @ARGV is the default variable for shift!

$a = shift;  	#gets monkey
$b = shift; #gets cheese
$c = shift; #gets ralph
$d = shift; #gets undef

Using Perl to add users to OpenLDAP

Assuming that the input file is a comma-separated-values text file in the form:

username,firstname,lastname,secondarygroup

...then this script will create the LDIF files and import them into OpenLDAP.

#!/usr/bin/perl -w
#2008, copy all you like.
#csv in form: username,firstname,lastname,2ndarygroup.
#inserts into openLDAP as POSIX users.

use strict;

my $howMany = 11000; #maximum users to insert.
my $uidNum=3000; #base UID number (the unix UID, not ldap UID).

#---------------------------------- make ldif file ------
open(RH, "sourcefile.csv"); #the source file.
open(WH, ">addme.ldif"); #makes an ldif of users to add.
open(WH2, ">addgrp.ldif"); #makes an ldif full of posixgroups.
open(WH3, ">me2grp.ldif"); #makes an ldif file for memberships.

$howMany +=$uidNum;
while () {
last if ($uidNum >= $howMany);
chomp;
/^(\w+),(\w+),(\w+),(\w+)/; #see the assumption at top.
my $ldifstring = "dn: uid=$1,ou=People,dc=techhelplist,dc=com\n".
"objectClass: top\n".
"objectClass: posixAccount\n".
"objectClass: shadowAccount\n".
"objectClass: account\n".
"cn: $2 $3\n".
"uid: $1\n". #ldap uid is the unix username.
"uidNumber: $uidNum\n". #ldap uidNumber is the unix uid.
"gidNumber: $uidNum\n". #ldap gidNumber is unix gid.
"homeDirectory: /home/$1\n".
"loginShell: /bin/bash\n".
"gecos: $2 $3, techhelplist employee,,\n".
"userPassword: {crypt}x\n".
"shadowLastChange: 0\n".
"shadowMax: 0\n".
"shadowWarning: 0\n";
print WH "$ldifstring\n\n"; #write ldif string to addme.ldif

#rewrite ldif string for group
$ldifstring = "dn: cn=$1,ou=Group,dc=techhelplist,dc=com\n".
"objectClass: top\n".
"objectClass: posixGroup\n".
"cn: $1\n".
"userPassword: {crypt}x\n".
"gidNumber: $uidNum\n\n";
print WH2 $ldifstring; #write ldif string to addgrp.ldif

#rewrite ldif string for ldapmodify to make memberships
$ldifstring = "dn: cn=$1,ou=Group,dc=techhelplist,dc=com\n".
"changetype: modify\n".
"add: memberuid\n".
"memberuid: $1\n\n";
print WH3 $ldifstring; #write ldif string to me2grp.ldif

$uidNum++; #increment uidNum for next around (unix uid)
}
close (WH2); #close all file handles
close (WH);
close (RH);
#------------------------------end make ldif file -------

#------------------------ run ldapadd on ldif file ------
#open(RH, "addme.ldif"); #open addme.ldif for reading

#run ldapadd for members
my $junk =`ldapadd -x -D \"cn=admin,dc=techhelplist,dc=com\"
-w secrepassword -c -F -f addme.ldif`;

#run ldapadd for groups
$junk = `ldapadd -x -D \"cn=admin,dc=techhelplist,dc=com\"
-w secretpassword -c -F -f addgrp.ldif`;


#run ldapmodify for membership
$junk = `ldapmodify -x -D \"cn=admin,dc=techhelplist,dc=com\"
-w secretpassword -c -F -f addgrp.ldif`;
#--------------------------end ldapadd on ldif file -----

Using Perl to delete users from OpenLDAP

Assuming that the input file is a comma-separated-values text file in the form:

username,firstname,lastname,secondarygroup

...then this script will create the LDIF files and delete them from OpenLDAP.

#!/usr/bin/perl -w
# 2008, copy all you want
# reads from a file and deletes entries from openLDAP.
#expects a 4 field comma seperated values text file.
use strict;

#if the csv file has more people than you want
#to delete, up to how many?
my $howMany = 11000;

#---------------------------------- make ldif file ----
open(RH, "sourcefile.csv");
open(WH, ">delme.ldif"); #creates the ldif file for writing.
my $i = 0;
while () {
last if ($i == $howMany);
chomp;
/^(\w+),(\w+),(\w+),(\w+)/; #perl regexp, see asumption above.

#puts both the user to delete
#and their default group (same name and gid=uid) in ldif file
print WH "uid=$1,ou=People,dc=techhelplist,dc=com\n";
print WH "cn=$1,ou=Group,dc=techhelplist,dc=com\n";

$i++;
}
close (WH); #close file handles
close (RH);
#------------------------------end make ldif file ------


#------------------------ run ldapdelete on ldif file ---
#open(RH, "delme.ldif");

system("ldapdelete -x -D \"cn=admin,dc=techhelplist,dc=com\"
-w secretpassword -c -f delme.ldif");

#--------------------------end ldapdelete on ldif file ---

 

 

If this article was at least a little helpful, how about a +1?