Skip to content

Instantly share code, notes, and snippets.

@bastengao
Created June 21, 2013 13:15
Show Gist options
  • Save bastengao/5831053 to your computer and use it in GitHub Desktop.
Save bastengao/5831053 to your computer and use it in GitHub Desktop.
自动更新 smart hosts 文件的小工具,暂时只支持 Windows
#encoding: utf-8
require "uri"
require "net/http"
# 自动更新 smart hosts 文件的小工具
# 暂时只支持 Windows
# 抓取最新的 hosts 内容
def fetch_latest_hosts
Net::HTTP.get('smarthosts.googlecode.com', '/svn/trunk/hosts')
end
def merge_to_one_file(dest_file, *src_files)
File.open(dest_file, "w+") do |out_file|
src_files.each do |src_file|
IO.copy_stream(src_file, out_file)
out_file.puts
end
end
end
#下载指定内容到某文件
def download_to_file(uri_path, dest_file)
uri = URI(uri_path)
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri.request_uri
http.request request do |response|
File.open dest_file, 'w+' do |io|
response.read_body do |chunk|
io.write chunk
end
end
end
end
end
BASE_PATH = 'C:\WINDOWS\system32\drivers\etc'
smart_hosts = File.join(BASE_PATH, "smart_hosts")
download_to_file("http://smarthosts.googlecode.com/svn/trunk/hosts", smart_hosts)
hosts = File.join(BASE_PATH, "HOSTS")
my_hosts = File.join(BASE_PATH, "my_hosts")
merge_to_one_file(hosts, my_hosts, smart_hosts)
puts "update smart hosts successful"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment