2

Below is a shell script I would really like to be able to feed a txt document of IP addresses to determine countries accessing my website.

Inside the text document, lets call it "IPlist.txt" might look something like:

123.123.123.123
111.111.111.111
222.222.22.222

Can i somehow modify the script below to call "IPlist.txt" and read out each line and feed that into the spot of the $IP variable?

#!/bin/sh
#
###
### For assistance, please visit forum.ipinfodb.com
#
# Created by Eric Gamache on 2009-05-26
# Version 1.0 by Eric Gamache -- 2009-06-04
# Version 1.1 updated by Marc-Andre Caron -- 2009-06-08 .. Added timezone
# Version 1.2 updated by Eric Gamache -- 2009-06-08 .. fix minors bugs.
# Version 1.3 updated by Marc-Andre Caron -- 2010-02-11 .. new timezone support, reduced complexity of the script.
# Version 1.4 updated by Junjie Wu -- 2012-06-03 .. added api_key and precision support, removed deprecated timezone support.
#
# This script is provided "as is", with absolutely no warranty expressed or
# implied. Any use is at your own risk. Permission to use or copy this
# script for any purpose is hereby granted without fee. Permission to
# modify the code and to distribute modified code is granted, provided
# the above notices are retained, and a notice that the code was modified
# is included with the above copyright notice.
#
###############################################
# Please supply your own API key
# You can get a free API key by registering on http://ipinfodb.com
YOUR_API_KEY=""
###############################################
####
####
####
WGET_OPTION="=-b -q --wait=3 --waitretry=2 --random-wait --limit-rate=9578 "
WGET_AGENT="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
#
ERROR=0
#
###############################################
if [ "$YOUR_API_KEY" = "" ]; then
  echo "Please edit the script to provide YOUR_API_KEY"
  exit
fi
##############
if [ "$1" = "" ]; then
  ERROR=1
else
  IP=$1
fi
##############
if [ "$2" != "" ]; then
  if [ "$2" != "json" ] && [ "$2" != "xml" ] && [ "$2" != "csv" ]; then
    ERROR=1
  fi
  TYPE="$2"
else
  ERROR=1
fi
##############
if [ "$3" != "" ]; then
  if [ "$3" != "city" ] && [ "$3" != "country" ] ; then
    ERROR=1
  fi
  PREC=$3
else
  ERROR=1
fi
###############################################

###############################################
if [ "$ERROR" != "0" ]; then
  echo " "
  echo " usage : $0 IP TYPE PRECISION"
  echo " Where IP is the IP to check"
  echo " TYPE is the output type (csv|xml|json)"
  echo " PRECISION can only be city or country (city|country)"
  echo " Big thanks to the team of IPInfoDB (http://ipinfodb.com)"
  exit
fi
###############################################
#
TST_wget=`wget > /dev/null 2>&1`
#
ErrorLevel=$?
#
if [ "$ErrorLevel" != 1 ] ; then
  echo " ----"
  echo " wget not found; please install it for proper operation."
  echo " ----"
  exit
fi
###############################################
###############################################
#######
#######
URL="http://api.ipinfodb.com/v3/ip-$PREC/?key=$YOUR_API_KEY&ip=$IP&format=$TYPE"
Info=`wget -qO- --user-agent="$WGET_AGENT" "$URL" 2>&1`
echo "$Info"

I'm sure this can be done straight forwardly. I just have a limited knowledge of grep/awk/sed/bash etc in general. I am hoping someone can come to my aid!

Cheers,

James

James
  • 23
  • 2

2 Answers2

3

You don't need to modify the script at all. Just run it in a bash loop. Something like:

$ while read ip; do IP_finding_script.sh $ip csv city; done < IPlist.txt

To save the output in a file do:

$ while read ip; do IP_finding_script.sh $ip csv city; done < IPlist.txt > outfile.txt

To save the output of each input IP into a separate file :

