in reply to IP address validation when IP is picked dynamically

Yeah you missed the chomp() in while loop as mentioned by toolic.More over did little bit changes in the script.

#!/usr/bin/perl use strict; use warnings; 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"; if (is_ipv4($parameter_value)) { print "Success -> $key:$parameter_value\n"; } else{ print "Failed -> $key:$parameter_value\n" } } } #Outer Foreach

All is well. I learn by answering your questions...

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

    Value of variable parameter_value is proper but it does not pass is_ipv4() function test

    [root@localhost bond]# perl ifcfgbondverification.pl Enter the absolute directory location for ifcfg files /root/office/ifcfgverification/bond Value of parameter is 172.20.1.1 Failed -> IPADDR:172.20.1.1 Value of parameter is 255.0.0.0 Failed -> NETMASK:255.0.0.0 Value of parameter is 172.30.12.25 Failed -> DNS1:172.30.12.25 Value of parameter is Failed -> MTU: Value of parameter is none Failed -> BOOTPROTO:none Value of parameter is yes Failed -> ONBOOT:yes Value of parameter is NO Failed -> USERCTL:NO Value of parameter is no Failed -> NM_CONTROLLED:no Value of parameter is 172.26.1.1 Failed -> IPADDR:172.26.1.1 Value of parameter is 255.0.0.0 Failed -> NETMASK:255.0.0.0 Value of parameter is 172.30.12.25 Failed -> DNS1:172.30.12.25 Value of parameter is Failed -> MTU: Value of parameter is none Failed -> BOOTPROTO:none Value of parameter is yes Failed -> ONBOOT:yes Value of parameter is NO Failed -> USERCTL:NO Value of parameter is no Failed -> NM_CONTROLLED:no Value of parameter is 172.28.1.1 Failed -> IPADDR:172.28.1.1 Value of parameter is 255.0.0.0 Failed -> NETMASK:255.0.0.0 Value of parameter is 172.30.12.25 Failed -> DNS1:172.30.12.25 Value of parameter is Failed -> MTU: Value of parameter is none Failed -> BOOTPROTO:none Value of parameter is yes Failed -> ONBOOT:yes Value of parameter is NO Failed -> USERCTL:NO Value of parameter is no Failed -> NM_CONTROLLED:no Value of parameter is 172.30.1.1 Failed -> IPADDR:172.30.1.1 Value of parameter is 255.0.0.0 Failed -> NETMASK:255.0.0.0 Value of parameter is 172.30.12.25 Failed -> DNS1:172.30.12.25 Value of parameter is Failed -> MTU: Value of parameter is none Failed -> BOOTPROTO:none Value of parameter is yes Failed -> ONBOOT:yes Value of parameter is NO Failed -> USERCTL:NO Value of parameter is no Failed -> NM_CONTROLLED:no Value of parameter is 172.27.1.1 Failed -> IPADDR:172.27.1.1 Value of parameter is 255.0.0.0 Failed -> NETMASK:255.0.0.0 Value of parameter is 172.30.12.25 Failed -> DNS1:172.30.12.25 Value of parameter is Failed -> MTU: Value of parameter is none Failed -> BOOTPROTO:none Value of parameter is yes Failed -> ONBOOT:yes Value of parameter is NO Failed -> USERCTL:NO Value of parameter is no Failed -> NM_CONTROLLED:no Value of parameter is 172.21.1.1 Failed -> IPADDR:172.21.1.1 Value of parameter is 255.0.0.0 Failed -> NETMASK:255.0.0.0 Value of parameter is 172.30.12.25 Failed -> DNS1:172.30.12.25 Value of parameter is Failed -> MTU: Value of parameter is none Failed -> BOOTPROTO:none Value of parameter is yes Failed -> ONBOOT:yes Value of parameter is NO Failed -> USERCTL:NO Value of parameter is no Failed -> NM_CONTROLLED:no Value of parameter is 172.31.1.1 Failed -> IPADDR:172.31.1.1 Value of parameter is 255.0.0.0 Failed -> NETMASK:255.0.0.0 Value of parameter is 172.30.12.25 Failed -> DNS1:172.30.12.25 Value of parameter is Failed -> MTU: Value of parameter is none Failed -> BOOTPROTO:none Value of parameter is yes Failed -> ONBOOT:yes Value of parameter is NO Failed -> USERCTL:NO Value of parameter is no Failed -> NM_CONTROLLED:no [root@localhost bond]#
      That's not sufficient. There is probably whitespace at the end of $parameter_value:
      print "Failed -> $key:$parameter_value<<<\n"

      Please read the Basic debugging checklist.

      Print a full $line and post it.

      Are you using the latest version of Data::Validate::IP from CPAN?

      Could you show the revised code producing this result?

      And also use the printing debugging trick suggested by toolic ad the end of his post Re^3: IP address validation when IP is picked dynamically, i.e.:

      my($key, $parameter_value) = split("=", $line); print ">>>$parameter_value<<<\n";
      because this will tell you, for example, if there is a trailing new line or space character at the end of your $parameter_value variable.

      Update: Ooops, too late, toolic was much faster.

      Je suis Charlie.