OSX networking witchcraftery

Differences:

While native linux folks have a pretty solid understanding of how networking works. OSX throws a few curve-balls.

Instead of sudo route -n, OSX alternative would be sudo netstat -nr

Luckily for both sides, /etc/hosts still holds true, but our next barrier we're likely to see is editing the DNS resolvers.

Linux folks are used to simply /etc/resolv.conf however in OSX the place to look would be scutil --dns.

route get 192.168.1.2 still works on both, however adding in a new route changes,

  • linux: ip route add 192.168.1.0/24 dev eth0

  • osx: sudo route -n add 192.168.1.0/24 -interface eth0

With a gateway instead of interface:

  • linux: route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.10

  • osx: sudo route -n add 192.168.2.0/24 192.168.2.10

Quick Scripts:

When doing split tunneling, its pretty common for VPN to drop off leaving the gateways attached, thus making it impossible to re-attach. Most people say reboot :(, i say script it.

#!/usr/bin/env python
###############################################################################
#      For use when all hope is lost and DNS doesn't work after killing vpn   #
###############################################################################
import subprocess

def print_kill(value):
    red = '\033[1;31m'
    clear = '\033[1;m'
    print '{0}{1}{2}'.format(red, value, clear)

commands = {'racoon': ['sudo', 'killall', 'racoon'],
                       'configd': ['sudo', 'killall', 'configd'],
                       'mDNSResponder': ['sudo', 'killall', 'mDNSResponder'],
                       'nesessionmanager': ['sudo', 'killall', 'nesessionmanager'],
                       'networkd': ['sudo', 'killall', 'networkd']}


for command in commands:
    kill_verbiage = 'Killing {0}...'.format(command)
    print_kill(kill_verbiage)
    try:
        subprocess.check_output(commands[command])
    except:
        print_kill("Unable to kill {0}".format(command))

How about being able to toggle off DNS resolvers when working with multiple DNS?

#!/usr/bin/env python
import sys
import argparse
import subprocess

result = None

parser = argparse.ArgumentParser(description='Script for managing osx dns')
parser.add_argument('--enable', action="store_true", default=False)
parser.add_argument('--disable', action="store_true", default=False)

results = parser.parse_args()

if not results:
    sys.exit("Run help for usage")

if results.enable and results.disable:
    sys.exit("Can't run both, run help for usage")

if results.enable:
    command = ['sudo', '/usr/sbin/networksetup', '-setdnsservers', 'Wi-Fi',
               '10.11.12.13', '10.11.12.14', '172.1.2.3', '172.1.2.4']
# Set back to google dns 8.8.8.8
if results.disable:
    command = ['sudo', '/usr/sbin/networksetup', '-setdnsservers', 'Wi-Fi',
               '8.8.8.8']

try:
    result = subprocess.check_output(command)
except Exception as e:
    print "Failed: {0}".format(e)
print result

Thats about it!, subprocess isn't that elegant but it does the trick when not having to keep everything in your head.

comments powered by Disqus