Networking

檢查重疊的子網

  • February 4, 2020

我想知道是否有辦法檢查某些子網是否與 IP 列表重疊(或不重疊)例如,我們有這個列表:

197.26.9.128/25
193.36.81.128/25
194.33.24.0/22
188.115.195.80/28
188.115.195.64/28
185.59.69.96/28
185.59.69.32/27
41.202.219.32/27
41.202.219.128/29
154.70.120.16/28
154.70.120.32/28
154.70.120.0/28
41.202.219.208/28
41.202.219.136/29
197.157.209.0/24

我想檢查以下 IP 是否與上一個列表重疊。

197.26.9.0/26
194.33.26.0/26      (IP overlapped with 194.33.24.0/22)
188.115.195.88/29   (IP overlapped with 188.115.195.80/28)
41.202.219.0/24
197.157.209.128/28  (IP overlapped with 197.157.209.0/24)

輸出將是下一個:

197.26.9.0/26
41.202.219.0/24

這裡有幾位給你。首先,Bash 中的腳本,因此效率不高。它並不完全符合您的要求,因為它只檢查一對子網並報告重疊。在腳本下面有幾個粗略的 shell 命令,認​​為結果不是你想要的形式。因此,您需要根據您的需要整合和調整整個系統,或者將它們視為說明邏輯的草圖。

#!/usr/bin/env bash

subnet1="$1"
subnet2="$2"

# calculate min and max of subnet1
# calculate min and max of subnet2
# find the common range (check_overlap)
# print it if there is one

read_range () {
   IFS=/ read ip mask <<<"$1"
   IFS=. read -a octets <<< "$ip";
   set -- "${octets[@]}";
   min_ip=$(($1*256*256*256 + $2*256*256 + $3*256 + $4));
   host=$((32-mask))
   max_ip=$(($min_ip+(2**host)-1))
   printf "%d-%d\n" "$min_ip" "$max_ip"
}

check_overlap () {
   IFS=- read min1 max1 <<<"$1";
   IFS=- read min2 max2 <<<"$2";
   if [ "$max1" -lt "$min2" ] || [ "$max2" -lt "$min1" ]; then return; fi
   [ "$max1" -ge "$max2" ] && max="$max2" ||   max="$max1"
   [ "$min1" -le "$min2" ] && min="$min2" || min="$min1"
   printf "%s-%s\n" "$(to_octets $min)" "$(to_octets $max)"
}

to_octets () {
   first=$(($1>>24))
   second=$((($1&(256*256*255))>>16))
   third=$((($1&(256*255))>>8))
   fourth=$(($1&255))
   printf "%d.%d.%d.%d\n" "$first" "$second" "$third" "$fourth" 
}

range1="$(read_range $subnet1)"
range2="$(read_range $subnet2)"
overlap="$(check_overlap $range1 $range2)"
[ -n "$overlap" ] && echo "Overlap $overlap of $subnet1 and $subnet2"

用法和結果如下:

$ ./overlap.bash 194.33.26.0/26 194.33.24.0/22
Overlap 194.33.26.0-194.33.26.63 of 194.33.26.0/26 and 194.33.24.0/22

現在鑑於您的第一個子網列表在文件中list並且要檢查的子網在文件中to_check,您可以使用腳本查找所有重疊。

$ while read l; do list+=("$l"); done < list
$ while read t; do to_check+=("$t"); done < to_check
$ for i in "${list[@]}"; do for j in "${to_check[@]}"; do \
./overlap.bash "$i" "$j"; done; done

這是結果:

Overlap 194.33.26.0-194.33.26.63 of 194.33.24.0/22 and 194.33.26.0/26
Overlap 188.115.195.88-188.115.195.95 of 188.115.195.80/28 and 188.115.195.88/29
Overlap 41.202.219.32-41.202.219.63 of 41.202.219.32/27 and 41.202.219.0/24
Overlap 41.202.219.128-41.202.219.135 of 41.202.219.128/29 and 41.202.219.0/24
Overlap 41.202.219.208-41.202.219.223 of 41.202.219.208/28 and 41.202.219.0/24
Overlap 41.202.219.136-41.202.219.143 of 41.202.219.136/29 and 41.202.219.0/24
Overlap 197.157.209.128-197.157.209.143 of 197.157.209.0/24 and 197.157.209.128/28

如您所見,41.202.219.0/24有四個重疊,這與您對問題的期望相反。

要僅獲取與第一個列表不重疊的子網,腳本會短得多。您不需要該to_octets函式,並且該check_overlap函式已經可以在這一行給出結果:

if [ "$max1" -lt "$min2" ] || [ "$max2" -lt "$min1" ]; then return; fi

最後兩行也可以更改(最後一行完全刪除)。

至於集成邏輯,可以對第一個列表進行短路檢查,因為並非所有組合都必須檢查。一個負數就足夠了。

嘗試使用ipconflict , pip install ipconflict.

將您的子網放入/tmp/subnets.txt

ipconflict -f /tmp/subnets.txt

輸出:

conflict found: 194.33.24.0/22 <-> 194.33.26.0/26
conflict found: 188.115.195.80/28 <-> 188.115.195.88/29
conflict found: 41.202.219.32/27 <-> 41.202.219.0/24
conflict found: 41.202.219.128/29 <-> 41.202.219.0/24
conflict found: 41.202.219.208/28 <-> 41.202.219.0/24
conflict found: 41.202.219.136/29 <-> 41.202.219.0/24
conflict found: 197.157.209.0/24 <-> 197.157.209.128/28

引用自:https://unix.stackexchange.com/questions/430588