下面是一个 shell 脚本,我非常希望能够提供一个 IP 地址的 txt 文档来确定访问我的网站的国家/地区。
在文本文档中,我们将其称为“IPlist.txt”,可能看起来像:
123.123.123.123
111.111.111.111
222.222.22.222
我可以以某种方式修改下面的脚本来调用“IPlist.txt”并读出每一行并将其输入到 $IP 变量的位置吗?
#!/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"
我确信这可以直接完成。我对 grep/awk/sed/bash 等的了解有限。我希望有人能帮助我!
干杯,
詹姆士
答案1
您根本不需要修改脚本。只需在 bash 循环中运行它即可。例如:
$ while read ip; do IP_finding_script.sh $ip csv city; done < IPlist.txt
要将输出保存在文件中,请执行以下操作:
$ while read ip; do IP_finding_script.sh $ip csv city; done < IPlist.txt > outfile.txt
保存输出每个将 IP 输入到单独的文件中:
$ while read ip; do IP_finding_script.sh $ip csv city > $ip".out"; done < IPlist.txt
答案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
################################################################