Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumerate AD 3

Status
Not open for further replies.

rbri

Programmer
Jun 27, 2002
84
US
Hello Everyone

I wrote a Perl script to gather NIS and Active directory info. The issue I am having is the only way I have been able to enumerate AD is with this snippet. The $sNisInfo contains the "userID:GrpName:UserRealName:" from NIS.

$result = $ldap->search(
base => "dc=mycompany,dc=com",
filter => "sAMAccountName=$sZid",
);
# die $result->error if $result->code;
foreach my $entry ($result->entries) {
printf OUTPUT "$sNisInfo:%s:%s:%s:%s\n",
$entry->get_value("displayName"),
$entry->get_value("telephoneNumber"),
$entry->get_value("l"),
$entry->get_value("mail");
}

I get an error whenever a user ID in AD does not have the info I am searching for filled out I get "Use of uninitialized value in printf". How do I get printf to just leave the value blank if it is empty and not error out. Thanks in advance for any help.

Randy

 
Check it first

Perl:
if ($str eq "")
{
  // ...
}


Perl:
if ($str ne "")
{
  // ...
}

EDIT:
Probably best had I posted the right code first!


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thank you Chris for your help. I am not sure where to put the "if" statement I tried putting it around all the code in the "foreach" loop but I get the same results without the "if" statement as I originally got. I then tried to put the "if" statement around the "printf" statement but then I get "Use of uninitialized value in printf" on all the results no data is returned at all.

Randy
 
It depends which you are going to use.

A: if ... eq "" is to check that the variable has no value so is eqal to an empty string.


B: if ... ne "" is to check that the variable has a value so is not equal to an empty string

and the statement you want to run if the comparison returns 'true' goes where the // ... is in the example


if ($str ne "")
{
print "Is NOT an empty string."
}

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Chris, the test [tt]if($str ne ""){}[/tt] checks if [tt]$str[/tt] is an empty string, but NOT if [tt]$str[/tt] is [tt]undef[/tt] (uninitialized). That's why Randy continues to see the error. A better test would be [tt]if($str){}[/tt], though this one is also not perfect, as a value [tt]$str="0"[/tt] returns false. Possibly the best one is [tt]if(length$str){}[/tt].
Randy, you could say [tt]no strict[/tt] just before the [tt]if[/tt] statement around the [tt]printf[/tt] statement and restore [tt]use strict[/tt] after it (if you want to take the risk), or say
Code:
$displayName=$entry->get_value("displayName");
$displayName=""unless length$displayName;

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
You could always use ternary to evaluate the value

Code:
printf OUTPUT "$sNisInfo:%s:%s:%s:%s\n",
((defined $entry->get_value("displayName"))?$entry->get_value("displayName"):""),
((defined $entry->get_value("telephoneNumber"))?$entry->get_value("telephoneNumber"):""),
((defined $entry->get_value("l"))?$entry->get_value("l"):""),
((defined $entry->get_value("mail"))?$entry->get_value("mail"):"");

Perl IMO is a bit ugly when trying to handle undefined values.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
It's working thank you both for your help. Thanks again.

Randy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top