Regular Expressions poorly match Internet Addresses
Internet addresses (IP) lead to highly complex regular expressions (regex) that attempt to match only valid addresses. Regex deal poorly with number ranges, and must account for optional portions of the IP address. For example, \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} matches the invalid 999.999.999.999 string, and fails to match the valid IP address of 127.1:
$ ping -c 1 127.1 PING 127.1 (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=0.062 ms --- 127.1 ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/std-dev = 0.062/0.062/0.062/0.000 ms
Instead, use a well tested and community supported regex from Regexp::Common or similar module. Another option: perform a loose match, then feed the results through the inet_aton and inet_ntoa functions:
$ perl -MSocket -le 'print inet_ntoa inet_aton shift' \ 127.1 127.0.0.1 $ perl -MSocket -le 'print inet_ntoa inet_aton shift' \ 999.999.999.999 Bad arg length for Socket::inet_ntoa, length is 0, should be 4 at -e line 1.
These functions also provide quick hostname resolution:
$ perl -MSocket -le 'print inet_ntoa inet_aton shift' \ sial.org 69.90.43.86
Matches may also fail if IPv4 mapped addresses appear, for example when performing OpenSSH security checks.