Home > Mobile >  How to get the shortName from an FQDN
How to get the shortName from an FQDN

Time:01-19

I'm struggling with getting the shortName from an FQDN. The main problem with the FQDN is that it can come in various forms, such as:

FQDN => ShortName 
node.a.domain.net => node.a
node.a.dev.domain.net => node.a.dev
node.a.sbx.domain.net => node.a.sbx
node.a.b.domain.net => node.a.b
node.a.dev.dom-as-me.domain.net => node.a.dev
node.a.dom-as-me.domain.net => node.a

I'm using Java or Scala to accomplish this, any ideas?

Thanks in advance!

CodePudding user response:

There is no generic way to do that.

You want to split the FQDN at some dot position into the "short name" and the "domain". From just looking at the string, there's no way, it needs additional knowledge.

FQDN => ShortName 
node.a.domain.net => node.a
node.a.dev.domain.net => node.a.dev (Why not node.a in dev.domain.net?)
node.a.sbx.domain.net => node.a.sbx
node.a.b.domain.net => node.a.b
node.a.dev.dom-as-me.domain.net => node.a.dev (Why not node.a.dev.dom-as-me in domain.net?)
node.a.dom-as-me.domain.net => node.a

You need a dictionary of domains, then you can identify string tails that are listed in the dictionary and

  • use the tail as the domain
  • and the head as the short name.

CodePudding user response:

Found that InetAddress do the work:

InetAddress.getByName(host).getCanonicalHostName == host
  • Related