写了一个 google 动态 dns 的更新脚本

2017-02-28 00:15:16 +08:00
 snnn

官方推荐的两个工具,一个依赖于 perl ,我懒得装 perl 。另一个在 windows 上编译不过去。于是我就干脆随手拿 java 撸了一个。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.bind.DatatypeConverter;

public class UpdateDNS {
  static Pattern p = Pattern.compile(".*sohu_user_ip=\"([0-9\\.]+).*");

  public static void main(String[] args) throws Exception {

    java.util.concurrent.ScheduledExecutorService exe = Executors.newScheduledThreadPool(1);
    exe.scheduleAtFixedRate(new Runnable() {

      @Override
      public void run() {
        try {
          String ip = getIP();
          if (ip == null || ip.isEmpty())
            return;
          Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
          String authString = "dddd:dddd";
          String encodedAuthString = DatatypeConverter
              .printBase64Binary(authString.getBytes(StandardCharsets.UTF_8));
          String url = "https://domains.google.com/nic/update?hostname=h.ddd.com&myip=" + ip;
          URL oracle = new URL(url);
          URLConnection yc = oracle.openConnection(proxy);
          yc.setRequestProperty("Authorization", "Basic " + encodedAuthString);

          BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
          StringBuilder sb = new StringBuilder();
          String inputLine;
          while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine);
          }
          in.close();
          String content = sb.toString();
          System.out.println(content);
        } catch (Exception ex) {
          ex.printStackTrace();
        }

      }
    }, 0, 10, TimeUnit.SECONDS);

  }

  private static String getIP() throws MalformedURLException, IOException {
    URL oracle = new URL("http://txt.go.sohu.com/ip/soip");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
      sb.append(inputLine);
    }
    in.close();
    String content = sb.toString();
    Matcher m = p.matcher(content);
    if (m.matches()) {
      return m.group(1);
    }
    return null;
  }

}
2280 次点击
所在节点    Google
4 条回复
lan894734188
2017-02-28 01:01:28 +08:00
google domain 的?
ryd994
2017-02-28 02:30:38 +08:00
ddclient 不好么?
哦, Windows
snnn
2017-02-28 09:00:15 +08:00
snnn
2017-02-28 09:00:55 +08:00
@ryd994 那个需要 Perl

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/343682

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX