Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Sync passwd and AD users full names

by TechFly (Scribe)
on Jul 28, 2010 at 16:03 UTC ( #851743=CUFP: print w/ replies, xml ) Need Help??

I have been tasked with cleaning up the AIX boxes. One of the things I wanted to do was add the users full names to the passwd file so that I can figure out who users are fairly easily. I did not want to do this manually, so here is a script I came up with to do it for me. It takes the passwd in the same folder as the script, and creates a passwd.new file that you can then replace the old passwd with.

#!/usr/bin/perl -w =head1 Author: TechFly Name: SyncUserNames.pl Description: Add the uses names from AD into the passwd file. The scr +ipt will read the passwd file in the same directory, and then connect + to AD. It will use the usernames in the passwd file to look up the +users full name in AD, then populate that in a passwd.new file. It a +lso creates a nousername file that is populated with the users not fo +und in AD. Start date: 7-23-2010 Last updated Date: 7-23-2010 =cut use strict; use warnings; use Net::LDAP; #for subroutine getADInfo my $ldap; my $mesg; my $entry; my @entries; my $username; my $userfullname = ""; #for main routine my $fileinname = "./passwd"; my $filein; my $fileoutname = "./passwd.new"; my $fonousername; my $fonousernamename = "./nousername"; my $userline; my @users; my $fileout; my $userlineout; open($filein, "<", $fileinname) || die $!; open($fileout, ">>", $fileoutname) || die $!; open($fonousername, ">>", $fonousernamename) || die $!; while($userline = <$filein>){ @users = split /:/, $userline; $username = $users[0]; getADInfo(); print $fonousername ("$username\n") if $userfullname eq ""; print($username." - ".$userfullname."\n"); $users[4] = "$userfullname"; chomp($userlineout = join ":", @users); print $fileout ("$userlineout\n"); $userfullname = ""; } close $filein; close $fileout; sub getADInfo{ $ldap = Net::LDAP->new("mcd-dct-2.hcmc.co.hennepin.mn.us", onerror=>'w +arn'); $mesg = $ldap->bind( "cn=some,cn=domain,cn=com", password => "somepass +word" ); $mesg = $ldap->search( base => "cn=some,cn=domain,cn=com", filter => "sAMAccountName=$username", scope => 'sub', sizelimit => '0', attrs => ['name'] ) || die $!; @entries = $mesg->entries; foreach $entry(@entries){ #print($entry->get_value('name')."\n"); $userfullname = $entry->get_value('name'); }}

Comment on Sync passwd and AD users full names
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://851743]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-09-17 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (92 votes), past polls