I have a bash script that creates 1024 clients. I have to allocate IPv4 addresses from a given subnet to those clients accordingly. The challenge is how to map the client ID to a IPv4 address.
COUNTER=0
for ip in 172.16.10{0..4}.{0..255}
do
COUNTER=$((COUNTER+1))
FILE_NUMBER="${COUNTER}"
CLIENT_IPV4="${ip}"
echo "###Client ${COUNTER}###
Address = ${CLIENT_IPV4}/32" >"$HOME/${FILE_NUMBER}.conf"
done
While this seems ok at first, later on I need to know that Client 500, corresponds to the IP address 172.16.101.243. Is there any clever way to do that?
I came up with this solution, but I'm not sure if this is a good one:
function map_client_to_ipv4() {
val=$(($1/256))
mod=$(($1%256))
ret_val=172.16.10${val}.$((mod-1))
}
map_client_to_ipv4 500
echo $ret_val