-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacfmt
executable file
·209 lines (196 loc) · 7.35 KB
/
macfmt
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#! /usr/bin/awk -f
# On POSIX systems, the first line of the script is:
# #! /usr/bin/awk -f
# On Solaris (tested on version 10), the first line of the script is:
# #! /usr/xpg4/bin/awk -f
# Convert MAC addresses between different formats.
#
# Copyright (C) 2012-2022 Erik Auerswald <[email protected]>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
#
# Usage: macfmt [-v cap=CASE] [-v format=FORMAT]
#
# Reads MAC addresses (one per line) from STDIN or file given as argument.
# Prints all formats unless one is specified using the -v format=FORMAT option.
# FORMAT is one of: all, cisco, linux, enterasys, cabletron, broadcom,
# vmps, netsight, hp, hex, hexprefix, pxe, pgsql,
# hexpostfix, exos, arista, mikrotik, huawei, ieee,
# ietf, dell, ftos, dellos9, macos, apple_ios, android,
# chromeos, ps3, ps4, ps5, switch, wii, xbox, xbox360,
# windows, windows_arp
# Converts hex digits A-F to lower or upper case if -v cap=CASE is given.
# CASE is one of: lower, upper
# If cap is not specified, the format's default case is used.
#
# Examples: echo 01:23:45:67:89:ab | macfmt
# echo 01-23-45-67-89-AB | macfmt -v format=linux
# echo 0123:4567:89AB | macfmt -v format=cisco
# macfmt -v format=linux -v cap=upper File_with_MAC_addresses
#
# Needs a POSIX compatible AWK, e.g. /usr/xpg4/bin/awk on Solaris, but
# usually /usr/bin/awk. Tested on Solaris 10 and with GAWK on GNU/Linux.
# Does not work with MAWK version 1.3.3-17 on Debian GNU/Linux.
#
# Version 2022-06-18-02
function print_macs(format) {
cisco = tolower(a[1] a[2] "." a[3] a[4] "." a[5] a[6])
linux = tolower(a[1] ":" a[2] ":" a[3] ":" a[4] ":" a[5] ":" a[6])
ets = toupper(a[1] "-" a[2] "-" a[3] "-" a[4] "-" a[5] "-" a[6])
ct = toupper(a[1] ":" a[2] ":" a[3] ":" a[4] ":" a[5] ":" a[6])
bcm = toupper(a[1] a[2] ":" a[3] a[4] ":" a[5] a[6])
vmps = toupper(a[1] a[2] "." a[3] a[4] "." a[5] a[6])
netsight = toupper(a[1] "." a[2] "." a[3] "." a[4] "." a[5] "." a[6])
hp = tolower(a[1] a[2] a[3] "-" a[4] a[5] a[6])
hex = tolower(a[1] a[2] a[3] a[4] a[5] a[6])
pxe = toupper(a[1] " " a[2] " " a[3] " " a[4] " " a[5] " " a[6])
pgsql = tolower(a[1] a[2] a[3] ":" a[4] a[5] a[6])
hexpostfix = toupper(hex) "h"
huawei = tolower(a[1] a[2] "-" a[3] a[4] "-" a[5] a[6])
wii = tolower(a[1] "-" a[2] "-" a[3] "-" a[4] "-" a[5] "-" a[6])
xbox360 = toupper(a[1] a[2] a[3] a[4] a[5] a[6])
if (cap == "lower") {
ets = tolower(ets)
ct = tolower(ct)
bcm = tolower(bcm)
vmps = tolower(vmps)
netsight = tolower(netsight)
pxe = tolower(pxe)
hexpostfix = tolower(hexpostfix)
} else if (cap == "upper") {
cisco = toupper(cisco)
linux = toupper(linux)
hp = toupper(hp)
hex = toupper(hex)
pgsql = toupper(pgsql)
huawei = toupper(huawei)
}
hexprefix = "0x" hex
if (!format || format == "all") {
print cisco, linux, ets, ct, bcm, vmps, netsight, hp, hex, \
hexprefix, pxe, pgsql, hexpostfix, huawei, wii, xbox360
} else if (format == "cisco" || format == "arista") {
print cisco
} else if (format == "linux" || format == "exos" || format == "ietf" ||
format == "ftos" || format == "dellos9" ||
format == "macos" || format == "ps4" ||
format == "ps5") {
print linux
} else if (format == "enterasys" || format == "ieee" ||
format == "windows") {
print ets
} else if (format == "cabletron" || format == "mikrotik" ||
format == "apple_ios" || format == "android" ||
format == "chromeos" || format == "ps3" || format == "switch" ||
format == "xbox") {
print ct
} else if (format == "broadcom") {
print bcm
} else if (format == "vmps" || format == "dell") {
print vmps
} else if (format == "netsight") {
print netsight
} else if (format == "hp") {
print hp
} else if (format == "hex") {
print hex
} else if (format == "hexprefix") {
print hexprefix
} else if (format == "pxe") {
print pxe
} else if (format == "pgsql") {
print pgsql
} else if (format == "hexpostfix") {
print hexpostfix
} else if (format == "huawei") {
print huawei
} else if (format == "wii" || format == "windows_arp") {
print wii
} else if (format == "xbox360") {
print xbox360
}
}
BEGIN {
format = tolower(format)
cap = tolower(cap)
}
/^[[:space:]]*[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]][[:space:]]*$/ {
gsub("[[:space:]]*","")
split($0, a, ":")
print_macs(format)
}
/^[[:space:]]*[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]][[:space:]]*$/ {
gsub("[[:space:]]*","")
split($0, a, "-")
print_macs(format)
}
/^[[:space:]]*[[:xdigit:]][[:xdigit:]]\.[[:xdigit:]][[:xdigit:]]\.[[:xdigit:]][[:xdigit:]]\.[[:xdigit:]][[:xdigit:]]\.[[:xdigit:]][[:xdigit:]]\.[[:xdigit:]][[:xdigit:]][[:space:]]*$/ {
gsub("[[:space:]]*","")
split($0, a, ".")
print_macs(format)
}
/^[[:space:]]*[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]\.[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]\.[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:space:]]*$/ {
gsub("[[:space:]]*","")
split($0, b, ".")
a[1] = substr(b[1],1,2)
a[2] = substr(b[1],3,2)
a[3] = substr(b[2],1,2)
a[4] = substr(b[2],3,2)
a[5] = substr(b[3],1,2)
a[6] = substr(b[3],3,2)
print_macs(format)
}
/^[[:space:]]*[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:space:]]*$/ {
gsub("[[:space:]]*","")
split($0, b, ":")
a[1] = substr(b[1],1,2)
a[2] = substr(b[1],3,2)
a[3] = substr(b[2],1,2)
a[4] = substr(b[2],3,2)
a[5] = substr(b[3],1,2)
a[6] = substr(b[3],3,2)
print_macs(format)
}
/^[[:space:]]*[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:space:]]*$/ {
gsub("[[:space:]]*","")
split($0, b, "-")
a[1] = substr(b[1],1,2)
a[2] = substr(b[1],3,2)
a[3] = substr(b[1],5,2)
a[4] = substr(b[2],1,2)
a[5] = substr(b[2],3,2)
a[6] = substr(b[2],5,2)
print_macs(format)
}
/^[[:space:]]*(0x)?[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]](h)?[[:space:]]*$/ {
gsub("[[:space:]]*","")
gsub("^0x","")
gsub("h$", "")
a[1] = substr($0,1,2)
a[2] = substr($0,3,2)
a[3] = substr($0,5,2)
a[4] = substr($0,7,2)
a[5] = substr($0,9,2)
a[6] = substr($0,11,2)
print_macs(format)
}
/^[[:space:]]*[[:xdigit:]][[:xdigit:]] [[:xdigit:]][[:xdigit:]] [[:xdigit:]][[:xdigit:]] [[:xdigit:]][[:xdigit:]] [[:xdigit:]][[:xdigit:]] [[:xdigit:]][[:xdigit:]][[:space:]]*$/ {
gsub("^[[:space:]]*","")
gsub("[[:space:]]*$","")
split($0, a, " ")
print_macs(format)
}
/^[[:space:]]*[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:space:]]*$/ {
gsub("[[:space:]]*","")
split($0, b, ":")
a[1] = substr(b[1],1,2)
a[2] = substr(b[1],3,2)
a[3] = substr(b[1],5,2)
a[4] = substr(b[2],1,2)
a[5] = substr(b[2],3,2)
a[6] = substr(b[2],5,2)
print_macs(format)
}