首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过Post请求更新Google域名

通过Post请求更新Google域名
EN

Stack Overflow用户
提问于 2015-02-12 02:34:48
回答 3查看 1.9K关注 0票数 0

谷歌域名最近增加了对DDNS更新的支持。我对如何根据他们的需求发出Post请求有点迷惑。到底需要怎样才能通过?我知道我需要传递用户名、密码和域。

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

代码语言:javascript
复制
$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams

https://support.google.com/domains/answer/6147083?hl=en接口使用说明

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-07 09:07:26

这里是一个脚本,我修改了通过PowerShell更新谷歌域名DDNS。只需使用任务调度程序将其作为事件进行调度,或将其包含在批处理中。

代码语言:javascript
复制
#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
EN

Stack Overflow用户

发布于 2015-04-12 21:40:01

我将使用System.Net.WebClient连接到具有基本身份验证的网页

代码语言:javascript
复制
$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。

代码语言:javascript
复制
$cred = Get-Credential
Invoke-WebRequest -Uri "https://domains.google.com/nic/update?hostname=<subdomain.yourdomain.com>&myip=<1.2.3.4>" -Credential $cred
票数 0
EN

Stack Overflow用户

发布于 2018-08-13 04:31:53

修改了Tyler W的脚本

代码语言:javascript
复制
#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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28461844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档