-1

I have domain list I want to test are they main domain or sub-domain or nameserver or mailserver:

  • www.domain.com
  • ns1.domain.com
  • mail.domain.com
  • community.domain.com
  • mx.domain.com
  • mx3.domain.com
  • aspmx.l.google.com
  • forums.domain.com

and I used tldextract python library

    from tld import get_tld
    import tldextract

    class DomainOpt(object):
    """docstring for DomainOpt"""
    _domainname = ''
    _tldextract = None
    _domaintype = ''

        def __init__(self, domainname):
            super(DomainOpt, self).__init__()
            self._domainname = domainname
            self._tldextract = tldextract.extract("http://%s/" % self._domainname)
            if self._tldextract.subdomain == 'mail':
                self._domaintype = 'maildomain'
            elif self._tldextract.subdomain in ['ns1','ns2','ns3','ns4','ns5','ns6',
            'ns7','ns8','ns9','ns10','ns11','ns12','ns13','ns14', 'ns15', 'ns16']:
                self._domaintype = 'dnsdomain'
            elif self._tldextract.subdomain is not None:
                self._domaintype = 'subdomain'
            else:
                self._domaintype = 'domain'

        @property
        def domainname(self):
            return get_tld(str('http://%s/' % self._domainname), fail_silently=True)

        @property
        def domain(self):
            return self._tldextract.domain
        @property
        def subdomain(self):
            return self._tldextract.subdomain
        @property
        def suffix(self):
            return self._tldextract.suffix
        @property
        def domaintype(self):
            return self._domaintype
Tarek Kalaji
  • 2,149
  • 27
  • 30

2 Answers2

1

The tldextract library does the opposite of what you need: given a string "mail.example.com", it will return "com" rather than "mail". To do what you need, you don't need any library; just search the domain name for "." and take the substring preceding it.

yole
  • 92,896
  • 20
  • 260
  • 197
1

Try this:

hostList = [
    'www.domain.com',
    'ns1.domain.com',
    'mail.domain.com',
    'community.domain.com',
    'mx.domain.com',
    'mx3.domain.com',
    'aspmx.l.google.com',
    'forums.domain.com'
    ]

for h in hostList:
    splitHost = h.split('.')
    tld = splitHost.pop()
    dom = splitHost.pop()
    host = '.'.join(splitHost)
    print 'FQHN: : ', h
    print 'Host  : ', host
    print 'Domain: ', dom
    print 'TLD   : ', tld
    print

Also note that this only works for 'simple' TLDs like com, net, org, etc.

JonB
  • 836
  • 1
  • 11
  • 15
  • it is useful thanks, but I want to distinct between 'mx3' or 'mx' is for sub-domain or mail domain and 'ns12' and 'aspmx.l' is it mail domain, sub-domain or name server? is there any library or script to do **distinction**? @JonB – Tarek Kalaji Dec 31 '14 at 12:29
  • 1
    You would be assuming that the FQHN actually reflects the purpose of the host. For example, if I named my mail server `george.example.com` would you be able to make any distinction as to its purpose? And, yes, they are out there. – JonB Dec 31 '14 at 12:35
  • 1
    You could try using [`nmap`](http://nmap.org/book/vscan.html) to find the open ports and guess the purpose of the host but it's not 100% accurate for hosts that provide more than one service. – JonB Dec 31 '14 at 12:49