I am trying to create a script that nmap's trough some of our networks in order to find active hosts and hostnames and to fill our IPAM correctly. I've written a function in Bash that works like a 'subnet calculator' and gives me the exact range that I want to loop through.
The input csv consists of the following information:
vlanId;vlanName,Network
1;localLan;192.168.1.0/24;;;
Note that the input the function gets is just the network without netmask "192.168.1.0
"
function calculateNet () {
splitRow=$(echo $1 | sed -e 's/\// /g'| awk '{print $1}')
netStart=${splitRow: -2}
if [[ $netStart =~ "." ]] ; then
netStart=${splitRow: -1}
fi
netEnd=$(expr $netStart + $2)
echo "$netStart..$netEnd"
}
inFile="input/file.csv"
for row in `cat $inFile | sed -e 's/;/ /'g | awk '{print $3}' | egrep ^1`; do
netSuffix=$(echo $row | sed -e 's/\./ /g' | awk '{print $1,$2,$3'} | sed -e 's/ /./g')
outFile="output/$netSuffix.csv"
if [[ $row =~ "/24" ]]; then
addrCount=254
elif [[ $row =~ "/26" ]]; then
addrCount=64
fi
range=$(calculateNet $row $addrCount)
for host in {$range};
do
echo $netSuffix.$host
done
done
The function works perfectly, it gives me the correct range to loop through, for a /24 network the result is 0..254. for a specific /26 network this an be 0..62/64..126/.....
However the for host in {$range}
does not work. It does not loop through all numbers in the range but outputs it as a string:
192.168.1.{0..254}
Why is it nog looping through my range, what am I missing here? Thanks in advance!