#!/usr/bin/perl -w =head1 Author: TechFly Name: SyncUserNames.pl Description: Add the uses names from AD into the passwd file. The script 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 also creates a nousername file that is populated with the users not found 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=>'warn'); $mesg = $ldap->bind( "cn=some,cn=domain,cn=com", password => "somepassword" ); $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'); }}