Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert MAC Address formats in Perl?

I have to make a Perl script that gets a MAC address in the format HHHH.HHHH.HHHH where "H" is a hex digit, and give me a output of HH:HH:HH:HH:HH:HH. How can I make this conversion in Perl?

Here’s an input text example:

System Information
Local port          :xgei-1/6/1
Group MAC address   :Nearest Bridge
Neighbor index      :1
Chassis type        :MAC address
Chassis ID          :4cf5.5b8b.f860
Port ID type        :Interface name
Port ID             :XGigabitEthernet0/0/1
Time to live        :109
Port description    :ZTE-2-C650-172.24.102.77
System name         :main-link-lab-cdi-sw-01

And here’s my script’s snippet where I treat the MAC Address data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if ($linha =~m/^Chassis ID/){
            my($chassisID) = $linha=~ /:(.*)/g;
            $lldpInfo{$localInt}{"chassisID"} = $chassisID;
            print $chassisID."\n";  
}

In this case, I have to process the variable $chassisID.

Any suggestions?

Thanks!

>Solution :

A quick and dirty approach could be to split the string on ., map the entries (assumed to be 4 characters each) into 2 character snippets separated by :, and then rejoin the results with : separators

my $chassisID = '4cf5.5b8b.f860';
# Transform the format:
$chassisID = join(':', map { substr($_, 0, 2).':'.substr($_, 2, 2) } split('\.', $chassisID));
print "ChassisID in MAC address format: $chassisID\n";

Output:

ChassisID in MAC address format: 4c:f5:5b:8b:f8:60
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading