LoginSignup
1
0

Cloudflareでの大量IPアドレスの一括更新

Posted at

ステップ1: PythonをインストールするためにVPS上のSSHにログインする

まだPythonがない場合は、以下のコマンドを使う

sudo dnf install python3

次に、最新版に更新するためにYesを選択する

ステップ2: Requestsのインストール: Python内でrequestsライブラリがインストールされていることを確認する。

pip install requests

ステップ3: .pyファイルを作成する。cloudflare_dns_update.pyという名前のファイルを以下のコマンドで作成する

nano cloudflare_dns_update.py

その後、nanoの環境内で以下のコードを貼り付ける

import requests

# APIとドメインの情報
api_key = 'YOUR_CLOUDFLARE_API_KEY'
email = 'telegramweb.vn@gmail.com'
old_ip = '96.30.197.xxx'
new_ip = '103.72.96.xxx'

# リクエスト用のヘッダー
headers = {
    'X-Auth-Email': email,
    'X-Auth-Key': api_key,
    'Content-Type': 'application/json',
}

# DNSレコードを更新するための関数
def update_dns_record(zone_id, record_id, record_type, record_name, record_content):
    url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}'
    data = {
        'type': record_type,
        'name': record_name,
        'content': record_content
    }
    response = requests.put(url, headers=headers, json=data)
    return response.json()

# DNSレコードのリストを取得する関数
def get_dns_records(zone_id):
    url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records?type=A'
    response = requests.get(url, headers=headers)
    return response.json()

# ゾーンのリストを取得する関数
def get_zones():
    url = 'https://api.cloudflare.com/client/v4/zones'
    response = requests.get(url, headers=headers)
    return response.json()

# メインスクリプト
if __name__ == '__main__':
    zones = get_zones()
    for zone in zones['result']:
        dns_records = get_dns_records(zone['id'])
        for record in dns_records['result']:
            if record['content'] == old_ip:
                result = update_dns_record(zone['id'], record['id'], 'A', record['name'], new_ip)
                print(f"Updated record {record['name']} in zone {zone['name']} with result: {result}")

api_key = 'YOUR_CLOUDFLARE_API_KEY' これは前のステップで取得するAPIキーです(準備部分を参照してください)
email = 'fpt@gmail.com' > Cloudflareのログイン用メールアドレス
old_ip = '96.30.197.xxx' > 古いVPSのIPアドレス
new_ip = '103.72.96.xxx' > 新しいVPSのIPアドレス
これらの情報は、最適ですが、何かテキストファイルにコピーして、きれいに整理した後、nanoにコピーすることをお勧めします。

1
0
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
1
0