Question: When outside our company firewall, I use a script to tunnel via SSH and expose our internal wiki to my OSX machine. Is there a command to temporarily tell OSX to resolve to my local port when the tunnel is set up?

The sequence I'm hoping to use is:

1. open the tunnel
`ssh -f external-proxy.example -L 8001:internal-wiki.example:8000 -N`
2. [DO SOMETHING HERE]
3. typing the URL **internal-wiki.example:8000** in my browser causes it to transparently view **localhost:8001**

Other details:

* I could edit **/etc/hosts** but that would be a persistent change. I want to use this only when outside
* We're not using SSL or certs.

Answer: 
I have a solution for you, wrap your ssh command into a bash script:

```
#!/bin/bash

function control_c {
    echo -en "\n## Caught SIGINT; Clean up /etc/hosts and Exit \n"
    sed -i '' "/internal-wiki/d" /etc/hosts
    exit $?
}

trap control_c SIGINT
trap control_c SIGTERM

(sleep 5; open http://internal-wiki.example:8001 &)&
echo '127.0.0.1 internal-wiki.example' >> /etc/hosts
ssh -L8001:internal-wiki.example:8000 -f external-proxy.example -N

```

Explaining:

1. function that executes the cleanup on Control-C when issued
2. trap Control-C and shutdown
3. sleep, tell osx to open your site, get out of the way (the
ampersand)
4. adds the entry to `/etc/hosts`
5. create the tunnel
6. when you ctrl-c, the functions kicks in and cleans up `/etc/hosts`
with the transient entry
Name services (DNS, ldap, /etc/hosts) do not "resolve ports" they resolve hostnames. This statement is not entirely true but a proper explanation at this point is irrelevant and time consuming.

Since OSX uses a BSD kernel, I guess it may also "borrow" a lot of other code from there. The files ns resolver defaults to reading from /etc/hosts but this can be overridden on Linux and BSD by setting the HOSTALIASES environment variable (in the shell you start the browser from) but this only works if there is no '.' in the name. If you can use urls without a . in the name this will solve your problem.

Alternatively, you could run a script to configure port redirection to reroute packets addressed to xxx.xxx.xxx.xxx:8000 to 127.0.0.1:8001 and conversely on incoming packets. But this is rather a kludgy solution.

Alternatively you could install squid and use a custom url redirector to rewrite the requests (also possible if you start using https -but tricky).

A much cleaner option would be to run a proper tunnelled connection across the ssh. I believe this is not supported by osx out of the box but there are [openvpn compliant add ons for osx](http://www.techrepublic.com/blog/apple-in-the-enterprise/how-to-set-up-an-os-x-openvpn-client/)

If it were me I would just set up scripts to swap around the hosts file, keeping all the traffic on one port and making sure that the default config is swapped in at boot time and when the ssh connection ends.