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'); }}

In reply to Sync passwd and AD users full names by TechFly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.