排除某个范围内的 IP 以避免冲突

排除某个范围内的 IP 以避免冲突

我有一个很长的 IP 和范围列表,我想调试我的列表,删除与跨域路由

例如:

8.8.8.0/24
8.8.8.8
23.236.62.147
23.236.48.0/20
104.154.76.93
104.154.0.0/15
etc

因此我需要删除23.236.62.147(因为它是的子网23.236.48.0/20)、104.154.76.93(因为它是的子网104.154.0.0/15)、8.8.8.8(因为是的子网8.8.8.0/24)等等,等等

如何在 bash/command linux 中执行此操作?

答案1

使用 bash 和 GNU grep:

#!/bin/bash

common() {
  # From http://stackoverflow.com/a/35114656/3776858

  D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
  declare -i c=0                              # set integer attribute

  # read and convert IPs to binary
  IFS=./ read -r a1 a2 a3 a4 m <<< "$1"
  b1="${D2B[$a1]}${D2B[$a2]}${D2B[$a3]}${D2B[$a4]}"

  IFS=./ read -r a1 a2 a3 a4 m <<< "$2"
  b2="${D2B[$a1]}${D2B[$a2]}${D2B[$a3]}${D2B[$a4]}"

  # find number of same bits ($c) in both IPs from left, use $c as counter
  for ((i=0;i<32;i++)); do
    [[ ${b1:$i:1} == ${b2:$i:1} ]] && c=c+1 || break
  done    

  # create string with zeros
  for ((i=$c;i<32;i++)); do
    fill="${fill}0"
  done    

  # append string with zeros to string with identical bits to fill 32 bit again
  new="${b1:0:$c}${fill}"

  # convert binary $new to decimal IP with netmask
  new="$((2#${new:0:8})).$((2#${new:8:8})).$((2#${new:16:8})).$((2#${new:24:8}))/$c"
  echo "$new"
}


cidr="$1"

# using process substitution
grep -vxFf <(
  grep -v / "$cidr" | while IFS= read -r ip1; do
    grep / "$cidr" | while IFS=/ read -r ip2 mask2; do
      out=$(common "$ip2/$mask2" "$ip1/32")
      out_mask="${out#*/}"
      if [[ $out_mask -ge $mask2 ]]; then   # -ge: greater-than-or-equal
        echo "$ip1"
      fi
    done
  done
) "$cidr" > cidr_clean.txt

用法:./shadow.sh your_cidr_file.txt

在当前目录中,您将获得 cidr_clean.txt:

8.8.8.0/24
23.236.48.0/20
104.154.0.0/15

相关内容