Re: [munkireport] Extracting mac address(es)

190 views
Skip to first unread message

Arjen van Bochoven

unread,
May 23, 2012, 5:08:20 PM5/23/12
to munki...@googlegroups.com
On May 22, 2012, at 7:11 PM, nbalonso <nbal...@gmail.com> wrote:

> Probably some of you guys are already doing this.

I haven't done it, so I can't give you the code to do it.

> I would like to know how to extract from the report the mac address to display it on the report.php

Do you want to display it on the reports listing or on the detailed report of a machine?

> I'm terrible in php and tried many formulas to add the mac add to // Extract system_profile info the part with no luck
>
> would it be possible to list the mac addresses depending on the model but not list bluetooth etc?

Of course that's possible, but some knowledge of php is required. At the bottom of the script that generates the detailed report (app/views/report.php, there's a line that you can uncomment so you get a sense of what's stored:

change
<?//print_r($report)?>
to
<?print_r($report)?>

But you'll still need some knowledge of php to get at the proper items.

> ex. MBair only en0 , MBpro en0 and en1, Macpro en0, en1 and en2.... or may be easyer to list only active adapters

You could check for 'ip_assigned' == 'yes' to see if an adapter is active

-Arjen

A.E. van Bochoven

unread,
Jul 1, 2012, 5:49:29 PM7/1/12
to munki...@googlegroups.com
Does this work on a machine without ethernet (MB air, retina MBP)?

-Arjen

On Jul 1, 2012, at 10:03 AM, nbalonso <nbal...@gmail.com> wrote:

