Skip to content

Instantly share code, notes, and snippets.

@ScotterC
Created March 23, 2017 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScotterC/14d89f1d8ab0b3d18ba0f883d82fa846 to your computer and use it in GitHub Desktop.
Save ScotterC/14d89f1d8ab0b3d18ba0f883d82fa846 to your computer and use it in GitHub Desktop.
blackhole.rb
#! /usr/bin/ruby
require 'fileutils'
path = "/etc/hosts"
sites = %w(
news.ycombinator.com
reddit.com
www.reddit.com
facebook.com
www.facebook.com
ign.com
www.ign.com
nytimes.com
www.nytimes.com
)
blacholed = File.open(path).grep(/Blackhole/)
if blacholed.empty?
File.open(path, "a") do |f|
f.puts "\n"
sites.each do |s|
f.puts("127.0.0.1 #{s} # Blackhole")
f.puts("fe80::1%lo0 #{s} # Blackhole")
end
end
else
tmp_path = path + ".tmp"
FileUtils.cp path, tmp_path
File.open(path, 'w') do |output|
IO.foreach(tmp_path) do |line|
output.write(line) unless line.include? "Blackhole"
end
end
FileUtils.rm tmp_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment