在DDNS情况下,我能否仅使用CNAME记录来重定向我的域而没有A记录?

时间:2019-02-27 01:58:42

标签: dynamic dns cname

我想我了解两者之间的区别以及何时使用CNAME进行正常重定向的区别,但是其他问题/答案无法回答我的特定情况,即:

我在家中无法获得主机的静态IP,因此不得不使用DDNS。假设我有mydomain.net,我想指向mydomain.ddns.net。我将CNAME记录放入DNS提供程序中,如下所示:

CNAME * .mydomain.net mydomain.ddns.net

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

我没有A记录,因为这只能暂时起作用,直到我的IP再次更改,因此CNAME记录是唯一的。我正在运行No-IP工具,因此mydomain.ddns.net的确会在发生更改时得到更新,因此效果很好。

当我尝试ping mydomain.net时,找不到主机,因此DNS无法正常工作,因此我怀疑我的CNAME条目有问题。我怀疑是因为我没有A记录,但找不到任何地方可以确认这一点。

谢谢。

2 个答案:

答案 0 :(得分:1)

*.example.net将不会捕获example.net。但是,正如您提到的那样,如果您对www.example.net进行ping操作,它将击中CNAME。

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

解决此问题的一种方法是使用具有API的提供程序,并直接更新example.net的A记录,而不是使用ddns.net。 Cloudflare就是这样的提供者之一,它免费提供DNS托管。 plenty中有guides条关于如何将其用作动态DNS的信息。

答案 1 :(得分:1)

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

它由Task Scheduler每10分钟执行一次,首先检查是否有Internet连接,如果有,请检查我的公共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: yourusername@hotmail.com" -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 ---"
相关问题