Need help with script. PLEASE :)

tommmy_s
New Contributor

I am looking for a "reliable" way to find out what network is the computer using. 
I think I have the WiFi part working.

check_wifi_status() {
    local wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | awk '/Device:/ {print $2}')
    if [ -n "$wifi_interface" ]; then
        local wifi_status=$(networksetup -getairportpower "$wifi_interface" | awk '{print $4}')
        if [ "$wifi_status" = "On" ]; then
            local ssid=$(networksetup -getairportnetwork "$wifi_interface" | awk -F': ' '{print $2}')
            echo "WiFi status ($wifi_interface): On"
            echo "Connected to network ($wifi_interface): $ssid"
        else
            echo "WiFi status ($wifi_interface): Off"
        fi
    else
        echo "WiFi interface not found"
    fi
}


With ethernet, users can be using different brand name docking station and also other network adapter/card may not be an actual working ethernet adapter. Some could be for ZTNA or Netskope or some other service using an adapter. 

I am looking for help with identifying if computer is connected to Ethernet or not (regardless of adapter/dock brand). Also looking for an active one only. Bonus if we can also tell which one is the primary one. 

Thanks all! 

3 REPLIES 3

AJPinto
Honored Contributor III

en0 will always be Wi-Fi, it used to be ethernet but that changed some time back. Now Ethernet will be one of the later en# network interfaces, on my device its en6 (using a dock). If a network adapter has an IP address, that is a very good determining factor on if that network adapter has a life network connection or not. I don't have a script for this, but that is where I would start poking. Basically, using networksetup to list all adapters, and grep what you want to see then awk out stuff you don't and echo the results. 

 

Your bonus topic is wanting to know macOS's Network Service Order. I use the script below to set Wi-Fi as the top Network Service, as well as echoing the service order when done. macOS will really do whatever it wants unless this is specified, and even when specified macOS will still pretty much do what it wants.

 

 

#!/bin/bash

######################
# Script Name: Configuration_Order_Network_Services
# Author: 
# Date: 
# Purpose: Promote the desired network Service to the 1st in the service order list
######################


######################
# Global Variables
######################


# echos the current network service order before the script makes changes. grep needs to be cleaned up.
Adapter_all=$(networksetup -listnetworkserviceorder | awk '{print $2}')

echo " "
echo "*********************************************"
echo "Being script:"
echo "*********************************************"
echo " "
echo "==============="
echo "Current Network Service order:"
echo "==============="
echo "$Adapter_all"
echo "==============="
echo " "
echo " "

new_top_net_service_name='Wi-Fi' # Change this to the name of the new desired top network service.


######################
# Script function
######################


echo " "
new_net_service_order="$(networksetup -listallnetworkservices | AWK_ENV_TOP_NET_SERVICE="${new_top_net_service_name}" awk '{ if (NR == 1) { print ENVIRON["AWK_ENV_TOP_NET_SERVICE"] } else if ($0 != ENVIRON["AWK_ENV_TOP_NET_SERVICE"]) { print $0 } }')"
	echo "==============="
	echo "New Network Service Order:"
    echo "==============="
    echo "$new_net_service_order"
	echo "==============="
	IFS=$'\n'
	networksetup -ordernetworkservices ${new_net_service_order}
	unset IFS
	
    
echo " "
echo "*********************************************"
echo "End script:"
echo "*********************************************"
exit 0

 

jwbeatty
New Contributor III

I don't have a script for this, but you could start by listing all the network hardware using networksetup. Ethernet connections will start with en. Then use ifconfig to get the details of each hardware connection. For example, my ethernet connection on a dock is en4, so I would use ifconfig en4 to get the connection details. Active connections will have a status listing of active and inet and inet6 entries.

aparten
New Contributor III

This is similar to what I do for an extension attribute, using the below. (I'm not entirely sure if it's best practices to create/edit a text file in an EA, but it works great for us!)

*Note the location of the file it outputs from networksetup, you'd want to adjust this to a location that is readable on all your computers.

#!/bin/bash

## Adjust location for your environment
touch /Users/Shared/OIT/inetconf
file=/Users/Shared/OIT/inetconf

deviceid=`networksetup -listallhardwareports | grep -A2 "Hardware Port.*Ethernet" | grep -e "Device" | awk {'print $2'} | grep -e "en"`

echo "$deviceid" > $file

active_interface=`route get google.com | grep interface | awk {'print $2'}`

if grep -q $active_interface "$file" ; then
	echo "<result>Ethernet Active</result>"
else 
	echo "<result>Ethernet Inactive</result>"
fi

exit 0

 

-Alex