Skip to main content
added explanation
Source Link
DenisZ
  • 176
  • 7

As per original question I managed to update grep statement to get wanted result, for anyone interested in bash only solution so updated part of the code looks like:

for IPBL in `cat /tmp/IPs`; do 
  CT=`grep -c "^${IPBL%.[0-9]*.[0-9]*}\." /tmp/IPs`
    if [ "$CT" -gt "10" ]; then 
      echo "$IPBL  ${IPBL%.[0-9]*.[0-9]*}.0.0/16  $CT" >>/tmp/spam.lst
    fi
done
cat /tmp/spam.lst |sort -n

And now ip 8.6.144.6 has only one matchchanged was argument form the grep
^-to start from IPs filethe begining of the line, and thus not displayed
\. to add a dot after second number in the output, but oneip address thus giving exact match of particular B class match would look like thisIP range:

"^${IPBL%.[0-9]*.[0-9]*}\."

And now ip 8.6.144.6 has only one match from IPs file, and thus not displayed in the output, but one B class match would look like this:
3.8.35.118  3.8.0.0/16  12
3.8.36.119  3.8.0.0/16  12
3.8.36.121  3.8.0.0/16  12
3.8.37.124  3.8.0.0/16  12
3.8.37.125  3.8.0.0/16  12
3.8.37.126  3.8.0.0/16  12
3.8.37.94  3.8.0.0/16  12
3.8.37.96  3.8.0.0/16  12
3.8.37.97  3.8.0.0/16  12
3.8.37.97  3.8.0.0/16  12
3.8.37.98  3.8.0.0/16  12
3.8.37.98  3.8.0.0/16  12

As per original question I managed to update grep statement to get wanted result, for anyone interested in bash only solution so updated part of the code looks like:

for IPBL in `cat /tmp/IPs`; do 
  CT=`grep -c "^${IPBL%.[0-9]*.[0-9]*}\." /tmp/IPs`
    if [ "$CT" -gt "10" ]; then 
      echo "$IPBL  ${IPBL%.[0-9]*.[0-9]*}.0.0/16  $CT" >>/tmp/spam.lst
    fi
done
cat /tmp/spam.lst |sort -n

And now ip 8.6.144.6 has only one match from IPs file, and thus not displayed in the output, but one B class match would look like this:

3.8.35.118  3.8.0.0/16  12
3.8.36.119  3.8.0.0/16  12
3.8.36.121  3.8.0.0/16  12
3.8.37.124  3.8.0.0/16  12
3.8.37.125  3.8.0.0/16  12
3.8.37.126  3.8.0.0/16  12
3.8.37.94  3.8.0.0/16  12
3.8.37.96  3.8.0.0/16  12
3.8.37.97  3.8.0.0/16  12
3.8.37.97  3.8.0.0/16  12
3.8.37.98  3.8.0.0/16  12
3.8.37.98  3.8.0.0/16  12

As per original question I managed to update grep statement to get wanted result, for anyone interested in bash only solution so updated part of the code looks like:

for IPBL in `cat /tmp/IPs`; do 
  CT=`grep -c "^${IPBL%.[0-9]*.[0-9]*}\." /tmp/IPs`
    if [ "$CT" -gt "10" ]; then 
      echo "$IPBL  ${IPBL%.[0-9]*.[0-9]*}.0.0/16  $CT" >>/tmp/spam.lst
    fi
done
cat /tmp/spam.lst |sort -n

changed was argument form the grep
^-to start from the begining of the line, and
\. to add a dot after second number in the ip address thus giving exact match of particular B class IP range:

"^${IPBL%.[0-9]*.[0-9]*}\."

And now ip 8.6.144.6 has only one match from IPs file, and thus not displayed in the output, but one B class match would look like this:
3.8.35.118  3.8.0.0/16  12
3.8.36.119  3.8.0.0/16  12
3.8.36.121  3.8.0.0/16  12
3.8.37.124  3.8.0.0/16  12
3.8.37.125  3.8.0.0/16  12
3.8.37.126  3.8.0.0/16  12
3.8.37.94  3.8.0.0/16  12
3.8.37.96  3.8.0.0/16  12
3.8.37.97  3.8.0.0/16  12
3.8.37.97  3.8.0.0/16  12
3.8.37.98  3.8.0.0/16  12
3.8.37.98  3.8.0.0/16  12
Source Link
DenisZ
  • 176
  • 7

As per original question I managed to update grep statement to get wanted result, for anyone interested in bash only solution so updated part of the code looks like:

for IPBL in `cat /tmp/IPs`; do 
  CT=`grep -c "^${IPBL%.[0-9]*.[0-9]*}\." /tmp/IPs`
    if [ "$CT" -gt "10" ]; then 
      echo "$IPBL  ${IPBL%.[0-9]*.[0-9]*}.0.0/16  $CT" >>/tmp/spam.lst
    fi
done
cat /tmp/spam.lst |sort -n

And now ip 8.6.144.6 has only one match from IPs file, and thus not displayed in the output, but one B class match would look like this:

3.8.35.118  3.8.0.0/16  12
3.8.36.119  3.8.0.0/16  12
3.8.36.121  3.8.0.0/16  12
3.8.37.124  3.8.0.0/16  12
3.8.37.125  3.8.0.0/16  12
3.8.37.126  3.8.0.0/16  12
3.8.37.94  3.8.0.0/16  12
3.8.37.96  3.8.0.0/16  12
3.8.37.97  3.8.0.0/16  12
3.8.37.97  3.8.0.0/16  12
3.8.37.98  3.8.0.0/16  12
3.8.37.98  3.8.0.0/16  12