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 :(得分:1)
以下是我修改过的脚本,可通过PowerShell更新Google Domains 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/
答案 1 :(得分:0)
我会使用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作为用户名:password。
$cred = Get-Credential
Invoke-WebRequest -Uri "https://domains.google.com/nic/update?hostname=<subdomain.yourdomain.com>&myip=<1.2.3.4>" -Credential $cred
答案 2 :(得分:0)
修改了泰勒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
}