Created
April 4, 2014 20:19
-
-
Save alexdantas/9982401 to your computer and use it in GitHub Desktop.
Adds a host to the Windows' hosts file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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