将字符串转换为 IP 范围数组
Convert string to IP range array
如何将字符串 192.168.1.{10..12}
转换为具有范围的 IP 数组?
它在以 192.168.1.10 192.168.1.11 192.168.1.12
格式添加 IP 时有效,但如果您添加范围则无效。
这个字符串示例也不起作用:
192.168.1.{10..12}
和这个 192.168.{1..3}.{10..12}
代码如下:
#!/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
这可能是您想要的:
#!/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
如何将字符串 192.168.1.{10..12}
转换为具有范围的 IP 数组?
它在以 192.168.1.10 192.168.1.11 192.168.1.12
格式添加 IP 时有效,但如果您添加范围则无效。
这个字符串示例也不起作用:
192.168.1.{10..12}
和这个 192.168.{1..3}.{10..12}
代码如下:
#!/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
这可能是您想要的:
#!/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