Okay; now that we have the basics down, I have a real-world application. Our network is behind firewall/router that does not allow loopbacks. Therefore, changing the lookup order and adding
machines entries to NetInfo to get local domain name resolution is the preferred method for adding entries to access our domain named e-mail server. This works fine for desktop machines within the confines of a LAN.
However, I have a laptop user to contend with who is not always on the network. He often travels and dials-up on the road. NetInfo 'machines' entries will trump his DNS resolution, making our server inaccessible to him.
The ideal solution(?) is to have the user switch lookup orders for when they are on the road and back when they are behind the network. I elected to create an AppleScript to do the job and install the Script Menu in the user's menubar to make the script easily accessible.
Overall, it seemed like the most effecient way to accomplish the goal. For those interested, here is the script in its enirety. I have also attached it to this post as a compiled AppleScript.
Code:
(*
This script will set optimize your lookup order for a LAN internet connection or dial-up
*)
set runresult to ""
tell me to activate
display dialog "Please choose your connection method to optimize your internet connection" buttons {"LAN", "Dial-up"} default button 2
set userchoice to button returned of result
if userchoice = "LAN" then
set myReply to (display dialog "Please enter your Administrator password" default answer "" giving up after 60)
set myPassword to (text returned of myReply)
if myPassword is not "" then
try
--LAN optimized
do shell script "sudo /usr/bin/niutil -create / /locations/lookupd/hosts" password myPassword with administrator privileges
do shell script "sudo /usr/bin/niutil -createprop / /locations/lookupd/hosts LookupOrder CacheAgent NIAgent DNSAgent" password myPassword with administrator privileges
do shell script "sudo /bin/kill -USR1 `/bin/cat /var/run/lookupd.pid`" password myPassword with administrator privileges
set runresult to "Network settings optimized for LAN connection" & return
on error errormsj
display dialog errormsj as string
set runresult to runresult & "Settings change failed: " & errormsj & return
--return adds ASCII13 to the output
end try
end if
tell me to activate
display dialog (runresult as string) buttons {"Ok"}
else if userchoice = "Dial-up" then
set myReply to (display dialog "Please enter your Administrator password" default answer "" giving up after 60)
set myPassword to (text returned of myReply)
if myPassword is not "" then
try
--Dial-up optimized
do shell script "/usr/bin/niutil -create / /locations/lookupd/hosts" password myPassword with administrator privileges
do shell script "/usr/bin/niutil -createprop / /locations/lookupd/hosts LookupOrder CacheAgent DNSAgent NIAgent" password myPassword with administrator privileges
do shell script "/bin/kill -USR1 `/bin/cat /var/run/lookupd.pid`" password myPassword with administrator privileges
--display progress
set runresult to "Settings optimized for dial-up connection"
on error errormsj
display dialog errormsj as string
set runresult to errormsj
end try
end if
tell me to activate
display dialog ("Network Configuration:" & return & runresult) as string buttons {"Ok"}
end if