Skip to main content
Solved

WifI Script


Forum|alt.badge.img+12

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!

Best answer by mm2270

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 original
Did this topic help you find an answer to your question?

4 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7881 replies
  • Answer
  • April 4, 2014

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.


Forum|alt.badge.img+12
  • Author
  • Contributor
  • 150 replies
  • April 4, 2014

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
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7881 replies
  • April 4, 2014

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.


Forum|alt.badge.img+12
  • Author
  • Contributor
  • 150 replies
  • April 4, 2014

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings