Some links on getting MacOS network interfaces and DHCP information
Posted by jpluimers on 2021/07/30
One day I’ll put this in a script that shows all DHCP information for all network interfaces.
For now some links I will need when writing that script.
Many of the below commands are also in [WayBack/Archive] Command-Line Tools: The Missing Manpages (Mac OS X for Unix Geeks).
Shows all interfaces:
networksetup -listallhardwareports
Show any DHCP server on any interface (but does not list interfaces):
system_profiler SPNetworkDataType | grep "Server Identifier"
- [WayBack/Archive] Find the IP address of your DHCP server. – Apple Community
Other options would be:
system_profiler SPNetworkDataType | grep "Server Identifier"
- [WayBack/Archive] Crabeater: Stateful Parser in Python
I came up with a method for parsing the output of system commands that require knowledge of previous lines to provide context. An example of this is parsing the MAC addresses for interfaces on a Mac running OS X Leopard using the system_profiler command. The output of the command has various levels of sections. The first level of section headers aren’t indented at all. The next level is indented four spaces and the next six spaces.
For this task I want to parse out the interface names, which are indented four spaces, but there are plenty of similar subsection headers that are not interfaces. The context I need is whether or not the current line is in the Network section.
I came up with a way to implement this where the Parser is a class and has an instance variable named “state” that holds the correct method to use to parse the next line.
Notice that h2Pattern matches any subsection header, but I only use it to match a line if the line is inside the Network section (i.e. it is only used inside the network method.)
For a NIC en0
, shows the most recent DHCP packet information:
ipconfig getpacket en0
- [WayBack/Archive] Find and display a DHCP server’s IP address – Mac OS X Hints
According to the manual page for ipconfig, this command appears to be unique to Mac OS X, first introduced in the Public Beta! The command will display a bunch of useful info, including:
- server_identifier (ip): That’s your DHCP server’s IP address.
- yiaddr: Your machine’s IP address.
- chaddr: Your machine’s MAC address.
- domain_name_server: Your domain name server(s).
There’s much more in the output. Keep in mind that if you have entered an IP address manually, then you’ll get no output from this command.
- [WayBack/Archive] Find the IP address of your DHCP server. – Apple Community
Other options would be:
system_profiler SPNetworkDataType | grep "Server Identifier"
and to narrow down the one you used:
ipconfig getpacket en0 | grep siaddr
ipconfig getpacket en1 | grep siaddr
There may be some mileage in using ifconfig as well? If you’re connected over Wi-Fi holding down the option key whilst clicking on the Airport Icon in the Menu Bar will also show you the WAP’s IP address, which in some cases will also be the DHCP server.
- [WayBack/Archive] Viewing DHCP information on an OS X system
$ ipconfig getoption en0 server_identifier 192.168.2.1 $ ipconfig getoption en0 router 192.168.2.1 $ ipconfig getoption en0 subnet_mask 255.255.255.0 $ ipconfig getoption en0 domain_name_server 192.168.2.1 $ ipconfig getoption en0 lease_time 86400 $ ipconfig getoption en0 domain_name $
…
$ ipconfig getpacket en0 op = BOOTREPLY htype = 1 flags = 0 hlen = 6 hops = 0 xid = 0xceb36b13 secs = 0 ciaddr = 0.0.0.0 yiaddr = 192.168.2.51 siaddr = 0.0.0.0 giaddr = 0.0.0.0 chaddr = ac:bc:32:ce:d3:f5 sname = file = options: Options count is 7 dhcp_message_type (uint8): ACK 0x5 server_identifier (ip): 192.168.2.1 lease_time (uint32): 0x15180 subnet_mask (ip): 255.255.255.0 router (ip_mult): {192.168.2.1} domain_name_server (ip_mult): {192.168.2.1} end (none): $
- [WayBack/Archive] ipconfig Man Page – macOS – SS64.com
COMMANDS The ipconfig utility provides several commands: waitall Blocks until all network services have completed configuring, or have timed out in the process of configuring. This is only useful for initial system start-up time synchronization for legacy network services that are incapable of dealing with dynamic network configuration changes. getifaddr interface-name Prints to standard output the IP address for the first net- work service associated with the given interface. The output will be empty if no service is currently configured or active on the interface. ifcount Prints the number of interfaces that IPConfiguration is capa- ble of configuring. The value that's printed will not change unless relevant network interfaces are either added to or removed from the system. getoption interface-name (option-name | option-code) Prints the BOOTP/DHCP option with the given name or option code integer value. See bootpd(8) for option code names. If an option has multiple values e.g. domain_name_server, only the first value is printed. getpacket interface-name Prints to standard output the DHCP/BOOTP packet that the client accepted from the DHCP/BOOTP server. This command is useful to check what the server provided, and whether the values are sensible. This command outputs nothing if DHCP/BOOTP is not active on the interface, or the attempt to acquire an IP address was unsuccessful. getv6packet interface-name Prints to standard output the latest DHCPv6 packet that the client accepted from the DHCPv6 server. In the case of stateful DHCPv6, it corresponds to the last packet from the server that contained addressing information. This command is useful to check what the server provided, and whether the values are sensible. This command outputs nothing if DHCPv6 is not active on the interface. set interface-name NONE set interface-name (DHCP | BOOTP) set interface-name (MANUAL | INFORM) ip-address subnet-mask set interface-name NONE-V6 set interface-name AUTOMATIC-V6 set interface-name MANUAL-V6 ipv6-address prefix-length set interface-name 6TO4 Sets the interface to have a new temporary network service of the given type. Any existing services on the interface for the particular protocol (IPv4 or IPv6) are first de-config- ured before the new service is instantiated. If NONE is specified, all existing IPv4 services are de-con- figured. If NONE-V6 is specified, all existing IPv6 services are de-configured. DHCP and BOOTP require no additional arguments. The IP address, subnet mask, router, and DNS information are retrieved automatically. Both MANUAL and INFORM require the specification of an IP address ip-address and a subnet mask subnet-mask. The INFORM service configures the IP address statically like MANUAL, but then broadcasts DHCP INFORM packets to retrieve DHCP option information. If the DHCP server responds and sup- plies a subnet mask, that subnet mask is used instead of the specified subnet-mask. AUTOMATIC-V6 requires no additional arguments. The IPv6 address, prefix length and router are retrieved automati- cally. MANUAL-V6 requires the specification of the IPv6 address ipv6-address and a prefix length prefix-length. 6TO4 only works on Six To Four (IFT_STF) interfaces e.g. stf0. If it is specified on a non-IFT_STF interface, it has the same effect as specifying NONE-V6. The set command requires root privileges. Note: The set command is very useful for debugging, but it can't be used to configure a persistent service. The tempo- rary services that are created only remain until the next network configuration change occurs. See scselect(8). setverbose level Sets verbose mode logging in the IPConfiguration agent. Spec- ify a level value of 0 to disable verbose logging, the default. Specify a value of 1 to enable verbose logging. When enabled, useful debugging information is logged using syslog(3) with level LOG_NOTICE. In addition, the packet trace file /var/log/com.apple.IPConfiguration.bootp is opened and BOOTP/DHCP packets that are sent and received are printed to the file. The setverbose command requires root privileges.
Examples
Display your routers ip address:
$ ipconfig getoption en0 router
192.168.0.1Renew DHCP Lease
$ sudo ipconfig set en0 DHCPDisplay all DHCP configuration details:
$ ipconfig getpacket en0 op = BOOTREPLY htype = 1 dp_flags = 0 hlen = 6 hops = 0 xid = 1956115059 secs = 0 ciaddr = 0.0.0.0 yiaddr = 192.168.4.10 siaddr = 192.168.4.1 giaddr = 0.0.0.0 chaddr = 0:3:93:7a:d7:5c sname = dhcp.mycompany.net file = options: Options count is 10 dhcp_message_type (uint8): ACK 0x5 server_identifier (ip): 192.168.4.1 lease_time (uint32): 0x164a subnet_mask (ip): 255.255.255.0 router (ip_mult): {192.168.0.1} domain_name_server (ip_mult): {8.8.4.4} domain_name (string): mycompany.net end (none): Display the DNS Server: $ ipconfig getoption en0 domain_name_server 8.8.4.4 Clear DNS Cache: $ sudo killall -HUP mDNSResponder
–jeroen
Leave a Reply