Advertisement
goebelmasse

country – Get country's name for an IP-address

Dec 14th, 2020
1,429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.10 KB | None | 0 0
  1. #!/bin/sh
  2. ########################################################################
  3. #
  4. # country
  5. # Get countryname for IP-address if possible
  6. #
  7. # Requires perl and installed Locale::Code.
  8. # On debianoid systems, apt-get install liblocale-codes-perl!
  9. # It isn't installed by default.
  10. #
  11. # $Id: country,v 1.2 2020/11/13 13:18:49 elias Exp $
  12. #
  13. ########################################################################
  14.  
  15. export PATH=/bin:/usr/bin
  16.  
  17. errc=0
  18. for ip
  19. do
  20.     code=`whois $ip | grep -i ^country | sed 's/^[^:]*:\s*//'`
  21.     if [ -z "$code" ]
  22.     then
  23.     echo "No country known for $ip" 1>&2
  24.     errc=`expr $errc + 1`
  25.     else
  26.     echo -n "$ip is from "
  27.     # Sometimes, there are more than one country in the whois
  28.     # record for an IP, therefore the funny loop.
  29.     # Would be nice to eliminate doubles, but I use my scripts
  30.     # and enhance them later if needed.
  31.     for i in $code
  32.     do
  33.         cntry=`perl -M'Locale::Country' -e "print code2country('$i');"`
  34.         echo -n "$cntry ($i) "
  35.     done
  36.     echo ""
  37.     fi
  38. done
  39.  
  40. if [ $errc -eq 0 ]
  41. then
  42.     exit 0
  43. else
  44.     echo "$errc error(s) occured" 1>&2
  45.     exit 1
  46. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement