0

Do you know if there is any pattern/logic that could be used to distinguish between an IP address and an FQDN in python? I have a script that process user input which could be ip or fqdn and i would like to to ip validation checks if ip, and no validation check in case it is fqdn.

addy = "1.2.3.4"
a = addy.split('.')
match = re.search('^(([0-9]|[0-9][0-9]|[0-9][0-9][0-9]))$', a[0])
if match is not None:
   if is_valid_ipv4(addy) == True:
      # code continues

what is case addy is fqdn? I wish to call is_valid_ipv4 if input string is only an IP address. Do I need a pattern for FQDN? How to distinguish between IP and FQDN?

Thanos Infosec
  • 57
  • 1
  • 10

2 Answers2

3

Python knows about IP addresses. Meanwhile, this answer gives a pretty good regexp for validating FQDNs.

import ipaddress
import re

addy = "192.0.2.1"

fqdn_re = re.compile('(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}\.?$)')

try:
    ip_addy = ipaddress.ip_address(addy)
    if ip_addy.version == 4:
        print("IPv4 address")
    elif ip_addy.version == 6:
        print("IPv6 address")
except ValueError:
    if fqdn_re.search(addy):
        print("FQDN address")
    else:
        print("Invalid address")
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • i wish to try if input string (your case addy) is ip address and not fqdn. if fqdn then ignore ip address validation. – Thanos Infosec Oct 02 '19 at 10:49
  • 1
    I don't understand. If it is IP address, then it is not FQDN, so that's a test you don't need to make. If the question is "is it IP address, FQDN _or invalid address_", then you need more work to do; however, I would argue it is not very useful (to me, the difference between "bad address" and "unassigned address" is almost certainly irrelevant). – Amadan Oct 02 '19 at 10:53
  • Thank you the code you share. Yes else in except ValueError is needed because In case you enter an invalid ip (out of range 1.2.3.500) it will not consider it as an invalid ip address. – Thanos Infosec Oct 02 '19 at 12:05
-1

Personally, I'd use regex. In Python you can use the re package.

Write a pattern for each (IP and FQDN) and see which gets a match (re.match()).

Here are some useful links:

S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • I already use regex but seems it is working if we assume that first part (till first dot) of IP is digit and first part (till first dot) of fqdn contains only chars. if we assume that first part (till first dot) of fqdn contains digits the distinguish fails. This approach becomes more complicated if you need to do ipv4 format checks in case user enters IP (example: if char is at last part of the IP the distinguish fails again.) – Thanos Infosec Oct 02 '19 at 10:44
  • @ThanosInfosec - Build two regex patterns which matches the **full** IP or FQDN strings. You need a robust solution. Amadan posts a good recommendation for your IP matching - but you need a pattern for FQDNs. – S3DEV Oct 02 '19 at 10:48
  • @asynts - Not true. If you have properly constructed patterns, you'll be able to match IPV4 and IPV6 patterns. – S3DEV Oct 02 '19 at 10:49
  • Is there any standard pattern for FQDNs? can i have fqdn 1mymail.college.com or mymail1.college.com, or mymail.college.com? – Thanos Infosec Oct 02 '19 at 10:52
  • Yes, all of those are valid. In fact, `9292.nl` is an existing server. – Amadan Oct 02 '19 at 11:14