2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cloudflare の IP を判定するコマンド

Posted at

概要

対象のドメインが Cloudflare を使っているかを判定して、
Cloudflare を使っていた場合には、その Cloudflare IP を返すコマンドを関数定義とエイリアスで作ります。

grepcidr インストール

macOS では brew でインストールできます。

brew install grepcidr

grepcidr 使い方

入力が IP レンジ内であれば、該当する IP を返します。なければ何も返しません。

% grepcidr 104.16.0.0/13 <(echo "104.17.110.184")
104.17.110.184
% grepcidr 104.16.0.0/13 <(echo "192.168.1.1") 

Cloudflare IP の該当確認

IP Ranges | Cloudflare で公開されている IP レンジに関して、確認するには以下のコマンドになります。

% grepcidr "$(curl -fsSL https://www.cloudflare.com/ips-v4) $(curl -fsSL https://www.cloudflare.com/ips-v6)" <(echo "172.70.49.134")
172.70.49.134

関数定義とエイリアス

名前解決の結果得られる IP アドレスを元にしたコマンドを ~/.zshrc に関数で定義し、エイリアスを設定します。

~/.zshrc
function checkcfip() {
    value=$(grepcidr "$(curl -fsSL https://www.cloudflare.com/ips-v4) $(curl -fsSL https://www.cloudflare.com/ips-v6)" <(echo "$(dig $1 +short A)\n$(dig $1 +short AAAA)"))
    if [ -z "$value" ] 
    then echo "not found"
    else echo $value
    fi
}
alias checkcfip=checkcfip

現在のターミナルセッションに設定を反映するには、以下のコマンドを実行します。

source ~/.zshrc 

checkcfip コマンド確認

% checkcfip dash.cloudflare.com
104.17.110.184
104.17.111.184
2606:4700::6811:6eb8
2606:4700::6811:6fb8
% checkcfip blog.cloudflare.com
104.18.41.174
172.64.146.82
2606:4700:4400::6812:29ae
2606:4700:4400::ac40:9252
% checkcfip google.com         
not found

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?