Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #26 from pittmesh/expr_math
Browse files Browse the repository at this point in the history
Avoid collisions #25
  • Loading branch information
colindean authored Jun 22, 2016
2 parents 136936f + 976f226 commit ce6ff1c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
17 changes: 13 additions & 4 deletions mm-mac2ipv4.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
# * 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
Expand All @@ -20,10 +28,10 @@ function list-all {
ip3=0
ip4=0

for ip2 in `seq 0 255`;do
mac4=$(printf "%02X\n" $ip2)
for octet in `seq 0 255`;do
normalize-ipv4-octet-2 $octet

((ip2=ip2%64 + 64))
mac4=$(printf "%02X\n" $ip2)

# Format IP address
ip="$ip1.$ip2.$ip3.$ip4"
Expand Down Expand Up @@ -99,6 +107,7 @@ ip2=$(printf "%d" "0x$mac4")
ip3=$(printf "%d" "0x$mac5")
ip4=$(printf "%d" "0x$mac6")

((ip2=ip2%64 + 64))
normalize-ipv4-octet-2 $ip2
normalize-ipv4-octet-4 $ip4

echo "$ip1.$ip2.$ip3.$ip4"
12 changes: 9 additions & 3 deletions mm-mac2ipv4.pl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sub list_all {
$mac[3] = $i;

# Format IP address
my $ip = sprintf('%d.%d.%d.%d', $ip[0], $i % 64 + 64, $ip[2], $ip[3]);
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);
Expand Down Expand Up @@ -66,9 +66,15 @@ sub list_all {
my @mac = @ARGV;
$_ = uc for @mac;

# Ensure nothing
# 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]) % 64 + 64, hex($mac[4]), hex($mac[5]));
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";
9 changes: 5 additions & 4 deletions mm-mac2ipv4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ while getopts ":-:" opt; do
ip3=0
ip4=0

for ip2 in `seq 0 255`;do
mac4=$(printf "%02X\n" $ip2)
for octet in `seq 0 255`;do
ip2=$(expr $octet % 32 + 96)

ip2=$(expr $ip2 % 64 + 64)
mac4=$(printf "%02X\n" $ip2)

# Format IP address
ip="$ip1.$ip2.$ip3.$ip4"
Expand Down Expand Up @@ -87,6 +87,7 @@ ip2=$(printf "%d" "0x$mac4")
ip3=$(printf "%d" "0x$mac5")
ip4=$(printf "%d" "0x$mac6")

ip2=$(expr $ip2 % 64 + 64)
ip2=$(expr $ip2 % 32 + 96)
ip4=$(expr $ip4 - $(expr $ip4 % 64 - $ip4 % 32))

echo "$ip1.$ip2.$ip3.$ip4"
49 changes: 32 additions & 17 deletions mm-mac2ipv4.tests
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,50 @@ test() {
fi
}

rand64to127() {
((randdec=$RANDOM / 512 + 64))
return $randdec
randIPv4octet1() {
randIP="100"
}

randIPv4octet2() {
# Random 96-127
((randdec=$RANDOM / 1024 + 96))
}

randIPv4octet3() {
# Random 0-255
((randdec=$RANDOM / 128))
}

rand0to255() {
randIPv4octet4() {
# Random 0-255 every other 32
((randdec=$RANDOM / 128))
return $randdec
((randdec=randdec - (randdec % 64 - randdec % 32)))
}

randIPMA() {
local a
randMA=("DC:9F:DB" "DC:9F:DB" "DC:9F:DB" "DC:9F:DB")
randIP="100"
rand64to127
randIPv4octet1
echo $randdec
randIPv4octet2
echo $randdec
randMA[0]="${randMA[0]}:$(printf "%02X\n" $((randdec - 64)))"
randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)"
randMA[2]="${randMA[2]}:$(printf "%02X\n" $((randdec + 64)))"
randMA[3]="${randMA[3]}:$(printf "%02X\n" $((randdec + 128)))"
randIP="$randIP.$randdec"
for face in ^ ^; do
rand0to255
randMA[0]="${randMA[0]}:$(printf "%02X\n" $randdec)"
randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)"
randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)"
randMA[3]="${randMA[3]}:$(printf "%02X\n" $randdec)"
randIP="$randIP.$randdec"
done
return
randIPv4octet3
randMA[0]="${randMA[0]}:$(printf "%02X\n" $randdec)"
randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)"
randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)"
randMA[3]="${randMA[3]}:$(printf "%02X\n" $randdec)"
randIP="$randIP.$randdec"
randIPv4octet4
randMA[0]="${randMA[0]}:$(printf "%02X\n" $randdec)"
randMA[1]="${randMA[1]}:$(printf "%02X\n" $randdec)"
randMA[2]="${randMA[2]}:$(printf "%02X\n" $randdec)"
randMA[3]="${randMA[3]}:$(printf "%02X\n" $randdec)"
randIP="$randIP.$randdec"
}

random-tests() {
Expand Down Expand Up @@ -86,7 +101,7 @@ list-all-tests() {
}

for script_to_test in "${scripts_to_test[@]}"; do
list-all-tests
#list-all-tests
random-tests
done

Expand Down

0 comments on commit ce6ff1c

Please sign in to comment.