in reply to Re: IP address validation when IP is picked dynamically
in thread IP address validation when IP is picked dynamically

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]#

Replies are listed 'Best First'.
Re^3: IP address validation when IP is picked dynamically
by fishmonger (Chaplain) on May 01, 2015 at 16:49 UTC

    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