Google Domains недавно добавила поддержку обновления DDNS. Я немного не понимаю, как сделать запрос 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
0
user3692274 11 Фев 2015 в 21:34

3 ответа

Лучший ответ

Вот сценарий, который я модифицировал для обновления DDNS Google Domains через PowerShell. Просто запланируйте это как событие с помощью планировщика задач или включите в пакет.

#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 /

1
Tyler W 7 Июл 2015 в 01:07

Модифицированный сценарий Тайлера В.

#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
}
0
EvansC 12 Авг 2018 в 20:31

Я бы использовал 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, вам нужно будет использовать 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
0
John Burdick 12 Апр 2015 в 13:40