Script to convert HBA WWNs to lowercase and add ":"

I hope that SAN Admins find this helpful.

Whenever HBA WWNs are entered in Cisco SAN Switches on the command line prompt, A colon (:) character needs to be added at every second position in the WWN. Also, the WWN has to be lower case.

So in case you get a WWN like so - 10000000C9ABCDEF it needs to be converted to lower case and : needs to be added. The following script does this.

Here it adds ":" and lowercase. It also checks if the characters entered are Hex, if the length is correct and if any unwanted characters are given or not. In these cases, it takes you back to the prompt to enter the WWN.

It also does it the other way around, i.e. if you enter a WWN with ":", it removes them and does all the checks as mentioned above.

The Ever Helpful Monks at PerlMonks gave a lot of suggestions, and I've tried to incorporate them here.

#!/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 = <STDIN>);
if ($choice eq "a")
{
    until ( $raw_wwn eq "q")
        {
            print "Enter the wwn or q to quit: ";
            chomp ($raw_wwn=<STDIN>);
            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=<STDIN>);
        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 test output:

[linuxguy@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
[linuxguy@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
[linuxguy@joesatch practice]$ 

Leave a comment

About perl514

user-pic Perlpetually Indebted To PerlMonks.