This repository has been archived by the owner on Sep 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm-mac2ipv4.pl
executable file
·80 lines (61 loc) · 1.9 KB
/
mm-mac2ipv4.pl
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
#!/usr/bin/perl
#
# © 2016 Meta Mesh Wireless Communities. All rights reserved.
# Licensed under the terms of the MIT license.
#
# AUTHORS
# * Jason Khanlar
#
use strict;
use warnings;
sub usage {
print <<USAGE;
Usage: ./mm-mac2ipv4.pl <MAC address>
Usage: ./mm-mac2ipv4.pl --list-all
Examples:
./mm-mac2ipv4.pl DC:9F:DB:CE:13:57
./mm-mac2ipv4.pl DC-9F-DB-CE-13-57
./mm-mac2ipv4.pl DC 9F DB CE 13 57
./mm-mac2ipv4.pl dc 9f db ce 13 57
USAGE
exit 1;
}
sub list_all {
my @mac = (0xDC, 0x9F, 0xDB, 0x00, 0x00, 0x00);
my @ip = (100, 64, 0, 0);
for my $i (0 .. 255) {
$mac[3] = $i;
# Format IP address
my $ip = sprintf('%d.%d.%d.%d', $ip[0], $i % 32 + 96, $ip[2], $ip[3]);
# Format MAC address
my $mac = sprintf('%02X:%02X:%02X:%02X:%02X:%02X', @mac);
## Output matching IP address and MAC address
printf("%-15s => %s\n", $ip, $mac);
}
exit;
}
# Check for --list-all, otherwise proceed
if (@ARGV == 1 and $ARGV[0] =~ m/^-/) {
if ($ARGV[0] eq '--list-all') { list_all(); }
else { usage(); }
}
# Proceed if not --list-all
# # of arguments should be 1 or 6
# 1 -> DC:9F:DB:CE:13:57 -or- DC-9F-DB-CE-13-57
# 6 -> DC 9F DB CE 13 57
if (@ARGV == 1) {
# Split 1 argument into 6 separate arguments, 1 for each octet
@ARGV = split('[:-]', $ARGV[0]);
}
if (@ARGV != 6) { usage(); }
my @mac = @ARGV;
$_ = uc for @mac;
# Ensure that we are working with the correct large MAC address block
# DC-9F-DB
if ($mac[0] ne "DC" || $mac[1] ne "9F" || $mac[2] ne "DB") {
print "Unsupported MAC address. Only Ubiquiti-assigned MAC addresses beginning with DC:9F:DB are supported.\n";
exit 1;
}
# Convert last three hexadecimal octets to decimal values
my @ip = (100, hex($mac[3]) % 32 + 96, hex($mac[4]), hex($mac[5]) - (hex($mac[5]) % 64 - hex($mac[5]) % 32));
print "$ip[0].$ip[1].$ip[2].$ip[3]\n";