IP GeoLocator. Нужен включенный скрипт для получения источника IP-адресов из текстового документа

482
James

Ниже приведен сценарий оболочки, и мне бы очень хотелось, чтобы я мог передавать текстовые 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 и т.д. в целом. Я надеюсь, что кто-то может прийти мне на помощь!

Ура,

Джеймс

2

1 ответ на вопрос

3
terdon

Вам не нужно изменять скрипт вообще. Просто запустите его в цикле 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 
Вы чемпионский товарищ! тердон хорошие вещи! Я знал, что это будет просто, просто не знал как! идеально! Я предполагаю, что если я добавлю "> output.txt" в конец, я должен сохранить их? Спасибо! James 11 лет назад 1
@James Cheers :) Да, добавление "> output.txt" сохранит их. Ответ обновлен соответственно. terdon 11 лет назад 0
Прочитайте [здесь] (http://mywiki.wooledge.org/BashFAQ/001), чтобы узнать о предпочтительных способах чтения файла. Короткая версия: используйте перенаправление ввода с циклом while и не используйте `for`. chepner 11 лет назад 1
@chepner Достаточно верно, +1. Ответ обновлен соответственно. В свою защиту, я _did_ знаю, что входной файл содержит IP-адреса. terdon 11 лет назад 0

Похожие вопросы