Skip to content

Instantly share code, notes, and snippets.

@mmacvicar
Last active November 11, 2015 20:55
Show Gist options
  • Save mmacvicar/1356a9a0e879c3abcf79 to your computer and use it in GitHub Desktop.
Save mmacvicar/1356a9a0e879c3abcf79 to your computer and use it in GitHub Desktop.
#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
require 'tempfile'
module Tty extend self
def blue; bold 34; end
def white; bold 39; end
def red; underline 31; end
def reset; escape 0; end
def bold n; escape "1;#{n}" end
def underline n; escape "4;#{n}" end
def escape n; "\033[#{n}m" if STDOUT.tty? end
end
class Array
def shell_s
cp = dup
first = cp.shift
cp.map{ |arg| arg.gsub " ", "\\ " }.unshift(first) * " "
end
end
def ohai *args
puts "#{Tty.blue}==>#{Tty.white} #{args.shell_s}#{Tty.reset}"
end
def warn warning
puts "#{Tty.red}Warning#{Tty.reset}: #{warning.chomp}"
end
def system *args
abort "Failed during: #{args.shell_s}" unless Kernel.system(*args)
end
def sudo *args
ohai "/usr/bin/sudo", *args
system "/usr/bin/sudo", *args
end
def getc # NOTE only tested on OS X
system "/bin/stty raw -echo"
if STDIN.respond_to?(:getbyte)
STDIN.getbyte
else
STDIN.getc
end
ensure
system "/bin/stty -raw echo"
end
def wait_for_user
puts
puts "Press RETURN to continue or any other key to abort"
c = getc
# we test for \r and \n because some stuff does \r instead
abort unless c == 13 or c == 10
end
PPP_PATH = '/etc/ppp'
PPP_SCRIPT_NAME = 'ip-up'
PPP_SCRIPT_CONTENT = <<-ROUTES_CONTENT
#!/bin/sh
/sbin/route -n add 10.0.0.0/16 172.16.1.1
ROUTES_CONTENT
if !File.directory? PPP_PATH
abort <<-EOABORT if !File.directory? PPP_PATH
The folder #{PPP_PATH} does not exists, your version of OSX might not be compatible
EOABORT
end
ohai "Creating route initialization script"
file = Tempfile.new('vpn_route_config_')
begin
file.write PPP_SCRIPT_CONTENT
file.flush
sudo "cp", file.path, File.join(PPP_PATH, PPP_SCRIPT_NAME)
ensure
file.close
file.unlink
end
sudo "/bin/chmod", "ug+rwx", File.join(PPP_PATH, PPP_SCRIPT_NAME)
ohai "done! to undo delete this file: #{File.join(PPP_PATH, PPP_SCRIPT_NAME)}"
ohai "vpn routes setup ready"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment