-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathmac
More file actions
executable file
·89 lines (81 loc) · 1.1 KB
/
mac
File metadata and controls
executable file
·89 lines (81 loc) · 1.1 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl
#
# output local mac addresses (all by default)
#
# -samy kamkar
#
# usage: mac [ip | iface | 'gw']
use Socket;
use Data::Dumper;
use strict;
my %arp = map {
my @s = split /[\s()]+/;
$s[1] => cleanmac($s[3])
} `arp -na`;
my $eth = lc shift;
if (!$eth || $eth eq 'gateway' || $eth eq 'gw')
{
foreach (`/usr/sbin/netstat -nr`)
{
if (/^default\s+(\S+)/)
{
if ($eth eq 'gw' || $eth eq 'gateway')
{
print "$arp{$1}\n";
exit;
}
else
{
print "gateway: $arp{$1}\n";
last;
}
}
}
}
# ip to mac
elsif ($arp{$eth})
{
print "$arp{$eth}\n";
exit;
}
# host to mac?
else
{
my $inet = inet_aton($eth);
if ($inet)
{
my $addr = inet_ntoa($inet);
if ($arp{$addr})
{
print "$arp{$addr}\n";
exit;
}
}
}
my $card;
foreach (`/sbin/ifconfig -a`)
{
if (/^(\w+):/)
{
$card = $1;
}
elsif (/ether (\S+)/)
{
my $mac = cleanmac($1);
if ($eth && lc $card eq $eth)
{
print "$mac\n";
}
elsif (!$eth)
{
print "$card: $mac\n";
}
}
}
sub cleanmac
{
my $mac = shift;
$mac = lc($mac);
$mac =~ s/(\w?)(\w)(:|$)/($1 || "0") . $2/eg;
return $mac;
}