# Script to get network info on Mac

The *Network* option in the *System Preferences* provides the basic network information about the macOS device. Alternatively, you can also use scripts to obtain the network-specific details of Mac. Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action lets you deploy these scripts to fetch the network info of remote macOS devices in bulk.

Scripting language – Bash

File extension – .sh

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

Find MAC address
----------------

Script to find MAC address

networksetup -listallhardwareports 

   1

  networksetup -listallhardwareports 

   

 

  

Execute the script above to list the MAC address of all hardware ports of the macOS device.

[![MAC address of all hardware ports](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/03/List-hardware-ports.jpg) ](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/03/List-hardware-ports.jpg)Script to find MAC address

networksetup -listallhardwareports 

   1

  networksetup -listallhardwareports 

   

 

  

Replace `<hardware port>` with the hardware port to display the MAC address of the hardware port provided. `en0` refers to the device’s physical network interfaces. Most new macOS devices will have `en0` as the Wi-Fi connection and `en1` as the wired connection.

For example, `networksetup -getmacaddress en0 `

[![MAC address of the hardware port en0](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/03/MAC-address-of-en0.jpg) ](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/03/MAC-address-of-en0.jpg)Find IP address
---------------

Script to find MAC address

ipconfig getifaddr <interface> 

   1

  ipconfig getifaddr <interface> 

   

 

  

For Wi-Fi, replace `<interface>` with `en0`, and the device’s local IP address will be retrieved. Replace `<interface>` with `en1`, and the local IP address of the wired connection of the device will be fetched.

For example, `ipconfig getifaddr en0 `

[![IP address of the hardware port en0](https:2023/03/IP-address.jpg) ](https:2023/03/IP-address.jpg)View network info 
------------------

The command below will display a brief overview of the device’s network information.

Script to display an overview of the network information

ifconfig

   1

  ifconfig

   

 

  

[![Network info of the device](https:2023/03/Network-Info.jpg) ](https:2023/03/Network-Info.jpg)View the subnet mask of en0:

Script to display the subnet mask of en0

ipconfig getoption en0 subnet\_mask 

   1

  ipconfig getoption en0 subnet\_mask 

   

 

  

[![Subnet mask of the hardware port en0](https:2023/03/Subnet-mask.jpg) ](https:2023/03/Subnet-mask.jpg)View the DNS server for en0:

Script to view DNS server number

ipconfig getoption en0 domain\_name\_server

   1

  ipconfig getoption en0 domain\_name\_server

   

 

  

[![DNS server of the hardware port en0](https:2023/03/DNS-server.jpg) ](https:2023/03/DNS-server.jpg)View port info 
---------------

The below command returns only TCP connections on a remote Mac, including open and active ports.

Script to list TCP connections

netstat -ap TCP

   1

  netstat -ap TCP

   

 

  

[![Port info of all the internet connections](https:2023/03/Port-Info.jpg) ](https:2023/03/Port-Info.jpg)Executing the command below will retrieve the open ports with the keyword “LISTEN”.

Script to list TCP connections

netstat -a | grep -i "LISTEN"

   1

  netstat -a | grep -i "LISTEN"

   

 

  

You can replace `LISTEN` with other keywords to modify the result. Examples of different keywords are `ESTABLISHED` and `TIME_WAIT`.

[![List of open ports with keyword ‘LISTEN’](https:2023/03/Port-info-with-keyword.jpg) ](https:2023/03/Port-info-with-keyword.jpg)Display ARP table
-----------------

The Address Resolution Protocol (ARP) is a utility that displays the Internet-to-Ethernet address translation tables. Every line displays the IP address and the corresponding physical MAC address of each host on the network. Execute the script below to display the ARP table.

Script to display ARP table

arp -a

   1

  arp -a

   

 

  

[![ARP table of the device](https:2023/03/ARP-table.jpg) ](https:2023/03/ARP-table.jpg) Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.