How to display user's IP address on the client machine

znilsson
Contributor II

We're a pretty large organization, and when a user calls our help desk one of the first things they ask for is the IP address of the machine they're on. A lot of our Mac users don't know how to get that information, even though it can be explained to them a better way would be to have a big button in Self Service that says "What is my IP Address" or something.

Casper already knows each user's IP address. Is there a way to leverage that information into a script that displays it for the end user when they click the button in self service? I already have a script that uses jamfhelper to put up a hud window type, I can modify it to display the user's IP address, I assume, if I can find a way to get the IP address in the first place. And possibly to copy it to the user's clipboard when they click an OK button in the window that pops up.

And before you ask, no, the help desk does not have access to casper and that will probably change in the future, but for right now I need a way to utilize Self Service to display an end user's IP address for them. Any ideas?

2 ACCEPTED SOLUTIONS

PeterG
Contributor II

Couldn't resist. did this today.

this will give you consecutive popups for any active IP addresses.
I use detecting a subnet to tell me if that interface has an IP.

#! /bin/bash

# Name: MyIP.sh
# Version:  1.0
# Author:  Peter Gawlocki

SUBonTB=`networksetup -getinfo Thunderbolt Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonWiFi=`networksetup -getinfo Wi-Fi|grep Subnet| wc |awk '{print $1}'`
SUBonEnet=`networksetup -getinfo Ethernet|grep Subnet| wc |awk '{print $1}'`


if [ $SUBonEnet = 1 ]
    then
        ReportEnetAddress=`networksetup getinfo Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportEnetAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonWiFi = 1 ]; then
        ReportWiFiAddress=`networksetup getinfo Wi-Fi|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Wireless is: "$ReportWiFiAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonTB = 1 ]; then
        ReportTBENAddress=`networksetup getinfo Thunderbolt Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportTBENAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

View solution in original post

jhbush
Valued Contributor II

@PeterG thanks for posting this. I added in the Display Ethernet my designers use.

#! /bin/bash

# Name: MyIP.sh
# Version:  1.0
# Author:  Peter Gawlocki

SUBonTB=`networksetup -getinfo Thunderbolt Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonDTB=`networksetup -getinfo Display Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonWiFi=`networksetup -getinfo Wi-Fi|grep Subnet| wc |awk '{print $1}'`
SUBonEnet=`networksetup -getinfo Ethernet|grep Subnet| wc |awk '{print $1}'`


if [ $SUBonEnet = 1 ]
    then
        ReportEnetAddress=`networksetup getinfo Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportEnetAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonWiFi = 1 ]; then
        ReportWiFiAddress=`networksetup getinfo Wi-Fi|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Wireless is: "$ReportWiFiAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonTB = 1 ]; then
        ReportTBENAddress=`networksetup getinfo Thunderbolt Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportTBENAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonDTB = 1 ]; then
        ReportTBENAddress=`networksetup getinfo Display Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Display Ethernet is: "$ReportTBENAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

View solution in original post

43 REPLIES 43

activitymonitor
New Contributor III

We have it up in the menu bar on all our machines.

http://www.monkeybreadsoftware.de/Software/IPinmenubar.shtml

znilsson
Contributor II

@activitymonitor unfortunately that would involve distributing it to all our mac clients, then instructing the end users on what to look for, and then instructing the help desk to ask for the user to get it from that menu item. It's not really practical for our purposes, and would most likely cause more confusion than ever. Our help desk doesn't really have a good grasp on English or on Macs, so I'm trying to keep it as simple as possible so that the help desk doesn't need to learn anything new, and the user has an easy option using a mechanism they're already familiar with, namely Self Service.

jhbush
Valued Contributor II

@znilsson, this is one I use from Rusty at PSU.

#!/bin/bash
################################################################################
#Display IP Address and Hostname of Computer
#Written to be used with Playtpus to create a app.
#Written by Rusty Myers
################################################################################

################################################################################
#Functions
################################################################################

function testinterface {
    en0test=`ifconfig en0 | grep -q 'inet ' && echo en0`

    en1test=`ifconfig en1 | grep -q 'inet ' && echo en1`

    # echo $en0test
    # echo $en1test

    if [ "$en0test" == "en0" ]
        then
            activeinterface=en0
    elif [ "$en1test" == "en1" ]
        then
            activeinterface=en1
    else
            activeinterface=None
    fi
}

