通过 Post 请求更新 Google 个域

Update Google Domains via Post request

Google Domains 最近添加了对 DDNS 更新的支持。我对如何根据他们的要求提出 Post 请求有点迷茫。事情究竟需要怎样通过?我知道我需要传递用户名、密码和域。

示例 post 我将使用的字符串:

$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams

有关如何使用 API 的详细信息 https://support.google.com/domains/answer/6147083?hl=en

POST /nic/update?hostname=subdomain.yourdomain.com&myip=1.2.3.4 HTTP/1.1 
Host: domains.google.com 
Authorization: Basic base64-encoded-auth-string User-Agent: Chrome/41.0
your_email@yourdomain.com

我会使用 System.Net.WebClient 通过基本身份验证连接到网页

$username="<username>"
$password="<password>"
$url="https://domains.google.com/nic/update?hostname=<subdomain.yourdomain.com>&myip=<1.2.3.4>"
$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password)
$webpage = $webclient.DownloadString($url)

或者如果您想使用 Invoke-WebRequest,您将需要为 username:password 使用 get-credential。

$cred = Get-Credential
Invoke-WebRequest -Uri "https://domains.google.com/nic/update?hostname=<subdomain.yourdomain.com>&myip=<1.2.3.4>" -Credential $cred

这是我修改的脚本,用于通过 PowerShell 更新 Google 域 DDNS。只需使用任务计划程序将其安排为事件或将其包含在批处理中。

#Your info goes here
$hostname = "yourhostname.com"
$user = "your_generated_dns_username"
$pwd = "your_generated_dns_password"
$pair = "$($user):$($pwd)"


#Get a page with your current IP
$MyIpPage = Invoke-WebRequest "https://domains.google.com/checkip"

#Make sure we got a IP back in the response
If ($MyIpPage.RawContent -match "(?:[0-9]{1,3}.){3}[0-9]{1,3}")
{
    #encode the username and password for the header
    $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
    $base64 = [System.Convert]::ToBase64String($bytes)

    $basicAuthValue = "Basic $base64"

    $headers = @{ Authorization =  $basicAuthValue }

    #Build up the URL
    $url = "https://domains.google.com/nic/update?hostname={0}&myip={1}" -f $hostname, $MyIpPage

    #Invoke the URL
    $resp = Invoke-WebRequest -Uri $url -Headers $headers
    $resp.Content #Expected answers that I found "good","nochg","nohost","badauth","notfqdn"
}
Else
{
 #fake response if we didn't get any IP
 "No IP"
}

原脚本来源:http://powershell.today/2014/03/powershell-and-dyndns-at-loopia/

修改了 Tyler W 的剧本

#Google Domains API information: https://support.google.com/domains/answer/6147083?hl=en
#Your Google Domains Dynamic DNS info goes here
$hostname = "HOSTNAMEHERE"
$user = "USERNAMEHERE"
$pwd = "PASSWORDHERE"
$pair = "$($user):$($pwd)"

#Get the domain to IP resolution
$MyDNS = (Resolve-DnsName $hostname).IPAddress
#Make sure we got a IP back in the response
If ($MyDNS -match "(?:[0-9]{1,3}.){3}[0-9]{1,3}")
{
    Write-Host "Current IP Address for $hostname`: $MyIp" -ForegroundColor Cyan
}
Else
{
    Write-Warning "No IP Recieved!"
}
#Get a your current IP
$MyIp = (Invoke-WebRequest "https://domains.google.com/checkip").Content
#Make sure we got a IP back in the response
If ($MyIp -match "(?:[0-9]{1,3}.){3}[0-9]{1,3}")
{
    $NameHost = (Resolve-DnsName $MyIp).NameHost
    Write-Host "Current IP Address for $NameHost`: $MyIp" -ForegroundColor Cyan
}
Else
{
    Write-Warning "No IP Recieved!"
}
If ($MyDNS -ne $MyIP)
{
    #encode the username and password for the header
    $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
    $base64 = [System.Convert]::ToBase64String($bytes)
    $basicAuthValue = "Basic $base64"
    $headers = @{ Authorization =  $basicAuthValue }

    #Build up the URL
    $url = "https://domains.google.com/nic/update?hostname={0}&myip={1}" -f $hostname, $MyIp

    #Invoke the URL
    $resp = Invoke-WebRequest -Uri $url -Headers $headers
    # 
    switch -Wildcard ($resp.Content)
    {
       "good*" {Write-Host "The update was successful!" -ForegroundColor Green}
       "nochg*" {Write-Host "The supplied IP address $MyIp is already set for this host." -ForegroundColor Green}
       "nohost*" {Write-Warning "The hostname does not exist, or does not have Dynamic DNS enabled. `nHostname: $hostname"}
       "badauth*" {Write-Warning "The username / password combination is not valid for the specified host! `nUsername: $user`nPassword: $pwd"}
       "notfqdn*" {Write-Warning "The supplied hostname is not a valid fully-qualified domain name! `nHostname: $hostname"}
       "badagent*" {Write-Warning "Your Dynamic DNS client is making bad requests. Ensure the user agent is set in the request, and that you’re only attempting to set an IPv4 address. IPv6 is not supported."}
       "abuse*" {Write-Warning "Dynamic DNS access for the hostname has been blocked due to failure to interpret previous responses correctly."}
       "911*" {Write-Warning "An error happened on our end. Wait 5 minutes and retry."}
    }
}
Else
{
    Write-Host "DNS resolution and the current IP match, no need to update!" -ForegroundColor Green
}