I'm writing a perl script and I'm trying to check a variable to see if it is a valid IP address. I've split the octets into an array and am using a foreach loop to check them one by one. I'm trying to use grep to check their validity (0-255), but I can't seem to get it to work.
My first attempt was to simpy use the following:
if(`echo $var | grep [0-255]` eq $var)
But that didn't work. So my next try was to do the following:
if(`echo $var | grep [[0-1]?[0-9]?[0-9], 2[0-4][0-9], 25[0-5]]` eq $var)
I wasn't sure that having classes within classes like that was valid, so when this didn't work, I wasn't surprised. I then tried to spread out the outer class by doing this:
if(`echo $var | grep -e [0-1]?[0-9]?[0-9] -e 2[0-4][0-9] -e 25[0-5]` eq $var)
Yet, that did not work either. After a while it occurred to me that the back-ticked command had to be chopped to get rid of the return character. I've implimented all three of the above commands with the chop(), and still to no avail.
Can anyone help me with this?