5
4

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 を CLI で操作する

Last updated at Posted at 2021-11-30

1. python-cloudflare

こちらにある Python ライブラリを活用できます。

インストール

sudo pip install cloudflare

バージョン確認

% cli4 -V
Cloudflare library version: 2.8.15

設定ファイル作成

image-20211104155415629

image-20211104155603778

mkdir ~/.cloudflare
cat << EOS > ~/.cloudflare/cloudflare.cfg
[CloudFlare]
email = user@example.com # YOUR_EMAIL
token = 00000000000000000000000000000000 # YOUR_API_KEY
certtoken = v1.0-xxxxxxx # YOUR_ORIGIN_CA_KEY
extras =
EOS

参考:extras の追加

こちらの Python スクリプトを使って extras を追加しておくと、ほぼ全ての API コールをおこなえるようにできます。

list_cf_api_endpoint.py
# Import Library
from bs4 import BeautifulSoup
import urllib.request

# Set URL
url = "https://api.cloudflare.com/"
# Retrieve from URL
html = urllib.request.urlopen(url)
# Use BeautifulSoup
soup = BeautifulSoup(html, "html.parser")

# Retrieve the list of API endpoint
list_of_endpoints = []

for e in soup.find_all('pre',class_='language-http'):
    list_of_endpoints.append(e.get_text().split()[-1])

#list_of_endpoints.sort()
#for e in list_of_endpoints:
#    print(e)

# Get and show the unique value of the list above
list_of_unique_endpoints = list(set(list_of_endpoints))
list_of_unique_endpoints.remove('object/:object_id')
list_of_unique_endpoints.sort()
  
for e in list_of_unique_endpoints:
    print('    /client/v4/' + e)
python3 list_cf_api_endpoint.py >> ~/.cloudflare/cloudflare.cfg

コマンド例

cli4 man page - python3-cloudflare - General Commands

cli4 /zones # List information for all zones.
cli4 --raw per_page=100 /zones # default paging is {"count":20,"page":1,"per_page":20}
cli4 /zones/:example.com # List specific zone info.
cli4 /zones/:example.com/settings # List settings for a specific zone.
cli4 --delete purge_everything=true /zones/:example.com/purge_cache # Purge cache for a specific zone.
cli4 --delete files='[http://example.com/css/styles.css]' /zones/:example.com/purge_cache # Purge cache for a specific zone.
cli4 --delete files='[http://example.com/css/styles.css,http://example.com/js/script.js] /zones/:example.com/purge_cache # Purge cache for a specific zone.
cli4 --delete tags='[tag1,tag2,tag3]' /zones/:example.com/purge_cache # Purge cache for a specific zone.
cli4 /zones/:example.com/available_plans # List available plans for a zone.
cli4 --patch status=active /zones/:example.com/dnssec # Make DNSSEC active for specfic zone.
cli4 /zones/:example.com/dnssec # List DNSSEC information and status for a specific zone.

ゾーンの設定値一覧を取得

このような形で取得しておくと、パラメータシート等で利用しやすいです。

cli4 /zones/:example.com/settings | jq -r '.[] | [.editable,.id,.modified_on,.value|tostring]|@csv'
result.csv
"true","0rtt","null","off"
"false","advanced_ddos","null","on"
"true","always_online","2021-07-01T15:11:49.794076Z","on"
"true","always_use_https","2021-09-01T03:49:14.208475Z","on"
"true","automatic_https_rewrites","null","off"
"true","brotli","2021-09-06T05:35:03.436837Z","on"
"true","browser_cache_ttl","null","14400"
"true","browser_check","2021-08-23T13:56:31.228728Z","off"
"true","cache_level","null","aggressive"
"true","challenge_ttl","null","1800"
"true","ciphers","null","[""ECDHE-ECDSA-AES128-GCM-SHA256""]"
"true","cname_flattening","null","flatten_all"
"true","development_mode","null","off"
"true","edge_cache_ttl","null","7200"
"true","email_obfuscation","null","on"
"true","filter_logs_to_cloudflare","null","off"
"true","hotlink_protection","null","off"
"true","http2","null","on"
"true","http3","2021-08-23T07:21:11.227618Z","on"
"true","ip_geolocation","null","on"
"true","ipv6","2021-07-02T14:32:19.338095Z","off"
"true","log_to_cloudflare","null","on"
"true","max_upload","2021-10-04T04:27:00.936383Z","500"
"true","min_tls_version","null","1.0"
"true","minify","2021-09-06T05:35:43.295595Z","{""css"":""on"",""html"":""on"",""js"":""on""}"
"true","mirage","2021-08-10T05:16:21.602454Z","on"
"true","mobile_redirect","null","{""mobile_subdomain"":null,""status"":""off"",""strip_uri"":false}"
"true","opportunistic_encryption","null","off"
"true","opportunistic_onion","null","on"
"true","orange_to_orange","null","off"
"true","origin_error_page_pass_thru","null","off"
"true","polish","2021-09-03T02:26:58.070012Z","lossy"
"true","prefetch_preload","null","off"
"true","privacy_pass","null","on"
"true","proxy_read_timeout","null","100"
"true","pseudo_ipv4","2021-07-29T04:15:51.230691Z","add_header"
"true","response_buffering","null","off"
"true","rocket_loader","2021-08-24T01:40:37.546320Z","on"
"true","security_header","null","{""strict_transport_security"":{""enabled"":false,""include_subdomains"":false,""max_age"":0,""nosniff"":false,""preload"":false}}"
"true","security_level","null","medium"
"true","server_side_exclude","null","on"
"true","sort_query_string_for_cache","null","off"
"true","ssl","2021-09-01T03:50:07.962873Z","full"
"true","tls_1_2_only","null","off"
"true","tls_1_3","null","on"
"true","tls_client_auth","2021-10-14T07:34:31.711373Z","on"
"true","true_client_ip_header","null","off"
"true","visitor_ip","null","on"
"true","waf","2021-08-04T05:45:29.377270Z","off"
"true","webp","2021-09-03T02:21:10.978032Z","on"
"true","websockets","null","on"

2. flarectl

こちらにある GO ライブラリを活用できます。

Release Latest · cloudflare/cloudflare-go

インストール

latest バージョンをインストールします。

pip install gh-release-install
gh-release-install "cloudflare/cloudflare-go" \
"flarectl_{version}_macos_amd64.tar.xz" \
--extract "flarectl" \
"/usr/local/bin/flarectl"

バージョン確認

% flarectl --version
flarectl version 0.27.0

環境変数設定

export CF_API_EMAIL='YOUR_EMAIL'
export CF_API_KEY='YOUR_APIKEY'

コマンド例

CLI を使って DNS レコード等の操作ができます。

% flarectl dns list --zone example.com
                 ID                | TYPE  |                NAME                 |                        CONTENT                        | PROXIED | TTL  
-----------------------------------+-------+-------------------------------------+-------------------------------------------------------+---------+------
  xxx | A     | a.example.com              | x.x.x.x                                          | true    |   1  
  xxx | A     | b.example.com            | x.x.x.x                                          | true    |   1  
  xxx | A     | c.example.com   | x.x.x.x                                          | true    |   1  
5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?