$ while read ip; do IP_finding_script.sh $ip csv city > $ip".out"; done < IPlist.txt
terdon
  • 52,568
  • 14
  • 124
  • 170
  • 1
    You are a champion mate! terdon good stuff! I knew it would be simple, just didn't know how! perfect! I'm assuming if i add " > output.txt" to the end of that I should have them saved? Thank you! – James Aug 25 '12 at 14:40
  • @James Cheers :) Yes, adding " > output.txt" will save them. Answer updated accordingly. – terdon Aug 25 '12 at 14:41
  • 1
    Read [here](http://mywiki.wooledge.org/BashFAQ/001) for the preferred ways to read a file. Short version: use input redirection with the while loop, and don't use `for`. – chepner Aug 25 '12 at 15:35
  • @chepner True enough, +1. Answer updated accordingly. In my defense, I _did_ know the input file contains IPs. – terdon Aug 25 '12 at 20:41
-2
#!/bin/sh
#
##############################################################################
### Source: https://ipinfodb.com/api
#           
# Created by Eric Gamache on 2009-05-26
#
# Version 1.0 by Eric Gamache -- 2009-06-04
# Version 1.1 updated by Marc-Andre Caron -- 2009-06-08 .. Added timezone
# Version 1.2 updated by Eric Gamache -- 2009-06-08 .. fix minors bugs.
# Version 1.3 updated by Eric Gamache -- 2009-06-10 .. fix minors on SHORT type.
# Version 1.4 updated by Junjie Wu -- 2012-06-03 .. added api_key and precision support, removed deprecated timezone support.
# Version 1.5 updated by Eric Gamache -- 2020-06-01 .. Minor changes to support error 
#                                                      and validate IP address
#
# This script is provided "as is", with absolutely no warranty expressed or 
# implied. Any use is at your own risk. Permission to use or copy this 
# script for any purpose is hereby granted without fee. Permission to 
# modify the code and to distribute modified code is granted, provided 
# the above notices are retained, and a notice that the code was modified 
# is included with the above copyright notice. 
#
##############################################################################
#
# 1) Create a account at: https://ipinfodb.com/register
# 2) Get you API key by email
# 3) Please supply your own API key
# 
YOUR_API_KEY=""
#
##############################################################################
#
URL="http://api.ipinfodb.com/v3"
WGET_Option="-qnv --wait=3 --random-wait --waitretry=2 --limit-rate=9578 -O- "
WGET_Agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
#
ThisScriptName=`basename "$0"`
#
##############################################################################
## Default values...
  TYPE="raw"
  PREC="country"
  ERROR=0
##
##############################################################################
if [ "$YOUR_API_KEY" = "" ]; then
  echo " "
  echo "  Please edit the script ($ThisScriptName) to provide YOUR_API_KEY"
  exit 1
fi
##############################################################################
## *** TO DO *** IPV6 support
if [ "$1" = "" ] ; then
  ERROR=1
else
  chkValidIP=`echo "$1" | awk -F. '{if (($1<0) || ($1>255) || ($2<0) || ($2>255) || ($3<0) || ($3>255) || ($4<0) || ($4>255) || (length($1)>3) || (length($2)>3) || (length($3)>3) || (length($4)>3) || ($1 ~ /[^0-9]/) || ($2 ~ /[^0-9]/) || ($3 ~ /[^0-9]/) || ($4 ~ /[^0-9]/) ) { print "ERROR";}}'`
  #
  if [ "$chkValidIP" != "" ]; then
    ERROR=1
  else
    IP=$1
  fi
fi
##############################################################################
if [ "$2" != "" ]; then
  if [ "$2" != "raw" ] && [ "$2" != "json" ] && [ "$2" != "xml" ] && [ "$2" != "city" ] && [ "$2" != "country" ]; then
    ERROR=2
  else
    if [ "`echo $2 $3 | grep xml`" != "" ]; then
      TYPE="xml"
    fi
    if [ "`echo $2 $3 | grep json`" != "" ]; then
      TYPE="json"
    fi
    if [ "`echo $2 $3 | grep raw`" != "" ]; then
      TYPE="raw"
    fi
    if [ "`echo $2 $3 | grep city`" != "" ]; then
      PREC="city"
    fi
    if [ "`echo $2 $3 | grep country`" != "" ]; then
      PREC="country"
    fi
  fi
fi
##############
if [ "$3" != "" ]; then
  if [ "$3" != "raw" ] && [ "$3" != "json" ] && [ "$3" != "xml" ] && [ "$3" != "city" ] && [ "$3" != "country" ]; then
    ERROR=3
  fi
fi
##############################################################################
##
HTTPLink="$URL/ip-$PREC/?key=$YOUR_API_KEY&ip=$IP&format=$TYPE"
##
##############################################################################
if [ "$ERROR" != "0" ]; then
  if [ "$ERROR" = "1" ]; then
    echo "  '$1' isn't a valid IP address"
  fi
  if [ "$ERROR" = "2" ]; then
    echo "  '$2' isn't a valid parameter"
  fi
  if [ "$ERROR" = "3" ]; then
    echo "  '$3' isn't a valid parameter"
  fi
    echo " "
    echo " Usage  $ThisScriptName IP [TYPE] [PRECISION]"
    echo " "
    echo " Where  IP is the IP to check (ipv4 format)"
    echo "        TYPE is the output type (raw|json|xml) (def.: raw)"
    echo "        PRECISION is the request precision (country|city) (def.: country)"
    echo "           'country' provide faster reply from server"
    echo "              'city' add georeferencing data to request"
    echo "   Big thanks to the team of IP Location Tools (https://ipinfodb.com/)"
    exit 1
fi
##############################################################################
#
TST_wget=`wget > /dev/null 2>&1`
#
ErrorLevel=$?
#
if [ "$ErrorLevel" != 1 ] ; then
  echo "  ----"  
  echo "  wget   not found; please install it for proper operation."
  echo "  ----"  
  exit 1
fi
##############################################################################
#
# Found problem with this IP: 51.219.59.129 on the ContryName -----\
#                                                     space here---|
# OK;;51.219.59.129;GB;United Kingdom of Great Britain and Northern ;England;Sheffield;S1;53.383;-1.4659;+01:00                                                      
#
# awk '{$1=$1};1'      --> Remove leading & trailing spaces [full line]
# sed 's/\s;\|;\s/;/g' --> Remove leading & trailing spaces [';' separator]
#               ex.: data1 ;data2; data3; data4; --> data1;data2;data3;data4;
# sed s/\"/""/g        --> remove double quotes
#               ex.: "data1";"data2" --> data1;data2
#
if [ "$TYPE" = "raw" ]; then
    INFO_IP=`wget $WGET_Option --user-agent="$WGET_Agent" $HTTPLink | sed 's/\s;\|;\s/;/g' | awk '{$1=$1};1' 2>&1`
    StatusCode=`echo "$INFO_IP"    | awk -F ";" '{print $01}' `
    StatusMessage=`echo "$INFO_IP" | awk -F ";" '{print $02}' `
  ######
    StatsIPAddress=`echo "$INFO_IP"   | awk -F ";" '{print $03}' `
    StatsCountryCode=`echo "$INFO_IP" | awk -F ";" '{print toupper($04)}' `
    StatsCountryName=`echo "$INFO_IP" | awk -F ";" '{print $05}' `
    StatsRegionName=`echo "$INFO_IP"  | awk -F ";" '{print $06}' `
    StatsCityName=`echo "$INFO_IP"    | awk -F ";" '{print $07}' `
    StatsZipCode=`echo "$INFO_IP"     | awk -F ";" '{print toupper($08)}' `
    StatsLatitude=`echo "$INFO_IP"    | awk -F ";" '{print $09}' `
    StatsLongitude=`echo "$INFO_IP"   | awk -F ";" '{print $10}' `
    StatsTimeZone=`echo "$INFO_IP"    | awk -F ";" '{print $11}' `
    ######
    if [ $PREC = "city" ]; then
    INFO_IP="$StatusCode;$StatusMessage;$StatsIPAddress;$StatsCountryCode;$StatsCountryName;$StatsRegionName;$StatsCityName;$StatsZipCode;$StatsLatitude;$StatsLongitude;$StatsTimeZone"
  else
    INFO_IP="$StatusCode;$StatusMessage;$StatsIPAddress;$StatsCountryCode;$StatsCountryName"
  fi
  #######################
elif [ "$TYPE" = "json" ]; then
    INFO_IP=`wget $WGET_Option --user-agent="$WGET_Agent" $HTTPLink 2>&1`
    StatusCode=`echo "$INFO_IP"  | head -n 2 | tail -n 1 | awk -F ":" '{print substr($02,1,length($02)-1)}' | awk '{$1=$1};1' | sed s/\"/""/g`
  #######################
elif [ "$TYPE" = "xml" ]; then
    INFO_IP=`wget $WGET_Option --user-agent="$WGET_Agent" $HTTPLink | sed s/" ;"/";"/g 2>&1`
    StatusCode=`echo "$INFO_IP" | grep "<statusCode>" | sed 's/<statusCode>//g' | sed 's/<\/statusCode>//g' | awk '{$1=$1};1' `
fi
################################################################
##
echo "$INFO_IP"
##
################################################################
if [ "$StatusCode" != "OK" ]; then
  exit 1
else
  exit 0
fi
################################################################
  • 1
    Thanks for posting. Please summarize what you changed and why. We can't easily diff the two versions here so it would be helpful if you quoted the lines you want to change and explain what needs to be done. – User5910 Jun 04 '20 at 01:52
  • 1
    @User5910 , I am the original author of this script... It was a quite surprise for me to find it here, and somewhere in China and Russia ;) I added the summarize as per your request. – AdasOsorus Jun 04 '20 at 04:52
  • Seems like you have edited the script in a way, that it now takes IPs as arguments when called. That as an explanation and a short example on how to call it with a text file of IP addresses would make a great answer! :) – Senkaku Jun 04 '20 at 18:27
  • Usage **getiploc.sh IP [TYPE] [PRECISION]** getiploc.sh IP [TYPE] [PRECISION] Where IP is the IP to check (ipv4 format) TYPE is the output type (raw|json|xml) (def.: raw) PRECISION is the request precision (country|city) (def.: country) 'country' provide faster reply from server 'city' add georeferencing data to request Ex.: getiploc.sh 172.217.13.196 raw city – AdasOsorus Feb 22 '21 at 16:08