我在bash中有两个数组。序号1为vlan子网,不包含最后一个八位字节。数组数2是我想忽略的八位元组列表,同时用Nmap扫描子网。假设每个子网有254个可ping通的ip (c类子网)我希望脚本扫描每个子网,并排除以1,2,3,252,253,254结束的ip,通常是路由器/防火墙/交换机。我成功地运行了2次迭代,但在if [[$host == $host."${ignore[@]"]]来识别相关的IP (sunbet + ignore string)非常感谢您的帮助。
#!/bin/bash
# lets assume each subnet has 254 ips and all ignore ip's like 10.6.114.1 10.6.115.1 and 10.5.120.1
declare -a vlans=(
10.6.114
10.6.115
10.5.120
)
declare -a ignore=(
1
2
3
252
253
254
)
for vlan in "${vlans[@]}"; do
nmap -sn "$vlan" | grep Nmap | awk "{print $5}" | sed -n '1!p' | sed -e "$d" | sort > /tmp/vlan_ips.txt
readarray -t hosts < /tmp/vlan_ips.txt
for host in "${hosts[@]}"; do
check=$(echo "$host" | cut -d"." -f1-3)
if [ $host == $check."${ignore[@]}" ]; then
echo 'skipping record'
fi
done
done
这可能适合您:
for vlan in "${vlans[@]}"; do
for ign in "${ignore[@]}"; do
printf '%s.%sn' "$vlan" "$ign"
done >/tmp/ignore
nmap -n -sn "$vlan.0/24" -oG - 2>/dev/null |
grep -vwFf /tmp/ignore |
awk '/Host:/{print $2}' |
while read -r host; do
echo "$host"
done
done