Saturday, January 21, 2012

Perl Script to remove the ":" or add them in HBA WWNs.

I've been getting sick and tired of having to either remove or add the ":" in the HBA WWNs.

When these need to be entered in the Cisco CLI, one has to add ":" after every two HBA WWN characters. If copied from the CLI and to be put in symcli, one has to remove them...so...I wrote a Perl script that takes care of that. The awesome guys at Perlmonks helped a lot :).

Here's the script:

#!/usr/bin/perl
use warnings;
use strict;

print "Enter
a for lowercase and colons.
b to do it the other way around.
q to quit ";
my $choice = 0;
my $raw_wwn = 0;
my $ripe_wwn = 0;
my @array_wwn;
my @ripe_wwn;

chomp ($choice = );
if ($choice eq "a")
{
until ( $raw_wwn eq "q")
{
print "Enter the wwn or q to quit: ";
chomp ($raw_wwn=);
if (length($raw_wwn)!=16 || $raw_wwn =~/[^0-9a-fA-F]/ )
{
print "Invalid Length Or Incorrect Format\n";


}

else
{
my @array_wwn = unpack ("(a2)*", lc($raw_wwn));
@ripe_wwn = join (":", @array_wwn);
print "@ripe_wwn\n";
}
}
}

elsif ($choice eq "b")
{ $raw_wwn =0;
until ($raw_wwn eq "q")
{

print "Enter the wwn with : or q to quit: ";
chomp ($raw_wwn=);
if (length($raw_wwn)!=23||$raw_wwn=~/[^:a-fA-F0-9]/)
{
print "Invalid Length Or Incorrect Format\n";
}
else
{
$raw_wwn=~ s/://g;
$ripe_wwn = $raw_wwn;
print lc($ripe_wwn), "\n";
}
}
}#closing for if choice =b
elsif ($choice eq "q")
{
exit;
}


And here is some sample output:

And here is some test output:

[perlpetual@joesatch practice]$ perl wwn_final.pl
Enter
a for lowercase and colons.
b to do it the other way around.
q to quit a
Enter the wwn or q to quit: 10000000C9ABCDEF
10:00:00:00:c9:ab:cd:ef
Enter the wwn or q to quit: 10:00:00:00:c9:ab:cd:ef
Invalid Length Or Incorrect Format
Enter the wwn or q to quit: 10000000C9ABCDEG
Invalid Length Or Incorrect Format
Enter the wwn or q to quit: 10000000abcdefga
Invalid Length Or Incorrect Format
Enter the wwn or q to quit: 10000000abcdefab
10:00:00:00:ab:cd:ef:ab
Enter the wwn or q to quit: q
Invalid Length Or Incorrect Format
[perlpetual@joesatch practice]$ perl wwn_final.pl
Enter
a for lowercase and colons.
b to do it the other way around.
q to quit b
Enter the wwn with : or q to quit: 10:00:00:00:c9:ab:cd:ef
10000000c9abcdef
Enter the wwn with : or q to quit: 10:00:00:00:c9:ab:cd:eg
Invalid Length Or Incorrect Format
Enter the wwn with : or q to quit: 10:00:00:00:c9:ab:cd:e
Invalid Length Or Incorrect Format
Enter the wwn with : or q to quit: 10000000c9abcdef
Invalid Length Or Incorrect Format
Enter the wwn with : or q to quit: q
Invalid Length Or Incorrect Format
[perlpetual@joesatch practice]$