Skip to content

Instantly share code, notes, and snippets.

@ArisBee
Created December 17, 2017 22:45
Show Gist options
  • Save ArisBee/c82197c9333cd680572fa97ff73acc67 to your computer and use it in GitHub Desktop.
Save ArisBee/c82197c9333cd680572fa97ff73acc67 to your computer and use it in GitHub Desktop.
A Google App Script function (javascript) to check if an IP address belongs to a given range.
function isInRange(ip, firstRangeIp, lastRangeIp)
{
var ipArr = ip.split(".");
var firstArr = firstRangeIp.split(".");
var lastArr = lastRangeIp.split(".");
var pos = 0
for(i=0;i<4;i++)
{
if(Number(ipArr[i])<Number(firstArr[i]))
return false;
else if (Number(ipArr[i])==Number(firstArr[i])) pos=i;
else{
if(Number(ipArr[pos+1])>Number(lastArr[pos+1]))
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment