SHOW:
|
|
- or go back to the newest paste.
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 | # Tested on 6.0rc14 and 6.0 | |
7 | ||
8 | # Set needed variables | |
9 | ||
10 | #Root domain | |
11 | :local YaDNSdomain "domain.ru" | |
12 | :local YaDNSsubdomain "xxx.domain.ru" | |
13 | ||
14 | # read http://api.yandex.ru/pdd/doc/api-pdd/reference/api-dns_get_token.xml#api-dns_get_token | |
15 | :local YaDNStoken "132456789012345678901234567890" | |
16 | ||
17 | # read http://api.yandex.ru/pdd/doc/api-pdd/reference/api-dns_edit_a_record.xml | |
18 | :local YaDNSrecordid "1234567" | |
19 | :local YaDNSTTL "300" | |
20 | ||
21 | :global YaDNSForceUpdateOnce | |
22 | :global YaDNSPreviousIP | |
23 | :local YaDNSInterfaceName "PPPoE_NBN" | |
24 | :local YaDNSDomainRecord | |
25 | ||
26 | # get the current IP address from the interface | |
27 | ||
28 | :if ([:len [/interface find name=$YaDNSInterfaceName]] = 0 ) do={ | |
29 | :log info "UpdateYaDNS: No interface named $YaDNSInterfaceName , please check configuration." | |
30 | :error "UpdateYaDNS: No interface named $YaDNSInterfaceName , please check configuration." | |
31 | } | |
32 | ||
33 | :if ([:typeof $YaDNSPreviousIP] = "nothing" ) do={ :global YaDNSPreviousIP 0.0.0.0 } | |
34 | ||
35 | :local YaDNSYaDNSCurrentIPMask [ /ip address get [/ip address find interface=$YaDNSInterfaceName] address ] | |
36 | ||
37 | :local YaDNSCurrentIP [:pick $YaDNSYaDNSCurrentIPMask 0 [:find $YaDNSYaDNSCurrentIPMask "/"]] | |
38 | ||
39 | ||
40 | :if ([ :typeof $YaDNSCurrentIP ] = "nothing" ) do= { | |
41 | :log info "UpdateDynDNS: No ip address present on $YaDNSInterfaceName, please check." | |
42 | :error "UpdateDynDNS: No ip address present on $YaDNSInterfaceName, please check." | |
43 | } | |
44 | ||
45 | :local YaDNSsrcpath1 ( "nsapi/get_domain_records.xml\?token=" . $YaDNStoken . "&domain=" . $YaDNSdomain ) | |
46 | ||
47 | :local YaDNSAPI [:resolve "pddimp.yandex.ru"] | |
48 | /tool fetch mode=https address="$YaDNSAPI" host="pddimp.yandex.ru" src-path=$YaDNSsrcpath1 dst-path="/YaDNSGetDomainRecord.txt" | |
49 | ||
50 | :local Result1 [/file get YaDNSGetDomainRecord.txt contents] | |
51 | :local Result2 [:pick $Result1 ([:find $Result1 "id=\"$YaDNSrecordid"]) ([:find $Result1 "id=\"$YaDNSrecordid"]+42) ] | |
52 | :set YaDNSDomainRecord [:pick $Result2 ([:find $Result2 ">"] + 1) ( [:find $Result2 "<"] ) ] | |
53 | ||
54 | :if (($YaDNSForceUpdateOnce or ($YaDNSCurrentIP != $YaDNSPreviousIP) or ($YaDNSCurrentIP != $YaDNSDomainRecord)) = true) do={ | |
55 | ||
56 | :log info "UpdateYaDNS: Try Update" | |
57 | ||
58 | :log info "UpdateYaDNS: YaDNSForceUpdateOnce = $YaDNSForceUpdateOnce" | |
59 | :log info "UpdateYaDNS: YaDNSPreviousIP = $YaDNSPreviousIP" | |
60 | :log info "UpdateYaDNS: YaDNSCurrentIP = $YaDNSCurrentIP" | |
61 | :log info "UpdateYaDNS: YaDNSDomainRecord = $YaDNSDomainRecord" | |
62 | ||
63 | :local YaDNSsrcpath2 ( "nsapi/edit_a_record.xml\?token=" . $YaDNStoken . "&domain=" . $YaDNSdomain . "&record_id=" . $YaDNSrecordid . "&ttl=" . $YaDNSTTL . "&content=" . $YaDNSCurrentIP ) | |
64 | ||
65 | if ( $YaDNSdomain != $YaDNSsubdomain ) do={ set YaDNSsrcpath2 ($YaDNSsrcpath2 . "&subdomain=" . YaDNSsubdomain) } | |
66 | ||
67 | :local YaDNSAPI [:resolve "pddimp.yandex.ru"] | |
68 | ||
69 | /tool fetch mode=https address="$YaDNSAPI" host="pddimp.yandex.ru" src-path=$YaDNSsrcpath2 dst-path="/YaDNS.txt" | |
70 | :local result [/file get YaDNS.txt contents] | |
71 | ||
72 | :global YaDNSResult [:pick $result ([:find $result "<error>"]+7) [:find $result "</error>"]] | |
73 | ||
74 | :if ( $YaDNSResult = "ok" ) do={ | |
75 | :set YaDNSForceUpdateOnce false | |
76 | :set YaDNSPreviousIP $YaDNSCurrentIP | |
77 | :log info "UpdateYaDNS: Update Success" | |
78 | } | |
79 | ||
80 | :log info "UpdateYaDNS: Result: $YaDNSResult" | |
81 | } |