我是否可以使用CNAME记录(仅限)在DDNS情况下重定向我的域而没有A记录?

问题描述 投票:0回答:2

我想我理解两者之间的区别以及何时使用CNAME正常重定向,但其他问题/答案不能回答我的具体情况:

我无法在家中为我的主机获取静态IP,因此被迫使用DDNS。假设我有mydomain.net,我想指向mydomain.ddns.net。我在我的DNS提供商中放置了一条CNAME记录,如下所示:

CNAME * .mydomain.net mydomain.ddns.net

我使用www和ftp因此使用通配符。

我没有A记录,因为这只会暂时起作用,直到我的IP再次更改,因此CNAME记录是唯一的记录。我运行了No-IP工具,因此mydomain.ddns.net会在更改时立即更新,这样做非常有效。

当我尝试ping mydomain.net时,它无法找到主机,因此DNS无效,因此我怀疑我的CNAME条目有问题。我怀疑这是因为我没有A-Record,但找不到任何地方来证实这一点。

提前致谢。

dynamic dns cname
2个回答
1
投票

*.example.net不会抓住example.net。但是,正如你所提到的,如果你ping www.example.net它将会击中CNAME。

遗憾的是,您不能在example.net上拥有CNAME,因为CNAME不允许与任何其他记录类型共存,而对于example.net,您至少会拥有NS类型的记录(指定您的名称服务器)。

解决这个问题的一种方法是使用具有API的提供程序而不是使用ddns.net,并直接更新example.net的A记录。一个这样的提供商可能是Cloudflare免费进行DNS托管。有关如何将它们用作动态DNS的plentyguides


1
投票

在@colde的灵感和一些工作之后,我想分享我的批处理脚本来实现他的答案。

它由Task Scheduler每10分钟执行一次,首先检查是否存在互联网连接,如果是,则检查我的公共IP是否发生了变化,只有这样,才能通过其API更新cloudflare DNS。

我已将我的个人标识符和API网址匿名,显然您需要更改这些标识符。

@echo off
cls
setlocal EnableExtensions EnableDelayedExpansion
set logFile=.\UpdatePublicIP.log
call :log "--- SCRIPT STARTED ---"
goto TestInternet

:log
echo [!date! !time!] %~1
echo [!date! !time!] %~1 >>%logFile%
exit /b 0

:TestInternet
REM First test for internet connectivity.
call :log "Detecting internet."
PING -n 1 8.8.8.8|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :CheckPublicIP
IF     ERRORLEVEL 1 goto :NoInternet

:NoInternet
call :log "No internet, nothing to do."
goto End

:CheckPublicIP
call :log "Detecting public IP."
for /f %%A in (
  'powershell -command "(Invoke-Webrequest "http://api.ipify.org").content"'
) do (
      set TempPublicIP=%%A
     )
call :log "Current Public IP: %TempPublicIP%"
if not "%TempPublicIP%"=="%PublicIP%" (
    call :log "Saved IP [%PublicIP%] different to current [%TempPublicIP%] IP, updating saved PublicIP."
    REM Note: setx saves env var but only available in future cmd windows, not current one.
    setx PublicIP %TempPublicIP% >nul
    goto UpdateDNS
    ) else (
            call :log "Current IP = saved IP, nothing to do."
           )
goto End

:UpdateDNS
call :log "Updating CloudFlare DNS record to [%TempPublicIP%]."
curl -X PUT "https://api.cloudflare.com/client/v4/zones/12345abcde12345abcde12345abcde12/dns_records/1234567890qwertyuiop0987654321ab" -H "X-Auth-Email: [email protected]" -H "X-Auth-Key:a123b4567c8defghijklmnopqrstuvwxyz123" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"yourdomainname.net\",\"content\":\"%TempPublicIP%\"}"|findstr.exe modified_on >nul
REM Can't use "success":true due to the quote. Assuming the string "modified_on" occurs on success only.
IF NOT ERRORLEVEL 1 goto :CloudFlareSuccess
IF     ERRORLEVEL 1 goto :CloudFlareError
goto End

:CloudFlareSuccess
call :log "CloudFlare DNS update succeeded.
goto End

:CloudFlareError
call :log "CloudFlare DNS update failed.
goto End

:End
call :log "--- SCRIPT FINISHED ---"
© www.soinside.com 2019 - 2024. All rights reserved.