我在 bash 中有 2 個陣列。陣列編號 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
uj5u.com熱心網友回復:
這可能對你有用:
for vlan in "${vlans[@]}"; do
for ign in "${ignore[@]}"; do
printf '%s.%s\n' "$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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322614.html
上一篇:嵌套回圈邊界
下一篇:將主要游戲回圈轉化為功能
