in reply to Re: TNSNAMES.ORA and Recdescent
in thread TNSNAMES.ORA and Recdescent

That's a cool regex, but IP addresses can be just a little more complicated than that. There's a bad old standard you might need to take into account:

Just like in IPv6, you can omit octets in the middle if they are zero, for example:

127.1 means 127.0.0.1
10.40.30 means 10.40.0.30

And it gets worse. 192.168.288 is a perfectly legal IP address, most people would write it as 192.168.1.32. Or 10.258 will get you to 10.0.1.2. The rule seems to be that the last decimal number in the address is first spread out into octets, then the remaining octets are dropped in from left to right, starting leftmost. This is the source of the classic trick of getting around IP and name based web filters by entering the 'one huge decimal number' version of the IP address in a browser (I don't think it works on modern browsers that try to be smart about DNS lookups.)

Deranged? Yes. But they're out there, and they work on all NT-based OSs and every *nix I've been able to try it on.

If the underlying system (Oracle, I presume) only supports sane IP addresses, you're cool. I don't know if there would be a way to write a regex for these odd cases. Monks?

--
Spring: Forces, Coiled Again!

Replies are listed 'Best First'.
Re: Re: Re: TNSNAMES.ORA and Recdescent
by iburrell (Chaplain) on Feb 20, 2004 at 06:13 UTC
    The question is what standard allows this. Some Unix implementation allows the extra formats in inet_aton. This is visible with Perl and web browsers. Most people would consider the short-form addresses to be errors and only four decimal components to be valid addresses. It makes sense to reject or ignore long form addresses to keep from confusing people or other software.

    It is possible to restrict the range of numbers with a more detail regular expression:

    /^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-9])$/
•Re: Re: Re: TNSNAMES.ORA and Recdescent
by merlyn (Sage) on Feb 23, 2004 at 18:43 UTC
    Also keep in mind that PRD rules are more than just regex. Nothing wrong with something like this:
    IP: /([\d.]+)/ { use Socket; inet_aton($1) and $1 }
    This takes any digit/dot string and calls inet_aton on it, and if it passes, accepts the value. Otherwise, rejects the value. PRD rocks.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Re: Re: TNSNAMES.ORA and Recdescent
by jmr4096 (Acolyte) on Feb 23, 2004 at 19:39 UTC

    Thanks for the responses on the REGEX for IP addresses. I had two motivations for writing this script. Using it for work was/is my primary concern BUT I would like it to be as robust as possible so that others could use it. Fortunately at work, we use the standard IP4 with no omitted octets. BUT I am interested in trying to get it to work with IP6 and the shorthand form of the IP address.

    I plan to immediately implement the regex that was supplied earlier BUT the problem I saw was with my hostname regular expression.

    Couldn't you have a hostname like 123.empire.com? Is it possible to have 123.224.empire.com? I am guessing that what I will need to do is make sure that last octet contains [a-zA-z].