Articles Archive
Articles Search
Director Wiki

Tapping the Power of Unix from Within Director - Part 1

December 22, 2004
by Cole Tierney

So _system.environmentPropList.internetConnected = #online. But are we really online? This system property tells us that the machine in question is capable of connecting to the internet. But this could be a laptop currently out of range of a wireless access point or otherwise not connected to a network. Or worse, this may be a machine with a dialup internet connection that happens to be offline at the time. Using GetNetText to sniff a text file on the internet may be sufficient most of the time, but what about modem users?

One solution is to use _system.environmentPropList.internetConnected in conjunction with DirectConnection xtra. But there is currently no MacOSX version available. Which brings me to the real purpose of this article. How to access the FreeBSD sub system of MacOSX with Valentin's shell xtra: This is just one example of the many goodies to be had under the hood of OSX!

First let's take a look at the command, netstat. This displays information about the machine's network status. The -r option will display the contents of the routing tables. (To get documentation on this command, open the terminal app and enter 'man netstat'. The 'man' keyword stands for 'manual'. You can use it with any command to see the documentation on that command).

When a network interface becomes active, the route daemon will update the routing tables. If the interface has a connection to the internet, a default route will be added. Here are my results while having an active connection: put shell_cmd("netstat -r")

-- "Routing tables
Destination        Gateway            Flags    Refs      Use  Netif Expire
default          UGSc        7        1    en1
127                localhost          UCS         0        0    lo0
localhost          localhost          UH         38    46834    lo0
169.254            link#5             UCS         0        0    en1
192.168.1          link#5             UCS         2        0    en1        0:c:42:d1:a4:ab    UHLW        8       18    en1    743       localhost          UHS         0        3    lo0      0:31:67:28:1f:6d   UHLW        0        0    en1    637

Now it may be tempting to crunch through this text with lingo's rich set of string functions, but unix has a sea of text handling functionality. The command 'grep' is a good example. Grep's purpose in life is to print lines containing certain patterns: Globally search for the Regular Expression and Print. Also unix lets you redirect the output of one command to the input of another with the pipe character, '|'. So here's how we could filter the output of the above command with grep. This statement will echo any line that starts with 'default' (case sensitive):

put shell_cmd("netstat -r | grep '^default'")
-- "default          UGSc        7        1    en1"

If you're currently not connected to a TCP/IP network, the string should be empty. So the next natural step is to wrap it with a little more lingo:

on connected(me)
  return (shell_cmd("netstat -r | grep '^default'")) <> EMPTY)

Parting notes: In your journeys, you may want to checkout the command, apropos. This comes in handy when you're looking for a command and you can't remember the exact wording. Just try something like 'apropos ifc'. Also install Apple's developer tools, if you haven't already. It includes lots of extra unix commands for dealing with mac files and resources. You'll also be able to compile open source projects.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.