I have a script which gets the IP address from a web site (Eg.
http://checkip.dyndns.org among others) and strips everything (Eg. "Your IP is:") leaving just the IP - that part works fine. I have a problem with error checking that a valid IP address was returned.
What I've been trying to do is run a sed command that matches the following:
A 0, 1, or 2, followed by 0-9 appearing either 0, 1, or 2 times, followed by a '.'. Then repeating that 3 times to match a 4 number IP address. Perhaps not perfect logic as that would validate 0.0.0.0 - but I can be fairly sure that if the getting the IP from a web site fails then it's not going to leave me with anything approximating an IP which might cause my error check to give a false positive.
Here's an example of my (
not working) sed command:
Code:
echo "255.1.22.33" | sed "s/^[0-2]{1,1}[0-9]{0,2}\.[0-2]{1,1}[0-9]{0,2}\.[0-2]{1,1}[0-9]{0,2}\.[0-2]{1,1}[0-9]{0,2}$//g"
My test is to run the sed command on what should be the IP with the sed replace section empty (as above), therefore if sed provides an empty string I know that the IP was valid.
The problem is that the code above just returns the whole IP given in the echo. I've been pulling my hair out trying to fix it but can't work it out at all.
Any help appreciated. Thanks.