mirror of
https://github.com/tonusoo/koduinternet-cpe
synced 2025-12-17 23:45:13 +02:00
Initial commit
This commit is contained in:
74
conf/usr/local/bin/mac_to_ip6_ll_addr
Executable file
74
conf/usr/local/bin/mac_to_ip6_ll_addr
Executable file
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Title : mac_to_ip6_ll_addr
|
||||
# Last modified date : 13.03.2023
|
||||
# Author : Martin Tonusoo
|
||||
# Description : Script finds the IPv6 link local address
|
||||
# from the MAC address.
|
||||
# Options : MAC address.
|
||||
# Notes : Script expects the MAC address as a
|
||||
# command line argument in the format
|
||||
# returned by "ip link" command, e.g
|
||||
# 52:54:00:db:99:99.
|
||||
|
||||
|
||||
mac_regex="^([0-9a-f]{2}:){5}([0-9a-f]{2})$"
|
||||
if [[ ! "$1" =~ $mac_regex ]]; then
|
||||
printf "%s\n%s\n" "Usage: ${0##*/} <MAC_addr>" \
|
||||
"Example: ${0##*/} 52:54:00:db:99:99" \
|
||||
>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
IFS=: read -r o1 o2 o3 o4 o5 o6 <<< "$1"
|
||||
|
||||
# Convert the four least significant bits of
|
||||
# the first octet of the MAC address from hex
|
||||
# to decimal.
|
||||
o1_dec=$(( 16#"${o1:1:1}" ))
|
||||
|
||||
# Loop through the four least significant bits of
|
||||
# the first octet of the MAC address by shifting
|
||||
# one bit to right and checking if the bit is set
|
||||
# or not on each cycle starting from the least
|
||||
# significant bit and moving towards the most
|
||||
# significant bit. This will convert the second
|
||||
# hex character of the MAC address reading from
|
||||
# left to binary.
|
||||
for (( n="$o1_dec"; n>0; n >>= 1 )); do
|
||||
o1_bits="$(( n & 1 ))$o1_bits"
|
||||
done
|
||||
|
||||
# Prepend leading zeros if needed.
|
||||
o1_bits=$(printf "%04d" "$o1_bits")
|
||||
|
||||
# Flip the second-least-significant bit of the
|
||||
# first octet of the MAC address and convert
|
||||
# four bits back to hex digit.
|
||||
flipped_bit=$(tr 01 10 <<< "${o1_bits:2:1}")
|
||||
o1_bits="${o1_bits:0:2}$flipped_bit${o1_bits:3:1}"
|
||||
o1_bits=$(printf "%x" $((2#"$o1_bits")))
|
||||
|
||||
# Finally, rebuild the first octet of the MAC address.
|
||||
o1="${o1:0:1}$o1_bits"
|
||||
|
||||
|
||||
# Build the hextets for the IPv6 link local address.
|
||||
if [[ $o1 == 00 ]] && [[ $o2 == 00 ]]; then
|
||||
h5=""
|
||||
else
|
||||
# Strip possible leading zeros.
|
||||
h5="$(printf "%x:" "0x$o1$o2")"
|
||||
fi
|
||||
|
||||
if [[ $o3 == 00 ]]; then
|
||||
h6="ff:"
|
||||
else
|
||||
h6="$(printf "%xff:" "0x$o3")"
|
||||
fi
|
||||
|
||||
h7="fe$o4:"
|
||||
|
||||
h8="$(printf "%x" "0x$o5$o6")"
|
||||
|
||||
printf "fe80::%s%s%s%s\n" "$h5" "$h6" "$h7" "$h8"
|
||||
75
conf/usr/local/bin/mcast_converter
Executable file
75
conf/usr/local/bin/mcast_converter
Executable file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Title : mcast_converter
|
||||
# Last modified date : 12.04.2023
|
||||
# Author : Martin Tonusoo
|
||||
# Description : Script converts IPv4 multicast address to
|
||||
# multicast MAC address or multicast MAC
|
||||
# address to IPv4 multicast addresses depending
|
||||
# on the input.
|
||||
# Options : IPv4 multicast address or multicast MAC address.
|
||||
# Notes :
|
||||
|
||||
|
||||
# 224.0.0.0 - 239.255.255.255
|
||||
read -r mip_regex << EOF
|
||||
^2(2[4-9]|3[0-9])\.\
|
||||
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\.\
|
||||
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\.\
|
||||
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)$
|
||||
EOF
|
||||
|
||||
|
||||
# 01:00:5e:00:00:00 - 01:00:5e:7f:ff:ff
|
||||
read -r mac_regex << EOF
|
||||
^01:00:5e:\
|
||||
([0-7][0-9a-f]):\
|
||||
([0-9a-f]{2}):\
|
||||
([0-9a-f]{2})$
|
||||
EOF
|
||||
|
||||
|
||||
if [[ "$1" =~ $mip_regex ]]; then
|
||||
|
||||
for (( n="${BASH_REMATCH[2]}"; n>0; n >>= 1 )); do
|
||||
o2_bits="$(( n & 1 ))$o2_bits"
|
||||
done
|
||||
|
||||
printf -v o2_bits "%08d" "$o2_bits"
|
||||
o2_bits="0${o2_bits:1}"
|
||||
|
||||
printf "%s:%02x:%02x:%02x\n" \
|
||||
"01:00:5e" \
|
||||
"$(( 2#$o2_bits ))" \
|
||||
"${BASH_REMATCH[3]}" \
|
||||
"${BASH_REMATCH[4]}"
|
||||
|
||||
elif [[ "$1" =~ $mac_regex ]]; then
|
||||
|
||||
o4_dec=$(( 16#"${BASH_REMATCH[1]}" ))
|
||||
|
||||
for (( n="$o4_dec"; n>0; n >>= 1 )); do
|
||||
o4_bits="$(( n & 1 ))$o4_bits"
|
||||
done
|
||||
|
||||
printf -v o4_bits "%08d" "$o4_bits"
|
||||
o4_bits="${o4_bits:1}"
|
||||
|
||||
for bits in {0..1}{0..1}{0..1}{0..1}{0..1}; do
|
||||
|
||||
printf "%d.%d.%d.%d\n" \
|
||||
$(( 2#"1110${bits:0:4}" )) \
|
||||
$(( 2#"${bits:4:1}$o4_bits" )) \
|
||||
$(( 16#"${BASH_REMATCH[2]}" )) \
|
||||
$(( 16#"${BASH_REMATCH[3]}" ))
|
||||
|
||||
done
|
||||
|
||||
else
|
||||
|
||||
printf "%s\n%s\n" \
|
||||
"Invalid multicast MAC or IPv4 address" \
|
||||
"Examples: '${0##*/} 01:00:5e:00:99:0a' or '${0##*/} 239.1.2.100'" >&2
|
||||
exit 1
|
||||
|
||||
fi
|
||||
Reference in New Issue
Block a user