function displayip {

if [ "$activeinterface" == "None" ]
    then
        ipaddress="None"
    else
        ipaddress=`ipconfig getifaddr $activeinterface`
fi

}

function displaydialog {

echoserial=`/usr/bin/osascript << EOT
tell app "System Events"
     Activate
     display dialog "Your IP address is $ipaddress. Your computer name is $computername" buttons "OK" default button 1 with title "IP Address and Computer Name"
end tell                           
EOT`

echo $echoserial

}


################################################################################
#Variables
################################################################################

computername=`scutil --get ComputerName`

################################################################################
#
################################################################################

testinterface

displayip

displaydialog

PeterG
Contributor II

I'd probably write a script that checks for ethernet enabled and pulls the IP address and hands it to jamf helper to display in a popup. Put it in self service and then ask the user to press the button.

If you wanted to get fancier you could use network setup to detect if they are on Ethernet, Airport, Thunderbolt Ethernet, etc... and pull the active one.

start with an If statement looking for ... networksetup -getnetworkserviceenabled Ethernet
to return Enabled.

then a ReportIpAddress=networksetup getinfo Ethernet|grep IP| awk '{print $3}' to get the numher

then send that info to jamf helper.
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportIpAddress -button1 "Ok" -cancelButton 1 -timeout 10;

Kevin
Contributor II

Jason, thanks for the script. It returns the IP address correctly on devices that only have one NIC active, but on some systems, it returns the information twice. It appears it is drawing a dialog for each active network interface, but only provides the IP address of en0.

Have you seen that behavior? Any way to easily return the values of each active NIC?

PeterG
Contributor II

Couldn't resist. did this today.

this will give you consecutive popups for any active IP addresses.
I use detecting a subnet to tell me if that interface has an IP.

#! /bin/bash

# Name: MyIP.sh
# Version:  1.0
# Author:  Peter Gawlocki

SUBonTB=`networksetup -getinfo Thunderbolt Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonWiFi=`networksetup -getinfo Wi-Fi|grep Subnet| wc |awk '{print $1}'`
SUBonEnet=`networksetup -getinfo Ethernet|grep Subnet| wc |awk '{print $1}'`


if [ $SUBonEnet = 1 ]
    then
        ReportEnetAddress=`networksetup getinfo Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportEnetAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonWiFi = 1 ]; then
        ReportWiFiAddress=`networksetup getinfo Wi-Fi|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Wireless is: "$ReportWiFiAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonTB = 1 ]; then
        ReportTBENAddress=`networksetup getinfo Thunderbolt Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportTBENAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

jhbush
Valued Contributor II

@PeterG thanks for posting this. I added in the Display Ethernet my designers use.

#! /bin/bash

# Name: MyIP.sh
# Version:  1.0
# Author:  Peter Gawlocki

SUBonTB=`networksetup -getinfo Thunderbolt Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonDTB=`networksetup -getinfo Display Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonWiFi=`networksetup -getinfo Wi-Fi|grep Subnet| wc |awk '{print $1}'`
SUBonEnet=`networksetup -getinfo Ethernet|grep Subnet| wc |awk '{print $1}'`


if [ $SUBonEnet = 1 ]
    then
        ReportEnetAddress=`networksetup getinfo Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportEnetAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonWiFi = 1 ]; then
        ReportWiFiAddress=`networksetup getinfo Wi-Fi|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Wireless is: "$ReportWiFiAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonTB = 1 ]; then
        ReportTBENAddress=`networksetup getinfo Thunderbolt Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportTBENAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonDTB = 1 ]; then
        ReportTBENAddress=`networksetup getinfo Display Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Display Ethernet is: "$ReportTBENAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

jhbush
Valued Contributor II

@Kevin I use that script wrapped as an app on my NetBoot sets. @PeterG may be a better choice for Self Service.

znilsson
Contributor II

Freaking fantastic. That is exactly what I needed, thanks so much. Confirmed it works using different interfaces and everything.

PeterG
Contributor II

