ipinfo.ioを使ってIPアドレスリストから国情報を抽出する
概要
更新履歴
- 2024/02/29 初版
目次
はじめに
このドキュメントは不正アクセスをおこなってきたIPアドレスリストを元に,そのIPアドレスがどこの国に所属するものなのかコマンドで調べて国情報を記録するプログラムの作成から実行までの記録である.プログラム生成にはChatGPTを利用している.
ipinfo.ioを使う
- IPアドレスの詳細を調べたい場合,digコマンドではなくてipinfo.ioにHTTPアクセスして情報を取得する.
- 例えば次のような感じ.
- このように調べたいIPアドレスの詳細を調べる.
$ curl -s ipinfo.io/14.11.13.160🆑
{
"ip": "14.11.13.160",
"hostname": "m014011013160.v4.enabler.ne.jp",
"city": "Nagoya",
"region": "Aichi",
"country": "JP",
"loc": "35.1815,136.9064",
"org": "AS2516 KDDI CORPORATION",
"postal": "460-0002",
"timezone": "Asia/Tokyo",
"readme": "https://ipinfo.io/missingauth"
}
$
$
プログラムを用意する
ChatGPTへのメッセージ
- 次のような文書でプログラム作成を依頼.
- IPアドレス1行ごとに複数の記録されたファイルを読み込んで,コマンドのcurl -s
ipinfo.io/の後ろに読み込んだIPアドレスをつけて実行するプログラムを作成し,結果がJSON形式で出力されるので,XMLの中からIPアド
レスとcountryを一件1行でCSV形式で作り,全ての結果を1つのファイルresult.txtに作成するbashのプログラムを作成してくださ
い.
- 今みたら作文,間違っているなぁ...
- それでも生成されたコマンドは次の通り.
#!/bin/bash
# 入力ファイルパス
input_file="input.txt"
# 結果ファイルパス
output_file="result.txt"
# 空の結果ファイルを作成
> "$output_file"
# 入力ファイルを1行ずつ読み込む
while IFS= read -r ip_address; do
# curlコマンドを使ってIP情報を取得し、JSONから必要な情報を抽出し、CSV形式で出力
curl_result=$(curl -s "ipinfo.io/$ip_address")
country=$(echo "$curl_result" | jq -r '.country' | head -n 1)
echo "$ip_address,$country" >> "$output_file"
done < "$input_file"
- JSON形式なのでjqコマンドでパースしている.
- コマンドが入ってないのでHomeBrewでインストール.
jqをインストールする
-
ipinfo.ioの出力結果がJSON形式で出力されるので,その中からデータを取り出すためにjqコマンドをインストールする.
$ brew install jq🆑
==> Downloading https://formulae.brew.sh/api/formula.jws.json
####################################################### 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
####################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/jq/manifests/1.7.1
####################################################### 100.0%
==> Fetching dependencies for jq: oniguruma
==> Downloading https://ghcr.io/v2/homebrew/core/oniguruma/manifests/6.9.9
####################################################### 100.0%
==> Fetching oniguruma
==> Downloading
https://ghcr.io/v2/homebrew/core/oniguruma/blobs/sha256:86b7b52451edba60d365586a975d1eb40f7823992565dde5c88abb97fde483d4
####################################################### 100.0%
==> Fetching jq
==> Downloading
https://ghcr.io/v2/homebrew/core/jq/blobs/sha256:449c76665ac72b34daeb1a09dd19217e3be1e723c63ec3ac88e02b8c9a750f34
####################################################### 100.0%
==> Installing dependencies for jq: oniguruma
==> Installing jq dependency: oniguruma
==> Downloading https://ghcr.io/v2/homebrew/core/oniguruma/manifests/6.9.9
Already downloaded:
/Users/ujpadmin/Library/Caches/Homebrew/downloads/35140c4d3995b75388bed026ef6d0acbb4d6076047cdcd895bfd996c0c8d6487--oniguruma-6.9.9.bottle_manifest.json
==> Pouring oniguruma--6.9.9.monterey.bottle.tar.gz
🍺 /usr/local/Cellar/oniguruma/6.9.9: 14 files, 1.4MB
==> Installing jq
==> Pouring jq--1.7.1.monterey.bottle.tar.gz
🍺 /usr/local/Cellar/jq/1.7.1: 19 files, 1.4MB
==> Running `brew cleanup jq`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
$ which jq🆑
/usr/local/bin/jq
$
- これで準備ができたので,実行する.
国情報を取り出すプログラムを実行する
生成されたプログラムを実行する.
$ cat ipcountry.sh🆑
#!/bin/bash
# 入力ファイルパス
input_file="input.txt"
# 結果ファイルパス
output_file="result.txt"
# 空の結果ファイルを作成
> "$output_file"
# 入力ファイルを1行ずつ読み込む
while IFS= read -r ip_address; do
# curlコマンドを使ってIP情報を取得し、JSONから必要な情報を抽出し、CSV形式で出力
curl_result=$(curl -s "ipinfo.io/$ip_address")
country=$(echo "$curl_result" | jq -r '.country' | head -n 1)
echo "$ip_address,$country" >> "$output_file"
done < "$input_file"
$ ./ipcountry.sh🆑
$
- 実行結果を確認.
$ head -n 10 result.txt🆑
122.131.100.32,JP
222.148.179.164,JP
49.96.236.122,JP
36.14.98.197,JP
180.44.74.25,JP
114.148.188.10,JP
116.64.99.17,JP
111.102.187.1,JP
106.139.100.200,JP
126.99.209.47,JP
$
大量アクセス時のメッセージ
- 未登録ユーザのまま利用すると,2000件を超えたあたりから結果が失敗する.
- そしてレスポンスの中身が次のような結果になっている.
$ curl -s ipinfo.io/216.58.220.132
{
"status": 429,
"error": {
"title": "Rate limit exceeded",
"message": "You've hit the daily limit for the
unauthenticated API. Create an API access token by signing up to
get 50k req/month."
}
}
$
- 「未認証APIの1日あたりの制限に達しました。 サインアップしてAPIアクセストークンを作成し、50k req/monthを取得してください。」とのこと.