Skip to content

Instantly share code, notes, and snippets.

@v12
Last active April 23, 2021 15:24

Revisions

  1. v12 revised this gist Feb 21, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions yandex-ddns
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@
    :error "[Ya.DNS] No IP address is assigned to the interface '$interfaceName'."
    }

    /tool fetch mode=https address=$updateHostIp host=$updateHost src-path=( $updateUrl . "get_domain_records" . $updateQueryGeneral ) dst-path="/YaDNS.txt"
    /tool fetch mode=https address=$updateHostIp host=$updateHost src-path=( $updateUrl . "get_domain_records" . $updateQueryGeneral ) dst-path="YaDNS.txt"
    :local result [/file get YaDNS.txt contents]

    # Checking whether request is successful or not
    @@ -115,8 +115,8 @@ if ($doneSearchingEntry && ([ :typeof $aRecordEntry ] != "str")) do={
    address=$updateHostIp \
    host=$updateHost \
    src-path=( $updateUrl . $queryAction . $updateQueryGeneral . $queryParams ) \
    dst-path="/YaDNS_2.txt"
    :set result [/file get YaDNS.txt contents]
    dst-path="YaDNS_2.txt"
    :set result [/file get YaDNS_2.txt contents]

    # Avoiding "no such item" bug: fetch result needs some time to be saved
    :delay 1000ms
  2. v12 revised this gist Feb 9, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions yandex-ddns
    Original file line number Diff line number Diff line change
    @@ -49,8 +49,8 @@
    }

    # Get current IP address for interface
    :local currentInterfaceIp [ /ip address get [/ip address find interface=$interfaceName] address ]
    :set currentInterfaceIp [ :pick [ :tostr $currentInterfaceIP ] 0 [ :find [ :tostr $currentInterfaceIP ] "/" ] ]
    :local currentInterfaceIp [ :tostr [ /ip address get [/ip address find interface=$interfaceName] address ] ]
    :set currentInterfaceIp [ :pick $currentInterfaceIp 0 [ :find $currentInterfaceIp "/" ] ]

    :if ([ :len $currentInterfaceIp ] = 0 ) do= {
    :log info "[Ya.DNS] No IP address is assigned to the interface '$interfaceName'."
  3. v12 revised this gist Feb 9, 2014. 1 changed file with 39 additions and 32 deletions.
    71 changes: 39 additions & 32 deletions yandex-ddns
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,12 @@
    # URL: https://gist.github.com/v12/8892066
    ########

    # There is no way of getting token using script, so you should get it manually: https://pddimp.yandex.ru/get_token.xml?domain_name=example.com

    ### Settings section

    # There is no way of getting token automatically via script, so you should get it manually: https://pddimp.yandex.ru/get_token.xml?domain_name=example.com
    # More info: http://api.yandex.ru/pdd/doc/reference/api-dns_get_token.xml
    :local YaDNStoken "12345678910123456789101234567891012345678910123456789101234567"
    :local apiToken "12345678910123456789101234567891012345678910123456789101234567"

    # Your domain delegated to Yandex
    :local YaDNSdomain "example.com"
    @@ -16,12 +19,15 @@
    :local YaDNSTTL "300"

    # Interface which IP address will be used for update
    :local YaDNSInterfaceName "ether1-gateway"
    :local interfaceName "ether1-gateway"

    :local YaDNShost "pddimp.yandex.ru"
    :local YaDNSurl "nsapi/"
    :local YaDNSqueryGeneral ( ".xml\?token=" . $YaDNStoken . "&domain=" . $YaDNSdomain )
    :local YaDNSqueryGeneralSub ( $YaDNSqueryGeneral . "&subdomain=" . $YaDNSsubdomain )
    :local updateHost "pddimp.yandex.ru"
    :local updateHostIp [ :resolve $updateHost ]
    :local updateUrl "nsapi/"
    :local updateQueryGeneral ( ".xml\?token=" . $apiToken . "&domain=" . $YaDNSdomain )
    :local updateQueryGeneralSub ( $updateQueryGeneral . "&subdomain=" . $YaDNSsubdomain )

    ### Settings section END

    :local findSubdomainEntry do={
    :local searchOffset 0
    @@ -35,30 +41,30 @@
    :return [ :pick $string ($searchEntrySubstrPos + [ :len $searchEntrySubstr ] ) [ :find $string "</record>" $searchEntrySubstrPos ] ]
    }

    :log info "[Ya.DNS] Attempting to update A record for $YaDNSsubdomain.$YaDNSdomain"

    :if ([ :len [ /interface find name=$YaDNSInterfaceName ] ] = 0 ) do={
    :log warning "[Ya.DNS] No interface '$YaDNSInterfaceName' was found, please do check updater script configuration."
    :error "[Ya.DNS] No interface '$YaDNSInterfaceName' was found, please do check updater script configuration."
    :if ([ :len [ /interface find name=$interfaceName ] ] = 0 ) do={
    :log warning "[Ya.DNS] No interface '$interfaceName' was found, please do check updater script configuration."
    :error "[Ya.DNS] No interface '$interfaceName' was found, please do check updater script configuration."
    }

    # Get current IP address for interface
    :local YaDNSCurrentIP [ /ip address get [/ip address find interface=$YaDNSInterfaceName] address ]
    :set YaDNSCurrentIP [ :pick [ :tostr $YaDNSCurrentIP ] 0 [ :find [ :tostr $YaDNSCurrentIP ] "/" ] ]
    :local currentInterfaceIp [ /ip address get [/ip address find interface=$interfaceName] address ]
    :set currentInterfaceIp [ :pick [ :tostr $currentInterfaceIP ] 0 [ :find [ :tostr $currentInterfaceIP ] "/" ] ]

    :if ([ :len $YaDNSCurrentIP ] = 0 ) do= {
    :log info "[Ya.DNS] No IP address is assigned to the interface '$YaDNSInterfaceName'."
    :error "[Ya.DNS] No IP address is assigned to the interface '$YaDNSInterfaceName'."
    :if ([ :len $currentInterfaceIp ] = 0 ) do= {
    :log info "[Ya.DNS] No IP address is assigned to the interface '$interfaceName'."
    :error "[Ya.DNS] No IP address is assigned to the interface '$interfaceName'."
    }

    /tool fetch mode=https address=[ :resolve $YaDNShost ] host=$YaDNShost src-path=( $YaDNSurl . "get_domain_records" . $YaDNSqueryGeneral ) dst-path="/YaDNS.txt"
    /tool fetch mode=https address=$updateHostIp host=$updateHost src-path=( $updateUrl . "get_domain_records" . $updateQueryGeneral ) dst-path="/YaDNS.txt"
    :local result [/file get YaDNS.txt contents]

    # Checking whether request is successful or not
    :local error [:pick $result ([:find $result "<error>"]+7) [:find $result "</error>"]]
    :if ($error != "ok") do={
    :log warning "[Ya.DNS] Unable to get list of records for $YaDNSdomain\r\n \
    [Ya.DNS] Error: $error"
    :error "Unable to get list of records for $YaDNSdomain\r\nError: $error"
    :log warning "[Ya.DNS] Unable to get list of records for $YaDNSdomain ($error)"
    :error "[Ya.DNS] Unable to get list of records for $YaDNSdomain ($error)"
    }

    # Searching an entry with the subdomain
    @@ -79,18 +85,19 @@ while (!$doneSearchingEntry) do={
    }
    }


    :local queryParams ( "&content=" . $YaDNSCurrentIP . "&ttl=" . $YaDNSTTL . "&subdomain=" . $YaDNSsubdomain )
    :local queryParams ( "&content=" . $currentInterfaceIp . "&ttl=" . $YaDNSTTL . "&subdomain=" . $YaDNSsubdomain )
    :local queryAction "add_a_record"

    if ($doneSearchingEntry && ([ :typeof $aRecordEntry ] != "str")) do={
    # Create new entry
    :log info "[Ya.DNS] Not found A entry for $YaDNSsubdomain.$YaDNSdomain. Attempting to create one."
    :put "[Ya.DNS] Not found A entry for $YaDNSsubdomain.$YaDNSdomain. Attempting to create one."
    } else={
    # Get the IP that is already assigned to the existing record
    :local currentRecordIP [ :toip [ :pick $aRecordEntry ([ :find $aRecordEntry ">" ] + 1) [ :len $aRecordEntry ] ] ]
    :local currentRecordIp [ :toip [ :pick $aRecordEntry ([ :find $aRecordEntry ">" ] + 1) [ :len $aRecordEntry ] ] ]
    # Compare current IP on our interface with the IP in the record
    :if ($currentRecordIP = $YaDNSCurrentIP) do={
    :if ($currentRecordIp = $currentInterfaceIp) do={
    :log info "[Ya.DNS] IP address hasn't changed since last update. Terminating."
    :error "[Ya.DNS] IP address hasn't changed since last update. Terminating."
    } else={
    # Searching id of entry we found
    @@ -100,27 +107,27 @@ if ($doneSearchingEntry && ([ :typeof $aRecordEntry ] != "str")) do={

    :set queryAction "edit_a_record"
    :set queryParams ( $queryParams . "&record_id=" . $recordId )
    :put "[Ya.DNS] Updating record #$recordId"
    }
    }

    /tool fetch \
    mode=https \
    address=[ :resolve $YaDNShost ] \
    host=$YaDNShost \
    src-path=( $YaDNSurl . $queryAction . $YaDNSqueryGeneral . $queryParams ) \
    address=$updateHostIp \
    host=$updateHost \
    src-path=( $updateUrl . $queryAction . $updateQueryGeneral . $queryParams ) \
    dst-path="/YaDNS_2.txt"
    :set result [/file get YaDNS.txt contents]

    # Avoiding "no such item" bug: fetch result needs some time to be saved
    :delay 300ms
    :delay 1000ms
    /file remove "YaDNS.txt"
    /file remove "YaDNS_2.txt"

    # Checking whether request is successful or not
    :set error [:pick $result ([:find $result "<error>"]+7) [:find $result "</error>"]]
    :if ($error != "ok") do={
    :log warning "[Ya.DNS] Unable to update record for $YaDNSsubdomain.$YaDNSdomain\r\n \
    [Ya.DNS] Error: $error"
    :error "Unable to update record for $YaDNSsubdomain.$YaDNSdomain\r\nError: $error"
    }
    :log warning "[Ya.DNS] Unable to update record for $YaDNSsubdomain.$YaDNSdomain ($error)"
    :error "[Ya.DNS] Unable to update record for $YaDNSsubdomain.$YaDNSdomain ($error)"
    }

    :log info "[Ya.DNS] Successfuly updated A record for $YaDNSsubdomain.$YaDNSdomain ($currentInterfaceIp)"
  4. v12 revised this gist Feb 9, 2014. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion yandex-ddns
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,18 @@
    # URL: https://gist.github.com/v12/8892066
    ########

    # There is no way of getting token using script, so you should get it manually: https://pddimp.yandex.ru/get_token.xml?domain_name=example.com
    # More info: http://api.yandex.ru/pdd/doc/reference/api-dns_get_token.xml
    :local YaDNStoken "12345678910123456789101234567891012345678910123456789101234567"

    # Your domain delegated to Yandex
    :local YaDNSdomain "example.com"
    # Subdomain which you're going to update (only subdomain, not the full hostname)
    # If you want to update A record for subdomain.example.com, you should set this variable to "subdomain"
    :local YaDNSsubdomain "subdomain"
    :local YaDNSTTL "300"


    # Interface which IP address will be used for update
    :local YaDNSInterfaceName "ether1-gateway"

    :local YaDNShost "pddimp.yandex.ru"
  5. v12 revised this gist Feb 8, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions yandex-ddns
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    ########
    # Name: yandex-ddns
    # Author: v12
    # URL: https://gist.github.com/v12/8892066
    ########

    :local YaDNStoken "12345678910123456789101234567891012345678910123456789101234567"

    :local YaDNSdomain "example.com"
  6. v12 created this gist Feb 8, 2014.
    114 changes: 114 additions & 0 deletions yandex-ddns
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    :local YaDNStoken "12345678910123456789101234567891012345678910123456789101234567"

    :local YaDNSdomain "example.com"
    :local YaDNSsubdomain "subdomain"
    :local YaDNSTTL "300"

    :local YaDNSInterfaceName "ether1-gateway"

    :local YaDNShost "pddimp.yandex.ru"
    :local YaDNSurl "nsapi/"
    :local YaDNSqueryGeneral ( ".xml\?token=" . $YaDNStoken . "&domain=" . $YaDNSdomain )
    :local YaDNSqueryGeneralSub ( $YaDNSqueryGeneral . "&subdomain=" . $YaDNSsubdomain )

    :local findSubdomainEntry do={
    :local searchOffset 0
    :if ([:typeof $1] = "num") do={ :set searchOffset $1 }
    :if ([:typeof $1] = "str") do={ :set searchOffset [ :tonum $1 ] }
    :local searchEntrySubstr ( "<record domain=\"" . $subdomain . "." . $domain . "\"" )
    :local searchEntrySubstrPos [ :find $string $searchEntrySubstr $searchOffset ]
    :if ([:typeof $searchEntrySubstrPos] != "num") do={
    :return false
    }
    :return [ :pick $string ($searchEntrySubstrPos + [ :len $searchEntrySubstr ] ) [ :find $string "</record>" $searchEntrySubstrPos ] ]
    }


    :if ([ :len [ /interface find name=$YaDNSInterfaceName ] ] = 0 ) do={
    :log warning "[Ya.DNS] No interface '$YaDNSInterfaceName' was found, please do check updater script configuration."
    :error "[Ya.DNS] No interface '$YaDNSInterfaceName' was found, please do check updater script configuration."
    }

    # Get current IP address for interface
    :local YaDNSCurrentIP [ /ip address get [/ip address find interface=$YaDNSInterfaceName] address ]
    :set YaDNSCurrentIP [ :pick [ :tostr $YaDNSCurrentIP ] 0 [ :find [ :tostr $YaDNSCurrentIP ] "/" ] ]

    :if ([ :len $YaDNSCurrentIP ] = 0 ) do= {
    :log info "[Ya.DNS] No IP address is assigned to the interface '$YaDNSInterfaceName'."
    :error "[Ya.DNS] No IP address is assigned to the interface '$YaDNSInterfaceName'."
    }

    /tool fetch mode=https address=[ :resolve $YaDNShost ] host=$YaDNShost src-path=( $YaDNSurl . "get_domain_records" . $YaDNSqueryGeneral ) dst-path="/YaDNS.txt"
    :local result [/file get YaDNS.txt contents]

    # Checking whether request is successful or not
    :local error [:pick $result ([:find $result "<error>"]+7) [:find $result "</error>"]]
    :if ($error != "ok") do={
    :log warning "[Ya.DNS] Unable to get list of records for $YaDNSdomain\r\n \
    [Ya.DNS] Error: $error"
    :error "Unable to get list of records for $YaDNSdomain\r\nError: $error"
    }

    # Searching an entry with the subdomain
    :local aRecordEntry
    :local doneSearchingEntry false
    :local entrySearchLastResult
    while (!$doneSearchingEntry) do={
    :set entrySearchLastResult [ $findSubdomainEntry subdomain=$YaDNSsubdomain domain=$YaDNSdomain string=$result ([ :find $result $entrySearchLastResult ] + 1) ]
    :if ([:typeof $entrySearchLastResult] = "str") do={
    :local currentEntryTypeSearch "type=\""
    :local currentEntryTypePos ([ :find $entrySearchLastResult $currentEntryTypeSearch ] + [ :len $currentEntryTypeSearch ])
    :if ([ :pick $entrySearchLastResult $currentEntryTypePos [ :find $entrySearchLastResult "\"" $currentEntryTypePos ] ] = "A") do={
    :set aRecordEntry $entrySearchLastResult
    :set doneSearchingEntry true
    }
    } else={
    :set doneSearchingEntry true
    }
    }


    :local queryParams ( "&content=" . $YaDNSCurrentIP . "&ttl=" . $YaDNSTTL . "&subdomain=" . $YaDNSsubdomain )
    :local queryAction "add_a_record"

    if ($doneSearchingEntry && ([ :typeof $aRecordEntry ] != "str")) do={
    # Create new entry
    :put "[Ya.DNS] Not found A entry for $YaDNSsubdomain.$YaDNSdomain. Attempting to create one."
    } else={
    # Get the IP that is already assigned to the existing record
    :local currentRecordIP [ :toip [ :pick $aRecordEntry ([ :find $aRecordEntry ">" ] + 1) [ :len $aRecordEntry ] ] ]
    # Compare current IP on our interface with the IP in the record
    :if ($currentRecordIP = $YaDNSCurrentIP) do={
    :error "[Ya.DNS] IP address hasn't changed since last update. Terminating."
    } else={
    # Searching id of entry we found
    :local recordIdSubstr "id=\""
    :local recordIdSubstrStart ( [ :find $aRecordEntry $recordIdSubstr ] + [ :len $recordIdSubstr ] )
    :local recordId [ :pick $aRecordEntry $recordIdSubstrStart [ :find $aRecordEntry "\"" $recordIdSubstrStart ] ]

    :set queryAction "edit_a_record"
    :set queryParams ( $queryParams . "&record_id=" . $recordId )
    :put "[Ya.DNS] Updating record #$recordId"
    }
    }

    /tool fetch \
    mode=https \
    address=[ :resolve $YaDNShost ] \
    host=$YaDNShost \
    src-path=( $YaDNSurl . $queryAction . $YaDNSqueryGeneral . $queryParams ) \
    dst-path="/YaDNS_2.txt"
    :set result [/file get YaDNS.txt contents]

    # Avoiding "no such item" bug: fetch result needs some time to be saved
    :delay 300ms
    /file remove "YaDNS.txt"
    /file remove "YaDNS_2.txt"

    # Checking whether request is successful or not
    :set error [:pick $result ([:find $result "<error>"]+7) [:find $result "</error>"]]
    :if ($error != "ok") do={
    :log warning "[Ya.DNS] Unable to update record for $YaDNSsubdomain.$YaDNSdomain\r\n \
    [Ya.DNS] Error: $error"
    :error "Unable to update record for $YaDNSsubdomain.$YaDNSdomain\r\nError: $error"
    }