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


in reply to Re^2: Centos 7 getadsmtp.pl outputing blank
in thread Centos 7 getadsmtp.pl outputing blank

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?

Replies are listed 'Best First'.
Re^4: Centos 7 getadsmtp.pl outputing blank
by gml_perl (Initiate) on Jul 03, 2018 at 15:32 UTC

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

      I have marked up your code with the 2 lines you need to comment out and 3 lines to add

      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 t +he # address without any other characters... if ( $mail =~ s/^(smtp|SMTP)://gs ) { push(@valid, $mail." OK\n"); } } } # Only continue on LDAP_SUCCESS $mesg->code and last; # Get cookie from paged control my($resp) = $mesg->control( LDAP_CONTROL_PAGED ) or last; #$cookie = $resp->cookie or last; # delete $cookie = $resp->cookie; # add last if (!defined($cookie) || !length($cookie)); # add # Set cookie in paged control $page->cookie($cookie); } #if ($cookie) { # delete if (defined($cookie) && length($cookie)) { # add # We had an abnormal exit, so let the server know we do not want any + more $page->cookie($cookie); $page->size(0); $ldap->search( @args ); # Also would be a good idea to die unhappily and inform OP at this p +oint die("LDAP query unsuccessful"); }
      poj

        Thank you for the response and the assistance I am still facing challenges though I will paste the modified script as per your suggestion and the tcpdump logs from both the working server and the troublesome server. I can notice the differences in length value the failing one is zero size and the successful has some big value. It seems like its stopping at some point

        # Connecting to Active Directory domain controllers $noldapserver=0; $ldap = Net::LDAP->new($dc1) or $noldapserver=1; if ($noldapserver == 1) { $ldap = Net::LDAP->new($dc2) or die "Error connecting to specified domain controllers $@ \n"; } $mesg = $ldap->bind ( dn => $user, password =>$passwd); if ( $mesg->code()) { die ("error:", $mesg->code(),"\n"); } # How many LDAP query results to grab for each paged round # Set to under 1000 for Active Directory $page = Net::LDAP::Control::Paged->new( size => 990 ); @args = ( base => $hqbase, # Play around with this to grab objects such as Contacts, Public Folde +rs, etc. # A minimal filter for just users with email would be: # filter => "(&(sAMAccountName=*)(mail=*))" filter => "(& (mailnickname=*) (| (&(objectCategory=person) (objectClass=user)(!(homeMDB=*))(!(msExchHomeServe +rName=*))) (&(objectCategory=person)(objectClass=user)(|(home +MDB=*) (msExchHomeServerName=*)))(&(objectCategory=person +)(objectClass=contact)) (objectCategory=group)(objectCategory=publicFolder +) ))", control => [ $page ], attrs => "proxyAddresses", ); my $cookie; 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 t +he # address without any other characters... if ( $mail =~ s/^(smtp|SMTP)://gs ) { push(@valid, $mail." OK\n"); } } } # Only continue on LDAP_SUCCESS $mesg->code and last; # Get cookie from paged control my($resp) = $mesg->control( LDAP_CONTROL_PAGED ) or last; $cookie = $resp->cookie; last if (!defined($cookie) || !length($cookie)); # Set cookie in paged control $page->cookie($cookie); } if (defined($cookie) && length($cookie)) { # We had an abnormal exit, so let the server know we do not want any +more $page->cookie($cookie); $page->size(0); $ldap->search( @args ); # Also would be a good idea to die unhappily and inform OP at this p +oint die("LDAP query unsuccessful"); } # Only write the file once the query is successful open VALID, ">$VALID" or die "CANNOT OPEN $VALID $!"; print VALID @valid; # Add additional restrictions, users, etc. to the output file below. #print VALID "user\@example.com OK\n"; #print VALID "user1\@example.com 550 User unknown.\n"; #print VALID "bad.example.com 550 User does not exist.\n"; close VALID;

        TCP DUMPS LOGS

        WORKING SERVER:-

        root@mxgw:~# tcpdump -i eth0 port 389 tcpdump: verbose output suppressed, use -v or -vv for full protocol de +code listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes 16:34:36.355076 IP 192.168.135.9.59727 > srvleon.mailgw.com.ldap: Flag +s [S], seq 1470389482, win 5840, options [mss 1460,sackOK,TS val 1191 +504975 ecr 0,nop,wscale 6], length 0 16:34:36.355362 IP srvleon.mailgw.com.ldap > 192.168.135.9.59727: Flag +s [S.], seq 2510267230, ack 1470389483, win 8192, options [mss 1460,n +op,wscale 8,sackOK,TS val 476329174 ecr 1191504975], length 0 16:34:36.355385 IP 192.168.135.9.59727 > srvleon.mailgw.com.ldap: Flag +s [.], ack 1, win 92, options [nop,nop,TS val 1191504975 ecr 47632917 +4], length 0 16:34:36.356892 IP 192.168.135.9.59727 > srvleon.mailgw.com.ldap: Flag +s [P.], seq 1:58, ack 1, win 92, options [nop,nop,TS val 1191504976 e +cr 476329174], length 57 16:34:36.360987 IP srvleon.mailgw.com.ldap > 192.168.135.9.59727: Flag +s [P.], seq 1:23, ack 58, win 260, options [nop,nop,TS val 476329174 +ecr 1191504976], length 22 16:34:36.361087 IP 192.168.135.9.59727 > srvleon.mailgw.com.ldap: Flag +s [.], ack 23, win 92, options [nop,nop,TS val 1191504977 ecr 4763291 +74], length 0 16:34:36.361087 IP 192.168.135.9.59727 > srvleon.mailgw.com.ldap: Flag +s [.], ack 23, win 92, options [nop,nop,TS val 1191504977 ecr 4763291 +74], length 0 16:34:36.370483 IP 192.168.135.9.59727 > srvleon.mailgw.com.ldap: Flag +s [P.], seq 58:462, ack 23, win 92, options [nop,nop,TS val 119150497 +9 ecr 476329174], length 404 16:34:36.379843 IP srvleon.mailgw.com.ldap > 192.168.135.9.59727: Flag +s [.], seq 23:1471, ack 462, win 258, options [nop,nop,TS val 4763291 +75 ecr 1191504979], length 1448 16:34:36.379857 IP srvleon.mailgw.com.ldap > 192.168.135.9.59727: Flag +s [.], seq 1471:2919, ack 462, win 258, options [nop,nop,TS val 47632 +9175 ecr 1191504979], length 1448 16:34:36.379868 IP 192.168.135.9.59727 > srvleon.mailgw.com.ldap: Flag +s [.], ack 2919, win 182, options [nop,nop,TS val 1191504981 ecr 4763 +29175], length 0 16:34:36.380276 IP srvleon.mailgw.com.ldap > 192.168.135.9.59727: Flag +s [.], seq 2919:4367, ack 462, win 258, options [nop,nop,TS val 47632 +9177 ecr 1191504981], length 1448

        FAILING SERVER:-

        [root@vm1000 ~]# tcpdump -i ens32 port 389 tcpdump: verbose output suppressed, use -v or -vv for full protocol de +code listening on ens32, link-type EN10MB (Ethernet), capture size 262144 b +ytes 16:28:02.668058 IP pauline-mkt.mailgw.com.41948 > srvleon.mailgw.com.l +dap: Flags [S], seq 2414468076, win 29200, options [mss 1460,sackOK,T +S val 800294598 ecr 0,nop,wscale 7], length 0 16:28:02.668903 IP srvleon.mailgw.com.ldap > pauline-mkt.mailgw.com.41 +948: Flags [S.], seq 2723246614, ack 2414468077, win 8192, options [m +ss 1460,nop,wscale 8,sackOK,TS val 476285837 ecr 800294598], length 0 16:28:02.668943 IP pauline-mkt.mailgw.com.41948 > srvleon.mailgw.com.l +dap: Flags [.], ack 1, win 229, options [nop,nop,TS val 800294599 ecr + 476285837], length 0 16:28:02.669954 IP pauline-mkt.mailgw.com.41948 > srvleon.mailgw.com.l +dap: Flags [P.], seq 1:58, ack 1, win 229, options [nop,nop,TS val 80 +0294600 ecr 476285837], length 57 16:28:02.674297 IP srvleon.mailgw.com.ldap > pauline-mkt.mailgw.com.41 +948: Flags [P.], seq 1:23, ack 58, win 260, options [nop,nop,TS val 4 +76285837 ecr 800294600], length 22 16:28:02.674431 IP pauline-mkt.mailgw.com.41948 > srvleon.mailgw.com.l +dap: Flags [.], ack 23, win 229, options [nop,nop,TS val 800294605 ec +r 476285837], length 0

        2018-07-06 Athanasius added code tags