> Well, now that finally had some time to put munkireport-php into production I got this to work.
>
> Code here:
>
> <b>MAC Address</b>
> <?php
> //Extract MAC Address
> foreach($report['MachineInfo']['SystemProfile'][0]['_items'] AS $part){
> //if there is a Mac Address to show (Fixes non thunderbolt monitors on 10.7)
> if (isset($part['Ethernet']['MAC Address'])){
> if (preg_match('/Ethernet|Wi-Fi|AirPort/', $part['_name'])) {
> echo $part['Ethernet']['MAC Address'];
> echo " (";
> echo $part['_name'];
> echo ")";
> echo "<br />";
> }
> }
> }
> ?>
> @Arjen maybe this can be included on the official release (:
>
> Thanks,
> Noel
>
> El jueves, 24 de mayo de 2012 01:51:32 UTC+3, nbalonso escribió:
> Thanks Arjen, that is an excellent debugging technique
>
> I have been able to drill down using the .plist as guide and now I can show the first Mac Address on the report using:
>
> <?=isset($report['MachineInfo']['SystemProfile'][0]['_items'][1]['Ethernet']['MAC Address']) ? $report['MachineInfo']['SystemProfile'][0]['_items'][1]['Ethernet']['MAC Address'] : '?' ?>
>
> Now I am trying to go with another loop as yours and extract all of the mac addresses plus their _name value so I can effectively show: 'Ethernet', 'Airport', 'Wi-Fi' (on 10.7), 'USB Ethernet' on MBair...
>
> Will post any resulting code
>
> Noel

Brian Mickelson

unread,
May 8, 2013, 4:27:15 PM5/8/13
to munki...@googlegroups.com
I did manage to get this working in a manner similar to above. However, I wanted to add the value into an array so, instead of having it inline, I could call the variable when I needed it. I also wanted to be able to use it in other places throughout the application. I have other modifications that I am polishing up to fit my organizational needs.

Here's the code if you want to adapt it.

// Extract MAC Addresses
foreach($report['MachineInfo']['SystemProfile'][0]['_items'] AS $part){
//if there is a Mac Address to show (Fixes non thunderbolt monitors on 10.7)
if (isset($part['Ethernet']['MAC Address'])){
if (preg_match('/Ethernet|Wi-Fi|AirPort/', $part['_name'])) {
$network_info[$part['_name']] = $part['Ethernet']['MAC Address'];
}
}
}
$network_info = (object) $network_info;

But then, I decided to take it a step further. I wanted the hardware addresses to be available in the client listing as well. While I could walk the array from the plist, that seemed expensive to execute it on ~4000 clients. So how do I go about getting it in the client table? Well, after overcoming my mild case of non-programmer's stupid, as well as several incidences of cranial flatulence, i managed to pass the values into the client table with postflight. I don't think it's necessarily pretty. but it works. Also, since it's a hardware value, I had no regrets including it in the table with the hardware records. Plus, now it can be called with $client. Another thing to keep in mind. This only works on the OS default values for network interface names. If you've changed yours for some reason, this will most likely not work.

Add this to scripts/postflight:
# Get Network Address fields
network=$(networksetup -listallnetworkservices|egrep "(Ethernet|AirPort|Wi-Fi)" |tr -cd [:alnum:][:space:]- |sed 's/^/"/g' |sed 's/$/"/g')
network_array=$(echo "$network"|sed 's/ //g')
for service in ${network_array[@]}; do
service=$(echo "$service" |tr -cd [:alnum:]- |sed 's/[0-9]/ &/g')
if [[ "$service" == "AirPort" || "$service" == "Wi-Fi" ]]; then
WIRELESS=$(networksetup -getmacaddress "$service" |awk '{print $3}')
elif [[ "$service" == "Ethernet" || "$service" == "Ethernet 1" ]]; then
ETHERNET=$(networksetup -getmacaddress "$service" |awk '{print $3}')
elif [[ "$service" == "Ethernet 2" ]]; then
ETHERNET2=$(networksetup -getmacaddress "$service" |awk '{print $3}')
fi
done

also in the report submission area, add these:
-d ethernet="$ETHERNET" \
-d ethernet2="$ETHERNET2" \
-d wireless="$WIRELESS" \

then in the __construct function in app/models/client:
$this->rs['ethernet'] = '';
$this->rs['ethernet2'] = '';
$this->rs['wireless'] = '';

Imagine my surprise that all this works. Not only does it work, but it seems (so far) to take into account myriad differences in hardware configurations. I've tested with everything I have available. iMacs and Mac minis are pretty straight forward. Mac Pros and Xserves report their two ethernet ports, MacBook Airs report AirPort (or Wi-Fi, OS dependent.)

I haven't gotten everything pushed to my github repo just yet. Since I'm new to version control, I'm cleaning up some other modifications before committing. Definitely need to work on my git workflow.

Please, have a look and let me know what you guys think.

- Brian



On Monday, July 2, 2012 1:45:26 AM UTC-4, nbalonso wrote:
it does work on MB air but hadn't had a chance to get one of the new retina MBP. Maybe someone else can confirm this.

As long as the device name contains Ethernet, AirPort or Wi-Fi I don't think it has any problem.
Noel

Brian Mickelson

unread,
May 9, 2013, 10:02:58 PM5/9/13
to munki...@googlegroups.com
Updated my Github repo with these changes.

The network_info object is a gist: https://gist.github.com/brianmickelson/5550123

My production version is on Github, in the working branch: https://github.com/brianmickelson/munkireport-php.git



On Wednesday, May 8, 2013 5:49:11 PM UTC-4, nbalonso wrote:
I will check out the first part of your code on Saturday when I get back to the office.

I totally agree when you say that looping over the thousands of plists to extract the MAC addresses in the client-table-view will over-kill the loading time. I considered something similar back in the day but in our case the asset manager cares more about in S/N, so ended up not implementing.

Personally I'd say that the munkireport-php views need to be optimized. Currently it displays all, then filters out.

Ideally (in my mind) it should display only the last XYZ clients and by clicking on 'next page' to see more would make viable something like the loop you talked about (and possible many other new customizable columns) saving this way CPU cycles on the server itself.

Your solution adds CPU cycles to all the clients. But hey, if it fits your envirnment great. I use a similar approach to filter CUDA capable machines in Munki.

I believe I saw for a couple of seconds Per Olofsson's version of munkireport-php last year during the Macsysadmin, and looked great. Maybe him or Arjen have some input (:

PS: just threw ideas out but I am not a web dev myself either, sorry for that. I am willing to investigate how to do it if someone points me into the right direction

Noel

El miércoles, 8 de mayo de 2013 23:27:15 UTC+3, Brian Mickelson escribió:

A.E. van Bochoven

unread,
May 11, 2013, 3:09:10 PM5/11/13
to munki...@googlegroups.com
On 8 mei 2013, at 23:49, nbalonso <nbal...@gmail.com> wrote:

I will check out the first part of your code on Saturday when I get back to the office.

I totally agree when you say that looping over the thousands of plists to extract the MAC addresses in the client-table-view will over-kill the loading time. I considered something similar back in the day but in our case the asset manager cares more about in S/N, so ended up not implementing.

Personally I'd say that the munkireport-php views need to be optimized. Currently it displays all, then filters out.

mr-php uses datatables to do the filtering, datatables also supports serverside filtering.


Ideally (in my mind) it should display only the last XYZ clients and by clicking on 'next page' to see more would make viable something like the loop you talked about (and possible many other new customizable columns) saving this way CPU cycles on the server itself.

Most of the work is now done in the client, but when using paged results, filtering has to be done on the server. To do make this work, the current database tables are not up for the task.

Your solution adds CPU cycles to all the clients. But hey, if it fits your envirnment great. I use a similar approach to filter CUDA capable machines in Munki.

You could change your serverside script to cache the mac-address extraction result, it's probably not going to change very often. 

I believe I saw for a couple of seconds Per Olofsson's version of munkireport-php last year during the Macsysadmin, and looked great. Maybe him or Arjen have some input (:

PS: just threw ideas out but I am not a web dev myself either, sorry for that. I am willing to investigate how to do it if someone points me into the right direction

Noel

El miércoles, 8 de mayo de 2013 23:27:15 UTC+3, Brian Mickelson escribió:

--
You received this message because you are subscribed to the Google Groups "munkireport" group.
To unsubscribe from this group and stop receiving emails from it, send an email to munkireport...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Marnin Goldberg

unread,
Jun 5, 2013, 8:38:22 PM6/5/13
to munki...@googlegroups.com
How would you extend this code to show the ip address along with the MAC address for each interface?

Marnin



Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages