Exclude IPs from IP list that are from Russia

0

Which command in Bash should I use to exclude all IPs that are from Russia from an IP list?

I had in mind maybe with the help of geoiplookup. What I would really like to have as result;

  • original-IP-list.txt
  • IP-list-excluding-IPs-from-Russia.txt

Gerald

Posted 2019-06-17T16:26:35.223

Reputation: 11

How is the list formatted? – Arkadiusz Drabczyk – 2019-06-17T17:13:03.950

1 IP per line: 1.2.3.4 – Gerald – 2019-06-17T17:43:04.067

Answers

2

You may find the list of Russian IPs in the webpage Russian Federation IP Address Ranges. The list currently contains 8381 ranges which makes assigned 45,626,826 IP addresses.

If you would rather have a programming solution, you may use the website ipinfo.io, free for 1000 requests/day:

curl ipinfo.io/23.66.166.151

You may alternatively use ifconfig.me or ipstack.com, each with its own free and paid plans.

harrymc

Posted 2019-06-17T16:26:35.223

Reputation: 306 093

This might be useful in some case but my IP list is around 80k if not more; I thought the best solution was with something like 'cat ./ip_list.txt | xargs -n 1 geoiplookup { } | grep "RU" > exclude.Russia_IPs.txt' but that's not the correct syntax to fulfill my needs... ;/ – Gerald – 2019-06-17T21:41:41.080

I didn't recommend geoiplookup because there are many negative reports for it. Good that it worked for you. – harrymc – 2019-06-18T06:06:13.290

Download IP ranges from https://lite.ip2location.com/russian-federation-ip-address-ranges, trim them and create exclude.Russia_IPs.txt with this script: https://stackoverflow.com/a/35681431/3776858

– Cyrus – 2019-06-19T08:14:29.567

1

For those who are searching for an answer like I was searching earlier;

while read -r ip; do gl=$(geoiplookup "$ip") || continue; case "$gl" in *': RU,'*) printf '%s\n' "$ip" ;; esac; done  <"$1"

enjoy!

Gerald

Posted 2019-06-17T16:26:35.223

Reputation: 11