Skip to content

Instantly share code, notes, and snippets.

@medeirosinacio
Forked from mangar/hosts.rb
Created February 6, 2020 13:40
Show Gist options
  • Save medeirosinacio/9cd2d54086664c713a016220deef3c63 to your computer and use it in GitHub Desktop.
Save medeirosinacio/9cd2d54086664c713a016220deef3c63 to your computer and use it in GitHub Desktop.
ruby script to update hosts file
#!/usr/bin/env ruby
HOSTS_FILE = "hosts"
MY_HOSTS_FILE = "hosts.marciog"
ENTRY_MARK = "########## STARTING PERSONAL CONFIGURATION ##########\n"
def contains_entry?
contains = false
file = File.new(HOSTS_FILE, "r")
while (line = file.gets)
if line == ENTRY_MARK
contains = true
end
end
file.close
contains
end
def add_entry
new_content = file_content MY_HOSTS_FILE
open(HOSTS_FILE, 'a') do |f|
f << "\n\n#{new_content} \n"
end
end
def file_content file_name=HOSTS_FILE
content = ""
file = File.new(file_name, "r")
while (line = file.gets)
content += "#{line}"
end
file.close
content
end
def main
puts ">>> HOSTS CONTENT..... \n"
puts "\n"
puts file_content
puts "\n"
if contains_entry?
puts ">>> HOSTS updated!"
else
puts ">>> HOSTS old.... updating.... \n"
add_entry
puts ">>> NEW HOSTS .... \n"
puts file_content
end
puts ">>> DONE!"
end
system "clear"
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment