Re: IP address validation when IP is picked dynamically
by toolic (Bishop) on May 01, 2015 at 12:29 UTC
|
while (my $line =<$fh>)
{
chomp $line;
next if $. < 2;
my($key, $parameter_value) = split("=", $line);
| [reply] [d/l] |
|
|
[root@localhost bond]# perl ifcfgbondverification.pl
Enter the absolute directory location for ifcfg files
/root/office/ifcfgverification/bond
FailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFa
+ilFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFai
+lFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFail
+FailFailFailFailFailFailFailFailFailFailFail[root@localhost bond]#
| [reply] [d/l] |
|
|
chomp is needed (if the IP address is the last text on the line after the split), but it may not be a complete solution to your problem. Consider the following code:
use warnings;
use strict;
use Data::Validate::IP qw(is_ipv4);
while (my $line = <DATA>) {
chomp $line;
my($key, $parameter_value) = split("=", $line);
if (is_ipv4($parameter_value))
{
print $parameter_value;
}
else {
print "Fail"
}
}
print "\n";
__DATA__
abc=1.2.3.4
It prints "1.2.3.4". If you comment out the chomp line, it prints "Fail".
Maybe $parameter_value isn't what you think it is. Tip #2 from the Basic debugging checklist: print
my($key, $parameter_value) = split("=", $line);
print ">>>$parameter_value<<<\n";
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
Re: IP address validation when IP is picked dynamically
by Laurent_R (Canon) on May 01, 2015 at 13:59 UTC
|
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.
| [reply] [d/l] [select] |
|
|
#!/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]#
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
Re: IP address validation when IP is picked dynamically
by vinoth.ree (Monsignor) on May 01, 2015 at 12:46 UTC
|
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...
| [reply] [d/l] |
|
|
[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]#
| [reply] [d/l] |
|
|
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? | [reply] [d/l] |
|
|
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.
| [reply] [d/l] [select] |