http://www.perlmonks.org?node_id=1216546


in reply to Centos 7 getadsmtp.pl outputing blank

I skimmed your code, and nothing obvious jumps out at me (the style of the code could be improved by adding strict, but it doesn't look like that's likely to be the source of the problem). Have you verified that you're using the same version of Net::LDAP on all these machines? (/path/to/perl -MNet::LDAP -le 'print $Net::LDAP::VERSION') Are you using exactly the same code on all the machines, or have you modified some of the variables? If so, that's another place I'd look.

If you could provide a more detailed description of the problem - for example, is the file named in $VALID created, or not, and if it is created, are you saying that it is empty? You can also add debugging statements inside the loops that are fetching the data to see what the LDAP server is returning (before the code filters it with the regex). Often a simple print is enough, or use Data::Dumper with $Data::Dumper::Useqq=1; to see hidden whitespace and other control characters.

Update: Also, how are you running the script, from the command line? Because otherwise (e.g. if you're running it from some kind of task scheduler like cron), the output (error messages) might get swallowed there.

Replies are listed 'Best First'.
Re^2: Centos 7 getadsmtp.pl outputing blank
by gml_perl (Initiate) on Jun 26, 2018 at 11:52 UTC

    Perl version for the successful script is 0.4001 and where it is failing 0.65. I am using the same code on both servers and files named $valid are all created but empty. I am running the script from the command line there is no need for a cron we just run it manually each time we create new AD users. I am not so sure how to include Data::Dumper or print statements into the code.

      I am not so sure how to include Data::Dumper or print statements into the code.

      So I take it you're not a Perl programmer? I would suggest you read perlintro, it's not very long and it'll give you a good idea of Perl's syntax. Then, you can insert use Data::Dumper; above the line use Net::LDAP;, and at various places in the code, insert print Dumper($variable); or print Dumper(\@array); statements to get debug output, which you can compare across the two machines, or post here for feedback, if you like.

      Just for example, you might insert print Dumper($mail); inside the loop foreach my $mail ( $entry->get_value( "proxyAddresses" ) ) { ... } to see which, if any, mail addresses you are getting and whether they match the pattern /^(smtp|SMTP):/. You can repeat this for the other loops to see how many times those loops get executed, etc.

      Perl version for the successful script is 0.4001 and where it is failing 0.65.

      Have you had a chance to investigate poj's suggestion?

        My apologies I am not a perl programmer I come from the Linux side. 1. I now have the code as :-

        use Data::Dumper; use Net::LDAP; use Net::LDAP::Control::Paged; use Net::LDAP::Constant ( "LDAP_CONTROL_PAGED" );
        . . .
        while(1) { # Perform search my $mesg = $ldap->search( @args ); # Filtering results for proxyAddresses attributes foreach my $entry ( $mesg->entries ) { my $name = $entry->get_value( "cn" ); # LDAP Attributes are multi-valued, so we have to print each one. foreach my $mail ( $entry->get_value( "proxyAddresses" ) ) { # Test if the Line starts with one of the following lines: # proxyAddresses: [smtp|SMTP]: # and also discard this starting string, so that $mail is only th +e # address without any other characters... if ( $mail =~ s/^(smtp|SMTP)://gs ) { push(@valid, $mail." OK\n"); print Dumper($mail); } }
        I do not get any output

        Using Poj's Suggesstion:- I get this error message:- root@vm1000 getadscripts# ./getadsmtp_itbak1.pl Name "main::valid" used only once: possible typo at ./getadsmtp_itbak1.pl line 116, <DATA> line 755. $VAR1 = '/etc/postfix/ad_itdepartment';