WifI Script

pat_best
Contributor III

I am using bits and pieces of tkimpton's script https://jamfnation.jamfsoftware.com/discussion.html?id=5327 and I am wanting to do an ip address comparison. How can I take the existing computer ip address and compare it to our ip range. as an example I have a computer that is at ip address 123.456.789.123 and I want to make sure that it falls in the range 123.456.xxx.xxx? All I want to compare are the first two sets of numbers and disregard the rest. Thanks in advance for any insight to this!

2 ACCEPTED SOLUTIONS

mm2270
Legendary Contributor III

Without really looking at the original script, you're going to want to create a new variable with just the first two sets of numbers. You can use cut in the script for that. Assume the current IP Address is stored in a variable called $IPADD, then:

IPSubnet=$( echo "$IPADD" | cut -d. -f1,2 )

This would give you "123.456" in your case above.

Later in the script simply do a if = then check like so

if [[ "$IPSubnet" == "123.456" ]]; then
   **do something**
else
  * do something else*
fi

And there are probably about 6 other ways if not more this can be done, so this is only one example. Someone may have an even better way to do it.

Hope that helps.

View solution in original post

mm2270
Legendary Contributor III

There is actually one other way to do it without creating a new variable at all. Like so-

if [[ $(echo ""$IPADD"" | grep "^123.456") ]]; then
    *condition passed. do something*
else
    *condition failed. do something else*
fi

Note the caret ^ at the beginning of the regex to look for. That tells grep to look for 123.456 at the start of the line, so even if "123.456" just happened to be the second and third octet of the IP address the condition wouldn't pass. Only if the address begins with those.

View solution in original post

4 REPLIES 4

mm2270
Legendary Contributor III

Without really looking at the original script, you're going to want to create a new variable with just the first two sets of numbers. You can use cut in the script for that. Assume the current IP Address is stored in a variable called $IPADD, then:

IPSubnet=$( echo "$IPADD" | cut -d. -f1,2 )

This would give you "123.456" in your case above.

Later in the script simply do a if = then check like so

if [[ "$IPSubnet" == "123.456" ]]; then
   **do something**
else
  * do something else*
fi

And there are probably about 6 other ways if not more this can be done, so this is only one example. Someone may have an even better way to do it.

Hope that helps.

pat_best
Contributor III

Oh okay, that makes sense, I was looking at it in a different way. That looks like a much better way of doing it. Thanks!

mm2270
Legendary Contributor III

There is actually one other way to do it without creating a new variable at all. Like so-

if [[ $(echo ""$IPADD"" | grep "^123.456") ]]; then
    *condition passed. do something*
else
    *condition failed. do something else*
fi

Note the caret ^ at the beginning of the regex to look for. That tells grep to look for 123.456 at the start of the line, so even if "123.456" just happened to be the second and third octet of the IP address the condition wouldn't pass. Only if the address begins with those.

pat_best
Contributor III

very nice! both of these responses get me going. Thanks!