Extension attribute with Network Segment help

AJPinto
Contributor III

I am trying to make an extension attribute to report which network segment a device is in for reporting to a "search inventory" report. Since you cannot really report on JAMFs network segments in settings in any meaningful way I am trying an extension attribute. Thankfully I only have 3 segments of interest for this specific situation.

We have a number of Mac Mini’s in our datacenter for contractors to remote access. I am trying to provide something easy for support to look at in an inventory report that tells them which computer belongs to which segment when they go to assign a device. I’m wanting it to tell them which vendors subnet the device is in (ie result1) as our support could not remember IP ranges if their live depended on it.

The following is what I was able to come up with however all devices come back with the "Does not fall in to 3 results" result regardless of their IP address. I have not tried to make an EA this complex before and have not really found anything useful so I am sure I missed something simple. Any help or suggestions would be much appreciated.

I have changed information to protect the identity of the individuals involved and to keep my legal team happy.

#!/bin/sh


IPAddress=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}')

if [[$IPAddress =~ 10.1.[147].*]]; then
    echo "<result>Result1</result>"
elif [[$IPAddress =~ 10.1.[246].*]]; then
    echo "<result>result2</result>"
elif [[$IPAddress =~ 10.1.[345].*]]; then
    echo "<result>result3</result>"
else
    echo "<result>Does not fall in to 3 results<result>"
fi
2 ACCEPTED SOLUTIONS

sdagley
Honored Contributor II

@AJPinto You need a space after the [[ and before the ]], but that should generate a syntax error, not return the if all else fails result. The other issue is that if you have a VPN connection active you're going to have 2 inet responses in addition to the 127.0.0.1 which your detection logic won't handle.

View solution in original post

AJPinto
Contributor III

@sdagley I think that got it, thank you very much. I also had to remove the brackets from the 3rd octet in the IP address. I added a grep to exclude our VPN IP as you suggested though this will only be used for reporting on devices in our datacenter, though EA's cannot be limited like that :/.

Incase anyone else needs it here is this final product.

#!/bin/sh

IPAddress=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | grep -Fv 10.127 | awk '{print $2}')

if [[ $IPAddress =~ 111.112.123.* ]]; then
    echo "<result>Result1</result>"
elif [[ $IPAddress =~ 111.112.124.* ]]; then
    echo "<result>Result2</result>"
elif [[ $IPAddress =~ 111.112.125.* ]]; then
    echo "<result>Result3</result>"
else
    echo "<result>Other catch all result</result>"
fi

View solution in original post

2 REPLIES 2

sdagley
Honored Contributor II

@AJPinto You need a space after the [[ and before the ]], but that should generate a syntax error, not return the if all else fails result. The other issue is that if you have a VPN connection active you're going to have 2 inet responses in addition to the 127.0.0.1 which your detection logic won't handle.

View solution in original post

AJPinto
Contributor III

@sdagley I think that got it, thank you very much. I also had to remove the brackets from the 3rd octet in the IP address. I added a grep to exclude our VPN IP as you suggested though this will only be used for reporting on devices in our datacenter, though EA's cannot be limited like that :/.

Incase anyone else needs it here is this final product.

#!/bin/sh

IPAddress=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | grep -Fv 10.127 | awk '{print $2}')

if [[ $IPAddress =~ 111.112.123.* ]]; then
    echo "<result>Result1</result>"
elif [[ $IPAddress =~ 111.112.124.* ]]; then
    echo "<result>Result2</result>"
elif [[ $IPAddress =~ 111.112.125.* ]]; then
    echo "<result>Result3</result>"
else
    echo "<result>Other catch all result</result>"
fi

View solution in original post