Team LiB   Previous Section   Next Section

Chapter 16. Network Setup Scripting

Open Transport is the Apple technology under Mac OS 9 that allows programmers and users to send and receive bytes across networks using TCP/IP, AppleTalk, Infrared, or Remote Access methods. When you can mount network volumes on your desktop over an Ethernet network, you are using Open Transport. When you log on to the Web using an analog modem, cable modem, digital subscriber line (DSL), or some other method, you are also calling on various Open Transport protocols. You specifically use Remote Access and TCP/IP to make most connections to the Web using a Mac. For example, I connect to the Web using a Local Area Network (LAN) connection to a proxy server and cable modem, and thus rely on my Mac's TCP/IP configuration to access the Internet. My father, on the other hand, uses a 56K modem and dial-up connection in a remote part of Maine. His Mac system uses Remote Access and TCP/IP configurations to connect over a phone line to his Internet Service Provider (ISP).

Network Setup Scripting Version 1.1.1 and Open Transport Version 2.6.1 are used for the examples in this chapter.

Configurations are collections of settings for various network methods, like AppleTalk, TCP/IP, or Remote Access. Open Transport stores these settings in a database system called the Open Transport configurations database. AppleScripts can access this database and all the various network configurations that you may want to script via the Network Setup Scripting application. Figure 16-1 shows this application icon. This program is located in the startup disk:System Folder:Scripting Additions folder. Therefore, all of your Network Setup Scripting AppleScripts have to target this application, as in:

tell app "Network Setup Scripting..." 

You can look at the Network Setup Scripting dictionary by choosing this application in Script Editor's File Open Dictionary... menu.

Figure 16-1. Network Setup Scripting icon
figs/ascr_1601.gif

Example 16-1 opens the Open Transport database then cycles through all of its configurations, looking for the TCP/IP configuration. Once it finds the TCP/IP configuration, it attempts to get the IP address of the machine. The code encloses the Network Setup commands in a try statement, so that if an error occurs it is caught and the Open Transport configurations database is closed. When you are accessing data in this database, you should close the database when you finish so that Open Transport continues to function properly. The script example shows the return values of these code statements, as they appear in Script Editor's Event Log window. Chapter 2, describes the Event Log. In this case, the machine is using the DHCP protocol (its IP address is allocated by a proxy server when the client machine boots up), and Open Transport returns a series of zeros (0.0.0.0) in lieu of the actual IP address.

Another way to get the machine's actual IP address (not 0.0.0.0) is to send a get IP address Apple event to the Apple System Profiler:

tell app "Apple System Profiler" to get IP address

This will return a string like "172.158.73.1" for the machine's IP address, if it has one. See Chapter 11.

Notice that the script uses the open database and close database commands of the Network Scripting application. You have to use these commands to get any data from the Network Setup Scripting application.

Example 16-1. Opening the Open Transport Configuration Database
tell application "Network Setup Scripting"

   try

      open database

      set con_set to current configuration set

      set con_list to con_set's configurations (* gets list of all the 

      current open transport configurations *)

      repeat with con in con_list -- look for a TCPIP config in this list

         if (class of con is TCPIP v4 configuration) then

            IP address of con

         end if

      end repeat

      close database

      (*make sure the open transport database is closed if the script is 

      interrupted by an error *)

   on error

      close database

   end try

end tell 

(* sample return value in Event Log window*)

get current configuration set

--> configuration set "My Network Settings"

get every configuration of configuration set "My Network Settings"

--> {AppleTalk configuration "printer_config", Modem configuration "Default", 

Remote Access configuration "Default", TCPIP v4 configuration "sygate"}

-- some return values snipped here ...

get IP address of TCPIP v4 configuration "sygate"

--> "0.0.0.0"

The rest of this chapter explains the Network Setup Scripting commands and classes in their own reference sections. The first section describes the commands that you can use to script the Open Transport network system, such as open database and close database. The classes are the blueprints for the objects that your scripts will target, such as the Network Setup Scripting application itself and Remote Access configurations (which are commonly used to connect modems with the Web).

    Team LiB   Previous Section   Next Section