Browse Source

增加脚本

liuyuqi-dellpc 4 years ago
parent
commit
1e2fc83111
3 changed files with 166 additions and 0 deletions
  1. 59 0
      tools/get_ip_utils.py
  2. 106 0
      tools/main.py
  3. 1 0
      tools/requirements.txt

+ 59 - 0
tools/get_ip_utils.py

@@ -0,0 +1,59 @@
+import requests
+from bs4 import BeautifulSoup
+import re
+
+def getIpFromIpaddress(site):
+    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
+    'Host': 'ipaddress.com' }
+    url = "https://ipaddress.com/search/" + site
+    trueip = None
+    try:
+        res = requests.get(url,headers=headers,timeout=2)
+        soup = BeautifulSoup(res.text, 'html.parser')
+            ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
+        result = soup.find_all('div', class_="comma-separated")
+        for c in result:
+            if len(ip) != 0:
+                trueip = ip[0]
+    except Exception as e:
+        print("查询" + site + " 时出现错误: "+str(e))
+    return trueip
+
+def getIpFromChinaz(site):
+    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
+    'Host': 'ip.tool.chinaz.com' }
+    url = "http://ip.tool.chinaz.com/" + site
+    trueip = None
+    try:
+        res = requests.get(url,headers=headers)
+        soup = BeautifulSoup(res.text, 'html.parser')
+        result = soup.find_all('span', class_="Whwtdhalf w15-0")
+        for c in result:
+            ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
+            if len(ip) != 0:
+                trueip = ip[0]
+    except Exception as e:
+        print("查询" + site + " 时出现错误: "+str(e))
+    return trueip
+
+
+def getIpFromWhatismyipaddress(site):
+    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
+    'Host': 'ip.tool.chinaz.com' }
+    url = "https://whatismyipaddress.com//hostname-ip"
+    data={
+        "DOMAINNAME":site,
+        "Lookup IP Address": "Lookup IP Address"
+    }
+    trueip = None
+    try:
+        res = requests.post(url,headers=headers,data=data)
+        soup = BeautifulSoup(res.text, 'html.parser')
+        result = soup.find_all('span', class_="Whwtdhalf w15-0")
+        for c in result:
+            ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
+            if len(ip) != 0:
+                trueip = ip[0]
+    except Exception as e:
+        print("查询" + site + " 时出现错误: "+str(e))
+    return trueip

+ 106 - 0
tools/main.py

@@ -0,0 +1,106 @@
+
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Author  :   liuyuqi
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2020/04/26 23:01:40
+@Version :   1.0
+@License :   Copyright © 2017-2020 liuyuqi. All Rights Reserved.
+@Desc    :   github.com
+'''
+
+import shutil
+import os,sys,ctypes
+import datetime
+import get_ip_utils
+import platform
+
+# 需要获取ip的网址
+sites = [
+    "github.global.ssl.fastly.net",
+    "assets-cdn.github.com",
+    "documentcloud.github.com",
+    "gist.github.com",
+    "gist.githubusercontent.com",
+    "github.githubassets.com",# 很卡
+    "help.github.com",
+    "nodeload.github.com",
+    "raw.github.com",
+    "status.github.com",
+    "training.github.com",
+    "avatars0.githubusercontent.com",
+    "avatars1.githubusercontent.com",
+    "avatars2.githubusercontent.com",
+    "avatars3.githubusercontent.com",
+    "avatars4.githubusercontent.com",
+    "avatars5.githubusercontent.com",
+    "avatars6.githubusercontent.com",
+    "avatars7.githubusercontent.com",
+    "avatars8.githubusercontent.com",
+    "codeload.github.com",
+    "camo.githubusercontent.com",
+    "raw.githubusercontent.com", 
+    "cloud.githubusercontent.com",
+    "user-images.githubusercontent.com",
+    "customer-stories-feed.github.com",
+    "pages.github.com",
+    "api.github.com",
+    "live.github.com",
+    "github.com"
+]
+
+addr2ip = {}
+# hostLocation = r"C:\Windows\System32\drivers\etc\hosts"
+hostLocation = r"E:\downloads\chrome\speed-github-master\hosts"
+
+def dropDuplication(line):
+    flag = False
+    if "#*********************github" in line:
+        return True
+    for site in sites:
+        if site in line:
+            flag = flag or True
+        else:
+            flag = flag or False
+    return flag
+
+# 更新host, 并刷新本地DNS
+def updateHost():
+    today = datetime.date.today()
+    for site in sites:
+        trueip=get_ip_utils.getIpFromChinaz(site)
+        if trueip != None:
+            addr2ip[site] = trueip
+            print(site + "\t" + trueip)
+    shutil.copy(hostLocation, hostLocation + ".bak")  # 做一份host备份
+    with open(hostLocation, "r") as f1:
+        f1_lines = f1.readlines()
+        with open("temphost", "w") as f2:
+            for line in f1_lines:                       # 为了防止 host 越写用越长,需要删除之前更新的含有github相关内容
+                if dropDuplication(line) == False:
+                    f2.write(line)
+            f2.write("#*********************github " +
+                     str(today) + " update********************\r\n")
+            for key in addr2ip:
+                f2.write(addr2ip[key] + "\t" + key + "\n")
+
+    shutil.copy("./temphost", hostLocation)
+    os.system("ipconfig /flushdns")
+
+def is_admin():
+    try:
+        return ctypes.windll.shell32.IsUserAnAdmin()
+    except Exception as e:
+        return False
+
+if __name__ == "__main__":
+    os=platform.system()
+    if os=="Windows":
+        # if is_admin():
+        #     print("admin.....")
+        # else:
+        #     if sys.version_info[0]==3:
+        #         ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
+        #         print("hahhah....")
+        updateHost()

+ 1 - 0
tools/requirements.txt

@@ -0,0 +1 @@
+bs4