Skip to content

Instantly share code, notes, and snippets.

@alexdantas
Created April 4, 2014 20:19
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 alexdantas/9982401 to your computer and use it in GitHub Desktop.
Save alexdantas/9982401 to your computer and use it in GitHub Desktop.
Adds a host to the Windows' hosts file
# It'll append the hosts file with an argument you
# send to it.
#
# For example, if you open 'cmd.exe' and go where this
# script is located:
#
# C:\Users\you> ruby add-host.rb "127.0.0.1 some-website.com"
#
# Your hosts file will have '127.0.0.1 some-website.com' at
# the end.
#
# Of course, you need administrator priviledges when running it.
begin
# 'ARGV' contains all the things you send to
# the program when calling it.
#
# For example, in your 'cmd.exe':
#
# C:\Users\you> ruby add-host.rb "127.0.0.1 some-website.com"
#
# ARGV.first will have the string "127.0.0.1 some-website.com"
if not ARGV.first
exit
end
filename = "C:/Windows/system32/drivers/etc/hosts"
# This block of code will try to open 'filename'
# and keep it open until it reaches 'end'.
#
# That '"a"' is to append things to the end of
# 'file'.
#
File.open(filename, "a") do |file|
# Appending the first argument to the file
file.puts ARGV.first
end
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment