Yandex DNSホスティングとダイナミックIP

2010年9月、Yandexは、一般向けにドメイン向けメールのDNSホスティングを開始ました。 ユーザーの喜びは際限がなく、トピックには多くの肯定的なコメントが寄せられ、Yandexは善良な企業によって発表されました。



残念ながら、DNSレコードの管理はWebインターフェースを介してのみ提供されていました。 APIは管理用に提供されておらず、まだ登場しておらず、長期間登場しない可能性があります。 この事実により、ダイナミックIPドメインの所有者の多くは、DynDNSによる後者の購入に関連して、優れたfree.editdns.netの無料サービスを有料ベース(カスタムドメイン用)に移行することになります。



奇跡が起こらなかったことを確認した後、この不正を修正するためにPythonからファイルを選びました...



まず、YandexサービスAPIについて少なくとも何らかの情報を探して、すべてを知っているGoogleに行きました。 最初に出会ったのは、ドメイン用のMail APIの詳細な説明でした。 残念ながら、利用可能な32の機能のうち、DNSホスティングの管理に関連するものは何もなかったので、検索を続けました。 リクエストにpythonマジックワードを追加し、その後cシャープを使用して、Yandexサービスで許可する際のブラウザーの詳細な説明とVBおよびC#のコードの例に関するAlexei Nemiroの記事に出会いました。



記事を読んで、ブラウザーをシミュレートする必要があることを確認した後、 FireBugHTTPアナライザーで武装しました 。 承認の複雑さを研究し、DNSホスティングで作業するのにしばらく時間を費やした後、Yandexサービスでの承認が非常に簡単に機能することがわかりました。 許可手順は、 yandexuid cookieで始まります。これは、Yandexサービスにログインしたときに受信されます。



Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  1. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  2. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  3. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  4. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  5. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  6. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  7. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  8. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid



  9. Copy Source | Copy HTML def initialize (self): connection = httplib .HTTPConnection( 'www.yandex.ru' ) connection.request( 'GET' , '/' ) response = connection.getresponse() cookies = response.getheader( 'set-cookie' , None) response.close() match = re .search( '(?<=yandexuid=)[^;]*' , cookies) self ._yandexuid = match.group( 0 ) print 'yandexuid =' , self ._yandexuid







Cookie yandexuidを受信した後ブラウザはログイン、パスワード、タイムスタンプをUNIX形式でPOSTリクエストとともに送信します。 リクエストの形成に問題がなければ、私は長い間タイムスタンプ式にとどまりました:



Copy Source | Copy HTML



  1. def ログイン (自己):
  2. content = 'login = {0}&passwd = {1}&timestamp = {2}'
  3. content = content.format( self ._login、 self ._passwd、 self .timestamp())
  4. connection = httplib .HTTPConnection( 'passport.yandex.ru'
  5. connection.request( 'POST''/ passport?mode = auth' 、content、{ 'Cookie'self .getcookies()})
  6. 応答= connection.getresponse()
  7. 内容= response.read()
  8. response.close()
  9. match = re .search( 'idkey \ "\ s。*' 、content)
  10. match = re .search( '(\ d \ w *)' 、match.group( 0 ))
  11. self ._idkey = match.group( 0
  12. print 'idkey ='self ._idkey




「このコンピューターに永続的な認証を設定する」というリクエストに応えて、マジックIDキーを受け取った後、「いいえ」ボタンのクリックをシミュレートするリクエストを作成する必要がありました。



Copy Source | Copy HTML



  1. def 認証 (自己):
  2. content = 'filled = yes&timestamp = {0}&idkey = {1}&no =%D0%9D%D0%B5%D1%82'
  3. content = content.format( self .timestamp()、 self ._idkey)
  4. connection = httplib .HTTPConnection( 'passport.yandex.ru'
  5. connection.request( 'POST''/ passport?mode = auth' 、content、{ 'Cookie'self .getcookies()})
  6. 応答= connection.getresponse()
  7. cookies = response.getheader( 'set-cookie' 、なし)
  8. ... cook regexpの解析...
  9. response.close()




ドメインのメールを操作するために必要なすべてのCookieを手に入れたので、内部AJAX APIを介してDNSレコードエディターの[保存]ボタンのクリックをシミュレートするだけで十分です。



Copy Source | Copy HTML



  1. def updatedomain (self、ns_record_id):
  2. content = 'domain = {0}&ns_record_id = {1}&ns_rec_type = A&ns_subdomain =%40&ns_weight =&ns_port =&ns_content = {2}&ns_priority = 1'
  3. content = content.format( self ._domain、ns_record_id、 self ._externalip)
  4. connection = httplib .HTTPSConnection( 'pdd.yandex.ru'
  5. connection.request( 'POST''/ajax/ns_simple_record_edit.ajax.xml' 、コンテンツ、\
  6. { 'Accept''application / json、text / javascript、* / *' 、\
  7. 'Cookie'self .getcookies()})
  8. 応答= connection.getresponse()
  9. response.close()




前のケースと同様に、この時点でAcceptヘッダーが必要でした。 ただし、これは最も重要ではありません。 ns_record_idを取得するには、ページのHTMLコードをDNSレコードのリストで解析する必要がありました。



Copy Source | Copy HTML



  1. def domainlist (self):
  2. connection = httplib .HTTPSConnection( 'pdd.yandex.ru'
  3. connection.request( 'GET''/ domain_ns / {0} /' .format( self ._domain)、なし、\
  4. { 'Accept''text / html、application / xhtml + xml、application / xml; q = 0.9、* / *; q = 0.8' 、\
  5. 'Cookie'self .getcookies()、\
  6. 'Referer''https://pdd.yandex.ru' })
  7. 応答= connection.getresponse()
  8. 内容= response.read()
  9. block = re .findall( 'item:\ s \' [\ d] + \ '(。+)value = "[\ w \。] +"' 、コンテンツ)
  10. ブロック内のアイテムの場合:
  11. match = re .search( '(?<= item:\ s \')[\ d] * ' 、item)
  12. ns_record_id = match.group( 0
  13. match = re .search( 'ns_subdomain(。+?)value = \ "(。+?)\"' 、item)
  14. match = re .search( '(?<= value =)\ "。+?\"' 、match.group( 0 ))
  15. ns_subdomain = match.group( 0
  16. match = re .search( 'ns_rec_type(。+?)value = \ "(。+?)\"' 、item)
  17. match = re .search( '(?<= value =)\ "。+?\"' 、match.group( 0 ))
  18. ns_rec_type = match.group( 0
  19. match = re .search( 'ns_content(。+?)value = \ "(。+?)\"' 、item)
  20. match = re .search( '(?<= value =)\ "。+?\"' 、match.group( 0 ))
  21. ns_content = match.group( 0
  22. レコード= 'ns_record_id = {0} | ns_subdomain = {1} | ns_rec_type = {2} | ns_content = {3} '
  23. record.formatの出力(ns_record_id、ns_subdomain、ns_rec_type、ns_content)
  24. response.close()




これは私の最初のPythonプログラムであったため、httplibと手動のCookie形成に限定しました。 コマンドラインパラメーターを追加し、構成を解析し、このカクテルに外部IPを取得すると、Yandex DNSホスティングのDNSレコードを更新するための簡単なスクリプトが得られました。



ソースコードとサンプル設定をダウンロードします



All Articles