How to convert string 192.168.1.{10..12}
in to array of IPs with range?
It's working when adding IPs in format 192.168.1.10 192.168.1.11 192.168.1.12
but not if you add range.
This examples as string also not working:
192.168.1.{10..12}
and this 192.168.{1..3}.{10..12}
Here is the code:
#!/bin/bash
iprange="";
tout="";
# Dialog for entering IP range and ping timeout
dialog --colors --ok-label "Submit" \
--backtitle "Scan IP range and request timeout" \
--title "\Zb Scan range \Zn" \
--form "Set scan IP range & request timeout\n \
\n\Zb------------------ Example ------------------ \
\nIP range: 192.168.1.1 192.168.1.2 \
\nIP range: 192.168.1.{10..255} \
\nTimeout (sec): 0.1 or 1\Zn" 15 50 0 \
"IP range:" 1 1 "$iprange" 1 15 40 0 \
"Timeout (sec):" 2 1 "$tout" 2 15 3 0 > file.tmp \
2>&1 >/dev/tty;
# Start retrieving each line from temp file 1 by 1 with sed and declare variables as inputs
iprange=$(sed -n 1p file.tmp)
tout=$(sed -n 2p file.tmp)
# remove temporary file created
rm -f file.tmp
# NOT WORKING if you enter in iprange 192.168.1.{10..12}
# WORKING if you enter in iprange 192.168.1.10 192.168.1.11 etc.
echo ${iprange[@]}
ips=($iprange)
echo "Scanning for devices...";
for ip in ${ips[@]}
do
echo "-------------------------";
echo "$ip";
gtimeout $tout ping "$ip"; # use timeout for linux
done
CodePudding user response:
This might be what you want:
#!/bin/bash
iprange=$(head -n1 file.tmp)
if [[ $iprange = *[^0-9.{}[:blank:]]* ]]; then
echo 'Invalid IP range' >&2
exit 1
fi
eval "ips=($iprange)"
for ip in "${ips[@]}"; do
echo "$ip"
# do stuff with $ip
done