V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
snnn
V2EX  ›  Google

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

  •  
  •   snnn · 2017-02-28 00:15:16 +08:00 · 2280 次点击
    这是一个创建于 2818 天前的主题,其中的信息可能已经有所发展或是发生改变。

    官方推荐的两个工具,一个依赖于 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;
      }
    
    }
    
    4 条回复    2017-02-28 09:00:55 +08:00
    lan894734188
        1
    lan894734188  
       2017-02-28 01:01:28 +08:00 via Android
    google domain 的?
    ryd994
        2
    ryd994  
       2017-02-28 02:30:38 +08:00 via Android
    ddclient 不好么?
    哦, Windows
    snnn
        3
    snnn  
    OP
       2017-02-28 09:00:15 +08:00 via Android
    snnn
        4
    snnn  
    OP
       2017-02-28 09:00:55 +08:00 via Android
    @ryd994 那个需要 Perl
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2853 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 02:58 · PVG 10:58 · LAX 18:58 · JFK 21:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.