@jhbush1973][/url as I was describing what I wrote to Sunny, our other CCA, she said, "Why don't you wrap it and put it in their Applications folder?" heh... we are all on the same page.... love JAMF Nation.

I am looking to populate self service with as much helpful stuff I can... because I can't hire staff :-(

I use the subnet detect thing when I wrote another script for controlling which Thunderbolt Ethernet Adapter can be used with which computer.

Since our 'current' network security uses MAC address to allow/deny access I can't give out a TbEn adapter to a user and have them bring in a non-company computer and use the company adapter and get on the network.

The script compares the MAC address of the adapter to a plist that we 'hid' and uses jamf helper to display a message if a mismatch is found. It also shuts down the TbEn Interface to make it unusable. It is a launched that runs every 15 seconds. (checks if the TbEn interface pulls an IP) then Acts, if needed.

Kevin
Contributor II

Interesting… I put this in Self Service and it works wonderfully.
However… on my Desktop–and so far ONLY on my Desktop, the results are returned as expected, but they are returned three times.

Any ideas?

PeterG
Contributor II

Are your results exactly the same (wording)? (like Ethernet repeating 3 times?
If so, and you get ethernet three times... if you turn on WiFi also do you get: Alerts like this order... Ethernet, WiFi, Ethernet, WiFi, Ethernet, WiFi?
or Ethernet, Ethernet, Ethernet, WiFi, WiFi, WiFi, ?

Do you have multiple interfaces actively pulling IPs?

Since there is no loop in the script, it does not seem possible to run multiple times.
Is it added multiple times in the Policy?

Kevin
Contributor II

Yes, exact wording three times. I have two NICs active and I get the results returned three times for each. If I run the script in Terminal, it only returns results once. Just as a policy does it behave this way…
Every other Mac I run it on in Self Service only returns results once. I have tried it on several Macs now - they all work fine. Same policy, pulling the same script.

johnnasset
Contributor

Same here. I get a total of 4 pop ups-2 for each connected interface.

jhbush
Valued Contributor II

Running this via Self Service works hit or miss right now and I'm not really sure why. The policy shows as succeeded, but the JAMF helper boxes don't show up.

tlarkin
Honored Contributor

Hi Everyone,

Saw this thread today and thought some cool stuff was going on so I marked it for later. I whipped up an example of how you can display all Network IP addresses to an end user. Just in case they happened to be connected to WiFi, their HotSpot, Ethernet, etc.

#!/bin/bash

# dispaly information to end user

# set IFS for new line
# use unset IFS to reset it to default value

IFS=$'
'

# get list of networkservices exlcuding disabled services

NetServices=$(networksetup -listallnetworkservices | grep -v '*')


for service in ${NetServices} ; do

echo "${service} is currently assigned: $(networksetup -getinfo ${service} | awk '/IP address:/ { gsub("[a-z]", ""); print $3 }' | sed 's/://g')" >> /tmp/netinfo.txt

done

# applescript to display dialog

osascript <<AppleScript
tell application "Finder"
 activate
 display dialog "Your Network Information is:
  $(cat /tmp/netinfo.txt)" buttons {"OK"} default button 1
end tell
AppleScript

unset IFS

# clean our temp file

srm /tmp/netinfo.txt

exit 0

What it looks like:

external image link

Unfortunately, I had a really hard time getting all that text to fit in my JAMF Helper examples. Like I said, not sure if this is even of interest to you all, but I figured if you displayed everything to an end user and they started reading off their IPs and services the Help Desk person would know what is best to connect to. Like if they happen to drag their service order to WiFi above Ethernet and the Help Desk person wants to remote in to install software, they would preferably want to do that over Ethernet.

Sorry for the pipes, my awk skills are slipping I had to pipe to sed to get rid of a pesky ":" that kept getting tossed in output. I also just loop through all Network Services that are currently enabled. That way you don't have to write specific code looking for specific network adapters.

EDIT - hooked up my hotspot to show how it will display multiple IPs when connected to multiple services, also should have mentioned I purposely disabled all my FW networking interfaces as a proof of concept so that is why they are excluded:

external image link

Hope this helps out

Thanks
Tom

mm2270
Legendary Contributor III

I thought I'd throw in another variation here. This one uses bash arrays, will report on any hardware ports with active IP addresses and skip the rest for clarity, and uses cocoaDialog for the messaging, although in theory you could easily use AppleScript or jamfHelper with this just as well.

#!/bin/bash

## Name:     show-my-ips.sh

## Path to cocoaDialog. Customize to your environment
cdPath="/path/to/cocoaDialog.app/Contents/MacOS/cocoaDialog"

## Get the Mac's name
MACNAME=$(scutil --get ComputerName)

IFS=""

## Place all hardware ports into array
while read port; do
    HWPORTS+=( "$port" )
done < <(echo $(networksetup -listallhardwareports | awk -F': ' '/Hardware Port/{ print $NF }'))

## Place all device ids into array
while read device; do
    DEVICES+=( "$device" )
done < <(echo $(networksetup -listallhardwareports | awk '/Device/{ print $NF }'))

## Loop over hardwareport array checking for active IP and add any to new array along with device IDs
for ((i = 0; i < ${#HWPORTS[@]}; i++)); do
    IP=$(ipconfig getifaddr ${DEVICES[$i]})
    if [[ ! -z "$IP" ]]; then
        IPADDRESSES+=("${HWPORTS[$i]} (${DEVICES[$i]}):        ${IP}
")
    fi
done

## Reset IFS to space
IFS=" "

## Clean up final array for message
ACTIVEIPS=$(echo -e "${IPADDRESSES[@]}" | sed 's/^ *//')

MESSAGE="The following are details about your computer:

Mac Name:
${MACNAME}

Device Name (Port):     IP Address:
${ACTIVEIPS[@]}"

"$cdPath" msgbox 
    --title "My IP Addresses" 
    --text "Details about your Mac" 
    --informative-text "$MESSAGE" 
    --button1 "   OK   " 
    --icon network

A screen shot of what it looks like. It would normally report the Mac name, but I removed that for the image.
external image link

wmateo
Contributor

I think IPMENUBAR works great. sure, there is the task of distributing it and executing it on the endpoint, but its very simple and in the users menu bar all the time. there is not much instruction the user will need other than look on your top right for a number that begins with your x.x.x.(depending on the class network you are using) - all my users (even the technically challenged ones) can see this number very quickly.

jhbush
Valued Contributor II

@mm2270 thanks for this version do you need to add an ```
exit 0

```
if [ "$rv" == "1" ]; then
    echo "User said OK"
exit
    fi

mm2270
Legendary Contributor III

@jhbush1973 - It depends. Since my message only has an "OK" button, I'm not particularly concerned about whether a user clicked it or not. (they would have to click it eventually)
I'd only care if there was more than one button and I needed to take some action or report on what button was clicked.
That said, its usually a good idea to include an exit in a script so we could include exit or exit 0 at the end.

jhbush
Valued Contributor II

@mm2270 the reason I ask is the script exits 1 and I thought maybe JAMF would see it as a failure.

PeterG
Contributor II

Heh... I like youze guys solutions better... <snag>

This place is great... any of you scripting whiz types want to assist with this one? It's driving me nuts!

See https://jamfnation.jamfsoftware.com/discussion.html?id=5238 and https://jamfnation.jamfsoftware.com/discussion.html?id=11226

mm2270
Legendary Contributor III

@jhbush1973 - I see what you mean. That's cocoaDialog reporting that the "OK" button was clicked, not the exit code for the script. I think there's a silent flag in CD that we can throw in there that will make the "1" not show up, but either way, I don't think Casper would see that as a failure since its not the actual exit code. At least I would hope not. That would be just as bad as looking for keywords like "error" ;)
I guess the best test would be to run it from Self Service and see what the end result shows as, as in Failed or Succeeded, etc.

mm2270
Legendary Contributor III

Ok, found it. Had the wrong term though. Its called "--quiet" So just add this to the end of the cocoaDialog section and the "1" will not show up in stdout, in case its a concern. Not sure if the older version of CD has this, but the latest beta does.

Updated code for the cocoaDialog section would look like this-

"$cdPath" msgbox 
    --title "My IP Addresses" 
    --text "Details about your Mac" 
    --informative-text "$MESSAGE" 
    --button1 "   OK   " 
    --icon network **
    --quiet**

jhbush
Valued Contributor II

@mm2270 thanks for pointing that out. One thing I noticed is that it displays the info twice using Self Service. Run by itself the script exits showing the information once. I'm running the beta and I couldn't find that --quiet trigger in the documentation. Good news is that without the --quiet it exits successfully.

mm2270
Legendary Contributor III

Huh, so that script ALSO shows the dialog twice from SS? That's pretty odd, since its the same issue reported with the other scripts. That's got to be a bug in Self Service I think because there's no reason for it to do that.

As for documentation, the stuff up on the github page is very out of date. To view more current info (albeit no real descriptions of them) do something like...

/path/to/cocoaDialog.app/Contents/MacOS/cocoaDialog mgbox --help

…to see all the available flags for that window type. Just change the window type from msgbox to whatever else you want to view relevant info for, like progressbar, etc.

znilsson
Contributor II

Not familiar with cocoaDialog - is this an application that would need to be installed on all our Mac clients before this script would work?

mm2270
Legendary Contributor III

Yes, its a very flexible (albeit getting old) windowing utility. It allows for everything from simple dialogs, file selection windows (like when you use the :Open" command in an application), progress bars that can move with real progress and several other modes.
Get the last beta release from here:
http://mstratman.github.io/cocoadialog/
Choose the Download below the cocoaDialog (development) section. You can read up on it at that same location. Keep in mind that the documentation is mostly written for the older 2.x release, not the 3.x beta, which most of us here are using. The docs haven';t really been updated in some time, nor has the utility itself.

You can package it up to whatever location you want, like /Applications/Utilities/ or wherever and push it with a policy to your Macs. We place ours in /Library/Application Support/JAMF/bin/

Do a search here on JN for "cocoadialog" and you will likely pull up dozens of threads that cover its usage and plenty of existing script examples to get you started.

mm2270
Legendary Contributor III

Here's a slightly updated version that I haven't thoroughly tested, but should display the word (Default) after the IP with the service that is listed first in the service order, meaning its the default interface for net connection, even when there are multiple ones active.
So for example if someone has 2 active NICs, Ethernet and Wi-Fi and they've reordered their interfaces to make Wi-Fi the default even when Ethernet is active, it will display this (hopefully) in the dialog. I tested it with my iPhone USB connection and both Ethernet (built-in) and USB Ethernet on different Macs after switching the service order around in System Preferences > Network and it always displays the Default one as the service that was listed at the top in the Network Pref Pane.

I also changed it to use "unset IFS" instead of explicitly setting IFS to a space since that's the safer way to do it.

Let me know if it doesn't work or displays anything odd.

#!/bin/bash

## Name:     show-my-ips.sh

## Path to cocoaDialog. Customize to your environment
cdPath="/path/to/cocoaDialog.app/Contents/MacOS/cocoaDialog"

## Get the Mac's name
MACNAME=$(scutil --get ComputerName)

IFS=""

## Place all hardware ports into array
while read port; do
    HWPORTS+=( "$port" )
done < <(echo $(networksetup -listallhardwareports | awk -F': ' '/Hardware Port/{ print $NF }'))

## Place all device ids into array
while read device; do
    DEVICES+=( "$device" )
done < <(echo $(networksetup -listallhardwareports | awk '/Device/{ print $NF }'))

## Get the default interface ID
DEFAULT=$(/usr/sbin/netstat -rn -f inet | awk '/default/{print $NF; exit}')

## Loop over hardwareport array checking for active IP and add any to new array along with device IDs
for ((i = 0; i < ${#HWPORTS[@]}; i++)); do
    IP=$(ipconfig getifaddr ${DEVICES[$i]})
    if [[ ! -z "$IP" ]]; then
        if [[ "${DEFAULT}" == "${DEVICES[$i]}" ]]; then
            IPADDRESSES+=("${HWPORTS[$i]} (${DEVICES[$i]}):        ${IP}   (Default)
")
        else
            IPADDRESSES+=("${HWPORTS[$i]} (${DEVICES[$i]}):        ${IP}
")
        fi
    fi
done

## Reset IFS to space
unset IFS

## Clean up final array for message
ACTIVEIPS=$(echo -e "${IPADDRESSES[@]}" | sed 's/^ *//')

MESSAGE="The following are details about your computer:

Mac Name:
${MACNAME}

Device Name (Port):     IP Address:
${ACTIVEIPS[@]}"

"$cdPath" msgbox 
    --title "My IP Addresses" 
    --text "Details about your Mac" 
    --informative-text "$MESSAGE" 
    --button1 "   OK   " 
    --icon network 
    --quiet

znilsson
Contributor II

@PeterG I modified your script to include Ethernet 1 and Ethernet 2 ports, for accurate reporting on Mac Pros. Tested and confirmed working on a Late 2013 "trash can" Mac Pro.

#! /bin/bash

# Name: MyIP.sh
# Version:  1.0
# Author:  Peter Gawlocki

SUBonTB=`networksetup -getinfo Thunderbolt Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonDTB=`networksetup -getinfo Display Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonWiFi=`networksetup -getinfo Wi-Fi|grep Subnet| wc |awk '{print $1}'`
SUBonEnet=`networksetup -getinfo Ethernet|grep Subnet| wc |awk '{print $1}'`
SUBonEnet1=`networksetup -getinfo Ethernet 1|grep Subnet| wc |awk '{print $1}'`
SUBonEnet2=`networksetup -getinfo Ethernet 2|grep Subnet| wc |awk '{print $1}'`


if [ $SUBonEnet = 1 ]
    then
        ReportEnetAddress=`networksetup getinfo Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportEnetAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonEnet1 = 1 ]
    then
        ReportEnetAddress=`networksetup getinfo Ethernet 1|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet 1 is: "$ReportEnetAddress -button1 "Ok" -cancelButton 1 -timeout 10;   

fi

if [ $SUBonEnet2 = 1 ]
    then
        ReportEnetAddress=`networksetup getinfo Ethernet 2|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet 2 is: "$ReportEnetAddress -button1 "Ok" -cancelButton 1 -timeout 10;   

fi

if [ $SUBonWiFi = 1 ]; then
        ReportWiFiAddress=`networksetup getinfo Wi-Fi|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Wireless is: "$ReportWiFiAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonTB = 1 ]; then
        ReportTBENAddress=`networksetup getinfo Thunderbolt Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Ethernet is: "$ReportTBENAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

if [ $SUBonDTB = 1 ]; then
        ReportTBENAddress=`networksetup getinfo Display Ethernet|grep IP| awk '{print $3}'`
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -icon /System/Library/PreferencePanes/Network.prefPane/Contents/Resources/Network.icns -title "Your IP Address" -description "Your current IP address for Display Ethernet is: "$ReportTBENAddress -button1 "Ok" -cancelButton 1 -timeout 10; 

fi

jacob_salmela
Contributor II

You could also use GeekTool with the following script:

wifiOrAirport=$(/usr/sbin/networksetup -listallnetworkservices | grep -Ei '(Wi-Fi|AirPort)')
wirelessDevice=$(/usr/sbin/networksetup -listallhardwareports | awk "/$wifiOrAirport/,/Device/" | awk 'NR==2' | cut -d " " -f 2)
wirelessIP=$(ipconfig getifaddr $wirelessDevice)
wiredDevice=$(networksetup -listallhardwareports | grep -A 1 "Port: Ethernet" | sed -n 's/Device/&/p' | awk '{print $2}')
wiredIP=$(ipconfig getifaddr $wiredDevice)

echo "Wireless IP:	$wirelessIP"
echo "Wired IP:	$wiredIP"

external image link

tlarkin
Honored Contributor

Applescript can do that as well. I just did this as a quick and dirty POC. I am sure you can add/change things to this.

osascript -e 'display notification "Casper Time!" with title "Stop"'

external image link

znilsson
Contributor II

@tlarkin

Casper's working so hard
Makes me say oh my lord
thank you for blessing me
with superior manageability

It feels good to push profiles down
a super dope framework for Appletown
Keeps our Macs in line with scripts and such
'cause Casper is a suite - you can't touch

tlarkin
Honored Contributor

@znilsson

That just made my day, I laughed at that pretty hard.

Thanks for that

-Tom

jebS
New Contributor III

@ jhbush1973

Anyway to add computer name to that script as well?

dgreening
Valued Contributor II

I really love MonkeyBread's IPInMenuBar along with a launchagent...

jebS
New Contributor III

Unfortunately, we cannot pushout freeware to our clients

jhalvorson
Valued Contributor

@mm2270 I've added your script mentioned above (7/2014) and made it available via Self Service.

After updating to Casper Suite 9.82, systems with 10.11.2 will run the script, but then the following error appears after clicking OK on the dialog box: Self Service: Cannot Install Item There was a problem installing Show IP. Contact your administrator.

I haven't dug into it yet. Not sure if it's an issue with Self Service 9.82.

UPDATE: Error appears on devices with 10.10.5. I wonder if it is a change in the error handling introduced with 9.82? Still digging into it...

mm2270
Legendary Contributor III

@jhalvorson When I get a chance I will run the script against a 10.11.2 Mac to see what the issue may be, but at the moment, we aren't on Casper Suite 9.82 yet, so I won't know if its a specific issue with 9.82 or with OS X 10.11.x. It could be the OS and not Casper. This is an older thread, long before El Capitan was released, so none of these scripts were designed with El Cap in mind.