[IMPROVED] Refactorizar el script hexroute para mejorar legibilidad

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Mauro Rosero P. 2025-03-11 05:25:10 -05:00
parent 066caaa0b1
commit e9211614c3
Signed by: mrosero
GPG key ID: 83BD2A5F674B7E26

View file

@ -5,6 +5,7 @@
# for Windows(r) clients # for Windows(r) clients
# #
# Copyright (c) 2005-2015 Karl McMurdo # Copyright (c) 2005-2015 Karl McMurdo
# Modified: 2025-03-11
# #
# Freely distributable, but please keep header intact # Freely distributable, but please keep header intact
# #
@ -12,31 +13,43 @@
# #
###################################################################### ######################################################################
# Validate and split an IP address into components
# Args: $1 - IP address, $2 - Array name to store components
function split_ipaddr() { function split_ipaddr() {
[ ${1:-X} = X -o "${1}" != ${1//[^.0-9]/} -o ${1:-X} = X \ # Validate basic format
-o "${1//[0-9]/}" != "..." ] && return 1 if [ ${1:-X} = X ] || [ "${1}" != ${1//[^.0-9]/} ] || [ "${1//[0-9]/}" != "..." ]; then
local rval=0 return 1
local t fi
local T[1]
local T[2] local rval=0
local T[3] local t
local T[4] local -a T=([1]="" [2]="" [3]="" [4]="")
T[1]=${1%%.*}
T[2]=${1#*.} # Split the IP address
T[2]=${T[2]%%.*} T[1]=${1%%.*}
T[3]=${1#*.*.} T[2]=${1#*.}; T[2]=${T[2]%%.*}
T[3]=${T[3]%%.*} T[3]=${1#*.*.}; T[3]=${T[3]%%.*}
T[4]=${1#*.*.*.} T[4]=${1#*.*.*.}
[ ${T[1]} = 0 ] && rval=1
for t in 1 2 3 4 # Validate each octet
do [ ${T[1]} = 0 ] && rval=1
[ ${T[${t}]:-999} -gt 255 ] && rval=1
eval "${2}[${t}]=${T[${t}]}" for t in 1 2 3 4; do
done [ ${T[${t}]:-999} -gt 255 ] && rval=1
return ${rval} eval "${2}[${t}]=${T[${t}]}"
done
return ${rval}
} }
# Constants and configuration
VERSION="1.0"
AUTHOR="Karl McMurdo"
YEAR="2015"
# Usage and help messages
USAGE='echo -e "Usage: ${0//*\/} [-v|-h] target/bits [gw] gateway [target/bits [gw] gateway ...]\n\n\tie: ${0} 172.16.0.0/16 gw 192.168.1.1\n" && exit 1' USAGE='echo -e "Usage: ${0//*\/} [-v|-h] target/bits [gw] gateway [target/bits [gw] gateway ...]\n\n\tie: ${0} 172.16.0.0/16 gw 192.168.1.1\n" && exit 1'
INFO="HexRoute 1.0 Copyright (c) 2015 Karl McMurdo INFO="HexRoute ${VERSION} Copyright (c) ${YEAR} ${AUTHOR}
Converts human readable route information to a dhcpd hex string Converts human readable route information to a dhcpd hex string
@ -44,57 +57,80 @@ Converts human readable route information to a dhcpd hex string
Twice also prints option definiton lines for dhcpd.conf Twice also prints option definiton lines for dhcpd.conf
-h Prints this message -h Prints this message
" "
# DHCP configuration strings
DHCPHDR="# New Option Type for Windows Client Static Routes DHCPHDR="# New Option Type for Windows Client Static Routes
option new-static-routes code 249 = string;" option new-static-routes code 249 = string;"
DHCPS="option new-static-routes " DHCPS="option new-static-routes "
DHCPE=";" DHCPE=";"
# Error messages
BADTARG='echo -e "Invalid target network: ${1}" && exit 2' BADTARG='echo -e "Invalid target network: ${1}" && exit 2'
BADBITS='echo -e "Invalid Network bit value: ${1}" && exit 3' BADBITS='echo -e "Invalid Network bit value: ${1}" && exit 3'
BADGW='echo -e "Invalid Gateway: ${2}" && exit 4' BADGW='echo -e "Invalid Gateway: ${2}" && exit 4'
# Default settings
VERBOSE=false VERBOSE=false
EXTRAVERBOSE=false EXTRAVERBOSE=false
while [ "${1:0:1}" = "-" ] # Process command line options
do while [ "${1:0:1}" = "-" ]; do
if [ "${1}" = "-v" ] case "${1}" in
then "-v")
$VERBOSE && EXTRAVERBOSE=true $VERBOSE && EXTRAVERBOSE=true
VERBOSE=true VERBOSE=true
elif [ "${1}" = "-h" ] ;;
then "-h")
echo "${INFO}" echo "${INFO}"
eval "${USAGE}" eval "${USAGE}"
else ;;
eval "${USAGE}" *)
fi eval "${USAGE}"
shift ;;
esac
shift
done done
# Check if we have enough arguments
[ ${#} -lt 2 ] && eval "${USAGE}" [ ${#} -lt 2 ] && eval "${USAGE}"
OUT="" OUT=""
while [ ${#} -ge 2 ] # Process routes
do while [ ${#} -ge 2 ]; do
split_ipaddr ${1/\/*/} TARG || eval "${BADTARG}" # Parse target network
BITS=${1/*\//} split_ipaddr ${1/\/*/} TARG || eval "${BADTARG}"
shift BITS=${1/*\//}
[ ${BITS:-X} = X -o "${BITS}" != ${BITS//[^0-9]/} ] && eval "${BADBITS}" shift
[ ${BITS} -gt 32 -o ${BITS} -lt 1 ] && eval "${BADBITS}"
[ ${1:-X} = gw ] && shift # Validate bits
split_ipaddr ${1:-X} GW || eval "${BADGW}" if [ ${BITS:-X} = X ] || [ "${BITS}" != ${BITS//[^0-9]/} ]; then
shift eval "${BADBITS}"
OUT=${OUT}$( printf ":%02x:%02x" ${BITS} ${TARG[1]} ) fi
[ ${BITS} -gt 8 ] && OUT=${OUT}$( printf ":%02x" ${TARG[2]} )
[ ${BITS} -gt 16 ] && OUT=${OUT}$( printf ":%02x" ${TARG[3]} ) if [ ${BITS} -gt 32 ] || [ ${BITS} -lt 1 ]; then
[ ${BITS} -gt 24 ] && OUT=${OUT}$( printf ":%02x" ${TARG[4]} ) eval "${BADBITS}"
OUT=${OUT}$( printf ":%02x:%02x:%02x:%02x" ${GW[@]} ) fi
# Handle optional "gw" keyword
[ ${1:-X} = gw ] && shift
# Parse gateway
split_ipaddr ${1:-X} GW || eval "${BADGW}"
shift
# Build output in hex format
OUT=${OUT}$( printf ":%02x:%02x" ${BITS} ${TARG[1]} )
[ ${BITS} -gt 8 ] && OUT=${OUT}$( printf ":%02x" ${TARG[2]} )
[ ${BITS} -gt 16 ] && OUT=${OUT}$( printf ":%02x" ${TARG[3]} )
[ ${BITS} -gt 24 ] && OUT=${OUT}$( printf ":%02x" ${TARG[4]} )
OUT=${OUT}$( printf ":%02x:%02x:%02x:%02x" ${GW[@]} )
done done
# Output results
$EXTRAVERBOSE && echo "${DHCPHDR}" $EXTRAVERBOSE && echo "${DHCPHDR}"
if $VERBOSE if $VERBOSE; then
then echo ${DHCPS}${OUT/:/}${DHCPE}
echo ${DHCPS}${OUT/:/}${DHCPE}
else else
echo ${OUT/:/} echo ${OUT/:/}
fi fi