-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter_mac_add_hostname
executable file
·56 lines (46 loc) · 1.07 KB
/
filter_mac_add_hostname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl
use warnings;
use strict;
use autodie;
use Data::Dump qw(dump);
$|=1; # flush stdout
my $debug = $ENV{DEBUG} || 0;
use lib 'lib';
use name2mac;
my $mac2name = $name2mac::mac2name;
sub mac2name {
my ( $mac, $name ) = @_;
$mac = lc($mac);
if ( exists $mac2name->{$mac} ) {
my $mac_name = $mac2name->{$mac};
warn "ERROR: name different $name != $mac_name" if $name && $name ne $mac_name;
#return ( $mac, $mac_name );
return $mac . ' ' . $mac_name;
}
#return ( $mac, $name );
return $mac;
}
warn "# mac2name = ",dump($mac2name) if $debug;
sub fix_mac {
my $mac = shift;
$mac = lc($mac);
$mac =~ s/^([0-9a-f]):/0$1:/;
while ( $mac =~ s/:([0-9a-f]):/:0$1:/g ) {};
$mac =~ s/:([0-9a-f])$/:0$1/;
# warn "# $mac\n";
return $mac;
}
# 744D.283D.3733
sub mac4 {
my $mac = shift;
warn "XXX mac4 ",dump($mac);
$mac =~ s{([0-9A-F]{2})([0-9A-F]{2})}{$1:$2}g;
$mac =~ s{\.}{:}g;
return lc($mac);
}
while(<>) {
# 744D.283D.3733
s/([0-9A-F]{4}\.[0-9A-F]{4}\.[0-9A-F]{4})/mac4($1)/ge;
s/((:?[0-9A-Fa-f]{2}[:-]){5}(:?[0-9A-Fa-f]{2}))/mac2name($1)/ge;
print;
}