#!/bin/sh # Get the address of the interface through which our default gateway is reached. # This is very likely to be the correct IP to bind to most of the time. # 'netstat -nr' prints out something that looks like this. #Routing tables # #Internet: #Destination Gateway Flags Refs Use Netif Expire #default 192.168.1.2 UGS 0 7840565 re0 #127.0.0.1 127.0.0.1 UH 0 1312935 lo0 IFACE=`netstat -nr | awk '/^default/ {print $6}'` ifconfig $IFACE | grep "inet " | head -n 1 | awk '{print $2}'