Find the source of DHCP-provided addresses via script

May 21, '08 07:30:01AM

Contributed by: RickoKid

I often need to know whilst debugging a network connection where my computer has picked up an IP address from, so I can find the IP address of a router, or to make sure there's not a rogue DHCP server on the network and so on. There is no way to find this in the GUI in Mac OS X, although it is available via ipconfig on the command line:

ipconfig getoption en0 server_identifier
Needless to say, I can never remember the parameters for it, so I wrote an AppleScript to automate the process. With the script, you can also get the DHCP server for multiple interfaces at once as well. Here's the code:
(*
    DHCP Server Lookup © RickoKid 2008
    Version 0.1
  
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)

set resultList to {}
set DNSlookup to ""
set OSVer to (do shell script "sw_vers -productVersion")
set tigerSupport to false
set legacySupport to false

if OSVer contains "10.4" or OSVer contains "10.3" or OSVer contains "10.2" or OSVer contains "10.1" or OSVer contains "10.1" then
  if OSVer contains "10.4" then
    set tigerSupport to true
  else
    display dialog "This script has only been tested on Mac OS X 10.4 and above.  You may encounter errors and/or crashes in earlier versions of Mac OS X" buttons {"OK"} default button "OK"
    set legacySupport to true
  end if
  
  set interfaceList to {}
  
  repeat with theInterface in words of (do shell script "ifconfig -lu")
    if theInterface contains "lo0" then
      set interfaceList to interfaceList & {"lo0 Loopback"}
    else if theInterface contains "en1" then
      set interfaceList to interfaceList & {"en1 (AirPort?)"}
    else if theInterface contains "en" then
      set interfaceList to interfaceList & {theInterface & " (Ethernet?)"}
    else if theInterface contains "fw" then
      set interfaceList to interfaceList & {theInterface & " (Firewire?)"}
    else
      set interfaceList to interfaceList & {theInterface}
    end if
  end repeat
else
  -- set interfaceList to paragraphs of (do shell script "networksetup -listallhardwareports | grep -e 'Hardware Port:' | sed 's/^.*: //'")
  tell application "System Events" to set interfaceList to name of every interface of network preferences
end if

set chosenInterface to choose from list interfaceList with prompt ¬
  "Please select the network interface(s) you would like the DHCP server address for (or just click OK to select all interfaces):" with title ¬
  "Select Network Interface" with multiple selections allowed and empty selection allowed
if chosenInterface is not false then --                      if something is chosen, but not if cancel is clicked.
  if chosenInterface is {} then set chosenInterface to interfaceList
  repeat with theInterface in chosenInterface
    if legacySupport or tigerSupport then
      set netid to word 1 of theInterface
    else
      set netid to (do shell script "networksetup -listallhardwareports | grep -A1 'Hardware Port: " & theInterface & "$'| tail -n1| sed 's/^.*: //'")
    end if
    
    try
      set theIP to (do shell script "ipconfig getoption " & netid & " server_identifier")
      --    set DNSlookup to " (" & (do shell script "nslookup " & theIP & " | grep name| sed 's/.*name = //'") & ")"
      set resultList to resultList & {"" & theInterface & ":" & tab & theIP & DNSlookup}
    on error errorText --                           Not connected or some other error
      set resultList to resultList & {"" & theInterface & ":" & tab & "-"}
    end try
  end repeat
  set AppleScript's text item delimiters to return
  if legacySupport then
    display dialog (resultList as text) with title "DHCP Server Lookup" buttons {"OK"} default button "OK"
  else
    display alert "DHCP Server Lookup" as informational message (resultList as text) buttons {"OK"} default button "OK"
  end if
end if
I've tested it on Tiger (10.4) and Leopard (10.5), but it may work on older versions of Mac OS X, too. Due to the differences in systems older than Leopard however, the script can't look up the friendly interface name (Built-in Ethernet, AirPort, Firewire and so on), but lists the interface UNIX name (en0, en1, fw0 etc). It does make a wild guess at which interface it is, though, based on the usual assignments.

You can copy and paste the above into Script Editor and save it as an application, or download a ready-to-use version from this post on my site.

[robg adds: This script worked as described when I tested it.]

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080518160815998