in reply to IP address validation when IP is picked dynamically

Yes, toolic is right, chomp is needed to remove the new line character from the input line, but the other change proposed by vinoth.ree is also needed, because, right now, this line of your code:
next if $. < 2;
is skipping the two first lines, whereas your IP address sits on the second line, so that the is_ipv4 subroutine never gets a chance to see it. Changing it to:
next if $. <= 2;
or something equivalent would solve this issue.

Actually, rather than skipping lines, which is not very robust (your file might have a commented-out line at the top, who knows?), I would rather check if the extracted key matches IPADDR.

Je suis Charlie.

Replies are listed 'Best First'.
Re^2: IP address validation when IP is picked dynamically
by rahulruns (Scribe) on May 01, 2015 at 14:24 UTC

    Guys got this resolved finally. Spaces were a problem with IP address catch. Removed spaces from both ends (left and right)

    #!/usr/bin/perl use strict; use warnings; use Acme::Tools; use Data::Validate::IP qw(is_ipv4); print "Enter the absolute directory location for ifcfg files\n"; my $directory = <>; chomp($directory); opendir(DIR, $directory) or die "couldn't open $directory: $!\n"; my @ifcfg_files = grep { /^ifcfg-bond/ } readdir(DIR); closedir DIR; foreach (@ifcfg_files) { my %hash = (); my $file = "$_"; open (my $fh, "<", $file) or die "Can't open the file $file: "; while (my $line =<$fh>) { chomp($line); next if $. <= 2; # Added '=' my($key, $parameter_value) = split("=", $line); next if $key eq "BONDING_OPTS"; $parameter_value =~ s/^\s+|\s+$//g; if ( lc "$key" eq lc "ipaddr" ) { if (is_ipv4($parameter_value)) { print "Yes"; } } } } #Outer Foreach Result [root@localhost bond]# perl ifcfgbondverification.pl Enter the absolute directory location for ifcfg files /root/office/ifcfgverification/bond YesYesYesYesYesYesYes[root@localhost bond]#

      Be aware that the script as written will only work correctly if it's in the same directory as the files to be processed.

      When using opendir/readdir only the file names are being returned, not the full path. If the script is in some other directory, then your open call will fail because it lacks the required path to the file and will die at that point without ever parsing any files.

      You should either include $directory in the open call, or add it to $file prior to the open call, or use the glob function to retrieve the file list.

        Thank you for your pick of information I have made those changes (replying late, know that but Script has been enhanced further. Thank you all for your support. We can close this thread