Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Find the source of DHCP-provided addresses via script Network
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.]
    •    
  • Currently 2.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[12,983 views]  

Find the source of DHCP-provided addresses via script | 10 comments | Create New Account
Click here to return to the 'Find the source of DHCP-provided addresses via script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Find the source of DCHP-provided addresses via script
Authored by: hamarkus on May 21, '08 07:57:38AM

I get this error message:
Can't get every <<class intf>> of <<class netp>>
MBP 10.5.2 via Airport to Time Capsule to cable modem.



[ Reply to This | # ]
Find the source of DCHP-provided addresses via script
Authored by: ddauerbach on May 21, '08 07:58:53AM

Me too, for what that's worth.



[ Reply to This | # ]
Works for me
Authored by: simbalala on May 21, '08 08:26:33AM

OS X 10.4.11, No problems.



[ Reply to This | # ]
Find the source of DCHP-provided addresses via script
Authored by: RickoKid on Jun 08, '08 08:37:43AM
Yeah - that's weird, because that function is Leopard only - but you're running Leopard!

Anyway - since someone pointed out that this info is available in System Profiler, I've rewritten the script to get the info from there, so now this error shouldn't happen because it doesn't use System Preferences scripting. The new version is on my blog: http://www.torrfamily.org/tumbleseed/products/dhcp-server-lookup/

---
Visit my technology (and Apple in particular) blog at http://tumbleseed.torrfamily.org/

[ Reply to This | # ]

Find the source of DCHP-provided addresses via script
Authored by: simsamsep on May 21, '08 08:19:01AM

This is what I get (with Intel iMac, 10.4.11, Airport, adsl), does not mean anything to me:

lo0 Loopback: -
en1 (Airport?): -
wlt1: -



[ Reply to This | # ]
Find the source of DCHP-provided addresses via script
Authored by: operator207 on May 21, '08 08:50:18AM
10.5.2 MBP Penryn:


>cat get-dhcp-server-ip.sh
#!/bin/sh
echo "en0 DHCP -- Ethernet"
ipconfig getoption en0 server_identifier
echo " "
echo "en1 DHCP -- Wireless"
ipconfig getoption en1 server_identifier


Output:

>./get-dhcp-server-ip.sh
en0 DHCP -- Ethernet
ipconfig_get_option failed, (os/kern) failure

en1 DHCP -- Wireless
10.4.1.113


Maybe you guys are not looking at the correct interface? ipconfig -a

[ Reply to This | # ]
Find the source of DCHP-provided addresses via script
Authored by: Peter_T on May 21, '08 12:32:19PM

I might misunderstand the tip, but isn't the source of a dhcp provided ip adress part of the info in system profile (under the network part)?



[ Reply to This | # ]
Find the source of DCHP-provided addresses via script
Authored by: owsla on May 21, '08 06:32:55PM

Yup! Verified on 10.5.2



[ Reply to This | # ]
Find the source of DCHP-provided addresses via script
Authored by: RickoKid on Jun 08, '08 08:47:05AM
Yes it is - I never knew that! Anyway - I've updated the script to get rid of some of the bugs, and it now uses system_profiler so the interface names should be right and it should work on most versions of Mac OS X.

The updated script is on my blog: http://www.torrfamily.org/tumbleseed/products/dhcp-server-lookup/

---
Visit my technology (and Apple in particular) blog at http://tumbleseed.torrfamily.org/

[ Reply to This | # ]

It is also easy to get similar information via a simple terminal command.
Authored by: st3phen on May 23, '08 03:56:15PM

ipconfig getpacket [ifaddr]

For example, on Ethernet 0:
ipconfig getpacket en0

This returns the DHCP information, including the DHCP server's address info, any Option 95 information. Here's example information for my MacBook Pro:

op = BOOTREPLY
htype = 1
flags = 0
hlen = 6
hops = 0
xid = 1301709434
secs = 1
ciaddr = 0.0.0.0
yiaddr = 192.168.15.100
siaddr = 0.0.0.0
giaddr = 0.0.0.0
chaddr = 0:12:34:56:78:9A
sname =
file =
options:
Options count is 9
dhcp_message_type (uint8): ACK 0x5
server_identifier (ip): 192.168.15.253
lease_time (uint32): 0x54600
renewal_t1_time_value (uint32): 0x2a300
rebinding_t2_time_value (uint32): 0x49d40
subnet_mask (ip): 255.255.255.0
router (ip_mult): {192.168.15.254}
domain_name_server (ip_mult): {192.168.15.253}
end (none):



[ Reply to This | # ]