如何在Perl中转换MAC地址格式?
我必须制作一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 netadaddr :: mac 来自CPAN到分析的模块到分析和格式Mac地址方法:
You can use the NetAddr::MAC module from CPAN to parse and format MAC addresses in many different ways:
一种快速而肮脏的方法可能是将字符串分开
,将条目(假定为每个字符为4个字符)映射到由
分隔的2个字符段:
,然后重新加入带有的结果:
分离器输出:
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:
separatorsOutput:
如果无法省略领先零,则可以使用以下较短的解决方案:
If leading zeroes can't be omitted, you can use the following shorter solutions: