如何在Perl中转换MAC地址格式?

发布于 2025-02-13 02:00:53 字数 821 浏览 1 评论 0原文

我必须制作一个perl脚本,该脚本以hhh.hhh.hh.hhh的格式获得Mac地址,其中“ H”是一个十六进制数字,并给我输出HH:HH:HH:HH:HH:HH:HH:HH:HH:HH:HH:HH。如何在Perl中进行此转换?

这是一个输入文本示例:

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

这是我的脚本片段,在这里我处理MAC地址数据:

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

在这种情况下,我必须处理变量$ chassisid

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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混浊又暗下来 2025-02-20 02:00:53

您可以使用 netadaddr :: mac 来自CPAN到分析的模块到分析和格式Mac地址方法:

#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;
use NetAddr::MAC;

my $macaddr = '4cf5.5b8b.f860';
my $mac = NetAddr::MAC->new($macaddr);
say $mac->as_ieee; # 4c:f5:5b:8b:f8:60

You can use the NetAddr::MAC module from CPAN to parse and format MAC addresses in many different ways:

#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;
use NetAddr::MAC;

my $macaddr = '4cf5.5b8b.f860';
my $mac = NetAddr::MAC->new($macaddr);
say $mac->as_ieee; # 4c:f5:5b:8b:f8:60
英雄似剑 2025-02-20 02:00:53

一种快速而肮脏的方法可能是将字符串分开,将条目(假定为每个字符为4个字符)映射到由分隔的2个字符段:,然后重新加入带有的结果:分离器

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";

输出:

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

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
同展鸳鸯锦 2025-02-20 02:00:53
my $out = join ":", unpack "(H2)*", pack "n*", map hex, split /\./, $in;

如果无法省略领先零,则可以使用以下较短的解决方案:

my $out = $in =~ s/\.//rg =~ s/..\K/:/rg;
s/\.//g, s/..\K/:/g for $s;                  # In-place
my $out = join ":", unpack "(H2)*", pack "n*", map hex, split /\./, $in;

If leading zeroes can't be omitted, you can use the following shorter solutions:

my $out = $in =~ s/\.//rg =~ s/..\K/:/rg;
s/\.//g, s/..\K/:/g for $s;                  # In-place
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文