Advertisement
Klajnor

UpdateYaDNS

Feb 7th, 2014
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. # Script Name: UpdateYaDNS
  2. # Created by Klajnor 16.05.2013
  3. # Last update by Klajnor 21.05.2013
  4. # Last update by Klajnor 22.05.2013
  5. # Last update by Klajnor 31.05.2013: Update subdomain record
  6. # Last update by Klajnor 07.02.2014: /ip address remove [ find dynamic=yes invalid=yes ]
  7. # Last update by TerAnYu for HabraHabr 22.06.2014 small fix
  8. # Tested on 6.0rc14 and 6.0-6.15
  9.  
  10. # Set needed variables
  11.  
  12. #Root domain
  13. :local YaDNSdomain "klajnor.net"
  14. :local YaDNSsubdomain "klajnor.net"
  15.  
  16. # read http://api.yandex.ru/pdd/doc/api-pdd/reference/api-dns_get_token.xml#api-dns_get_token
  17. :local YaDNStoken "132456789012345678901234567890"
  18.  
  19. # read http://api.yandex.ru/pdd/doc/api-pdd/reference/api-dns_edit_a_record.xml
  20. :local YaDNSrecordid "1234567"
  21. :local YaDNSTTL "300"
  22.  
  23. :global YaDNSForceUpdateOnce
  24. :global YaDNSPreviousIP
  25. :local YaDNSInterfaceName "PPPoE_NBN"
  26. :local YaDNSDomainRecord
  27.  
  28. # get the current IP address from the interface
  29.  
  30. :if ([:len [/interface find name=$YaDNSInterfaceName]] = 0 ) do={
  31. :log info "UpdateYaDNS: No interface named $YaDNSInterfaceName , please check configuration."
  32. :error "UpdateYaDNS: No interface named $YaDNSInterfaceName , please check configuration."
  33. }
  34.  
  35. :if ([:typeof $YaDNSPreviousIP] = "nothing" ) do={ :global YaDNSPreviousIP 0.0.0.0 }
  36.  
  37. /ip address remove [ find dynamic=yes invalid=yes ]
  38. :local YaDNSYaDNSCurrentIPMask [ /ip address get [/ip address find interface=$YaDNSInterfaceName !disabled] address ]
  39.  
  40. :local YaDNSCurrentIP [:pick $YaDNSYaDNSCurrentIPMask 0 [:find $YaDNSYaDNSCurrentIPMask "/"]]
  41.  
  42. :if ([ :typeof $YaDNSCurrentIP ] = "nothing" ) do= {
  43. :log info "UpdateDynDNS: No ip address present on $YaDNSInterfaceName, please check."
  44. :error "UpdateDynDNS: No ip address present on $YaDNSInterfaceName, please check."
  45. }
  46.  
  47. :local YaDNSsrcpath1 ( "nsapi/get_domain_records.xml\?token=" . $YaDNStoken . "&domain=" . $YaDNSdomain )
  48.  
  49. :local YaDNSAPI [:resolve "pddimp.yandex.ru"]
  50. /tool fetch mode=https address="$YaDNSAPI" host="pddimp.yandex.ru" src-path=$YaDNSsrcpath1 dst-path="/YaDNSGetDomainRecord.txt"
  51.  
  52. :local Result1 [/file get YaDNSGetDomainRecord.txt contents]
  53. :local Result2 [:pick $Result1 ([:find $Result1 "id=\"$YaDNSrecordid"]) ([:find $Result1 "id=\"$YaDNSrecordid"]+42) ]
  54. :set YaDNSDomainRecord [:pick $Result2 ([:find $Result2 ">"] + 1) ( [:find $Result2 "<"] ) ]
  55.  
  56. :if (($YaDNSForceUpdateOnce or ($YaDNSCurrentIP != $YaDNSPreviousIP) or ($YaDNSCurrentIP != $YaDNSDomainRecord)) = true) do={
  57.  
  58. :log info "UpdateYaDNS: Try Update"
  59.  
  60. :log info "UpdateYaDNS: YaDNSForceUpdateOnce = $YaDNSForceUpdateOnce"
  61. :log info "UpdateYaDNS: YaDNSPreviousIP = $YaDNSPreviousIP"
  62. :log info "UpdateYaDNS: YaDNSCurrentIP = $YaDNSCurrentIP"
  63. :log info "UpdateYaDNS: YaDNSDomainRecord = $YaDNSDomainRecord"
  64.  
  65. :local YaDNSsrcpath2 ( "nsapi/edit_a_record.xml\?token=" . $YaDNStoken . "&domain=" . $YaDNSdomain . "&record_id=" . $YaDNSrecordid . "&ttl=" . $YaDNSTTL . "&content=" . $YaDNSCurrentIP )
  66.  
  67. if ( $YaDNSdomain != $YaDNSsubdomain ) do={ set YaDNSsrcpath2 ($YaDNSsrcpath2 . "&subdomain=" . $YaDNSsubdomain) }
  68.  
  69. :local YaDNSAPI [:resolve "pddimp.yandex.ru"]
  70.  
  71. /tool fetch mode=https address="$YaDNSAPI" host="pddimp.yandex.ru" src-path=$YaDNSsrcpath2 dst-path="/YaDNS.txt"
  72. :local result [/file get YaDNS.txt contents]
  73.  
  74. :global YaDNSResult [:pick $result ([:find $result "<error>"]+7) [:find $result "</error>"]]
  75.  
  76. :if ( $YaDNSResult = "ok" ) do={
  77. :set YaDNSForceUpdateOnce false
  78. :set YaDNSPreviousIP $YaDNSCurrentIP
  79. :log info "UpdateYaDNS: Update Success"
  80. }
  81.  
  82. :log info "UpdateYaDNS: Result: $YaDNSResult"
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement