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.bash
executable file
·113 lines (91 loc) · 2.16 KB
/
mm-mac2ipv4.bash
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
#!/bin/bash
#
# © 2016 Meta Mesh Wireless Communities. All rights reserved.
# Licensed under the terms of the MIT license.
#
# AUTHORS
# * Jason Khanlar
#
function normalize-ipv4-octet-2 {
((ip2=$1 % 32 + 96))
}
function normalize-ipv4-octet-4 {
((ip4=$1 - ($1 % 64 - $1 % 32)))
}
function list-all {
mac1=DC
mac2=9F
mac3=DB
mac4=00
mac5=00
mac6=00
ip1=100
ip2=64
ip3=0
ip4=0
for octet in `seq 0 255`;do
normalize-ipv4-octet-2 $octet
mac4=$(printf "%02X\n" $ip2)
# Format IP address
ip="$ip1.$ip2.$ip3.$ip4"
# Format MAC address
mac="$mac1:$mac2:$mac3:$mac4:$mac5:$mac6"
# Pad with space
space=`printf '%*s' "$((15 - ${#ip}))"`
# Output matching IP address and MAC address
echo "$ip $space=> $mac"
done
}
function usage {
echo "Usage: $0 <MAC address>"
echo "Usage: $0 --list-all"
echo
echo "examples:"
echo " $0 DC:9F:DB:CE:13:57"
echo " $0 DC-9F-DB-CE-13-57"
echo " $0 DC 9F DB CE 13 57"
echo " $0 dc 9f db ce 13 57"
}
# Check for --list-all, otherwise proceed
while getopts ":-:" opt; do
if [ $OPTARG = "list-all" ]; then
list-all
exit
fi
done
if [[ "$*" =~ -*- ]]; then
usage
exit 1
fi
# Proceed if not --list-all
# Get # of arguments passed to this script
args=$#
# # 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 [ $args -eq 1 -a ${#1} -eq 17 ]; then
# Split 1 argument into 6 separate arguments, 1 for each octet
# and pass the 6 arguments to a new instance of this script
$0 `echo $1 | tr ":-" " "`
# After the new instance completes, make sure to end this one
exit
elif [ $args -eq 6 ]; then
mac1=$(echo $1|tr '[a-z]' '[A-Z]')
mac2=$(echo $2|tr '[a-z]' '[A-Z]')
mac3=$(echo $3|tr '[a-z]' '[A-Z]')
mac4=$(echo $4|tr '[a-z]' '[A-Z]')
mac5=$(echo $5|tr '[a-z]' '[A-Z]')
mac6=$(echo $6|tr '[a-z]' '[A-Z]')
else
usage
exit 1
fi
# Ensure nothing
# Convert last three hexadecimal octets to decimal values
ip1=100
ip2=$(printf "%d" "0x$mac4")
ip3=$(printf "%d" "0x$mac5")
ip4=$(printf "%d" "0x$mac6")
normalize-ipv4-octet-2 $ip2
normalize-ipv4-octet-4 $ip4
echo "$ip1.$ip2.$ip3.$ip4"