I have been struggling with a script to automate some security related activities. I have it pretty much working, all except the search. I have an input file formatted as such:
216.234.246.158 216.234.246.158 `[Spyware_Sites]`
24.249.221.22 24.249.221.200 `[Spyware_Sites]`
24.249.226.0 24.249.226.255 `[Spyware_Sites]`
24.23.0.0 24.30.255.255 `[Spyware_Sites]`
64.14.90.11 64.14.97.255 `[Spyware_Sites]`
64.128.107.0 64.128.107.255 `[Spyware_Sites]`
64.60.0.0 64.62.255.255 `[Spyware_Sites]`
64.62.133.6 64.62.133.6 `[Spyware_Sites]`
This represents ranges of IP addresses that have been defined in a security appliance. I need to search this file to determine if the input variable (An single IP Address) falls within any of these ranges. If so display the line.
I am a a novice at development, and naively though this would be fairly straight-forward. It wasn't. However, after many false starts, I was able to get some fairly accurate (fuzzy) results by iteratively searching the first octect for an exact match>file, then using agrep -B to find the best match from there. My code:
echo $1 >htmp1.tmp
cut -f 1 -d "." htmp1.tmp >htmp2.tmp
var1=$(cat htmp2.tmp)
grep -E ''"^$var1"'\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' hgworking.txt >htmp3.tmp
agrep -B -y $1 htmp3.tmp
This is not ideal, but works for now. I am new to programming -- surely there are folks out there that help me produce more exact results. Thanks in advance!
- Kevin