Skip to content

Instantly share code, notes, and snippets.

@MarshalW
Last active September 8, 2023 12:14
Show Gist options
  • Save MarshalW/99ffa9a426fb47957c039f442c7c0444 to your computer and use it in GitHub Desktop.
Save MarshalW/99ffa9a426fb47957c039f442c7c0444 to your computer and use it in GitHub Desktop.
dnspod创建A记录

dnspod创建A记录

#!/usr/bin/env bash

set -e

DNSPOD_LOGIN_TOKEN="id,token"
public_ip=10.100.1.2
domain=your-domain.com

names=(
    "www"
    "git"
)

for name in "${names[@]}"; do
    # 查找已存在的记录
    response=$(curl -s -X POST https://dnsapi.cn/Record.List -d \
        "login_token=$DNSPOD_LOGIN_TOKEN&format=json&domain=$domain&sub_domain=$name&record_type=A&offset=0&length=10")

    if echo $response | grep -q records; then
        echo "has records"
        record_ids=$(echo $response | jq -r .records[].id)
        # 删除已经存在的记录
        echo $record_ids | xargs -n 1 -I % curl -X POST https://dnsapi.cn/Record.Remove -d \
            "login_token=$DNSPOD_LOGIN_TOKEN&format=json&domain=$domain&record_id=%"
    fi

    response=$(curl -s -X POST https://dnsapi.cn/Record.Create -d \
        "login_token=$DNSPOD_LOGIN_TOKEN&format=json&domain=$domain&sub_domain=$name&record_type=A&record_line=默认&value=$public_ip")

    code=$(echo $response | jq -r .status.code)

    echo

    if [[ $code -eq 1 ]]; then
        echo "add $name ok."
    else
        echo "add $name error"
    fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment