open a predefined VPN connection on Mac using ruby

Amazing, it seems that there is no simple way to open a predefined VPN connection with a terminal command. So after asking the search engine of my choice I found some applescript stuff to do this.

I have wrapped it in ruby such that I can use it now from my rake files.

class VpnOnMac

  def self.open(service)
    cmd=[]
    cmd < < "osascript <<-EOF"
    cmd << "tell application \"System Events\""
    cmd << "        tell current location of network preferences"
    cmd << "                set VPN to service \"#{service}\" -- your VPN name here"
    cmd << "                if exists VPN then connect VPN"
    cmd << "                repeat while (current configuration of VPN is not connected)"
    cmd << "                    delay 1"
    cmd << "                end repeat"
    cmd << "        end tell"
    cmd << "end tell"
    cmd << "EOF"

    cmd=cmd.join("\n")

    system(cmd)
  end


  def self.close(service)
    cmd=[]
    cmd << "osascript <<-EOF"
    cmd << "tell application \"System Events\""
    cmd << "        tell current location of network preferences"
    cmd << "                set VPN to service \"#{service}\" -- your VPN name here"
    cmd << "                if exists VPN then disconnect VPN"
    cmd << "        end tell"
    cmd << "end tell"
    cmd << "EOF"

    cmd=cmd.join("\n")

    system(cmd)
  end

end

with this, you can open your service e.g. by

VpnOnMac::open("my Vpn")

Noch kein Kommentar