# Get FQDN Based On IP



## jjrabbit (Oct 12, 2019)

I'm trying to get the FQDN of the VPS I am using based on it's private IP. This is for the same VPS that I am logged into.

I'm able to find it by typing `host 10.10.10.20` and it returns `20.10.10.10.in-addr.arpa domain name pointer vps.fqdn.name.` but I'm wondering if there's an easier way to get just the `vps.fqdn.name` part?

Reason is I need this name to use as part of my script and I'm not sure how to get only the relevant portion. Is there maybe a different command that returns only the name?


----------



## DutchDaemon (Oct 12, 2019)

I don't think there's a "+short" option for host as there is for dig (not for drill, strangely). If all else fails, you can just do `host 10.10.10.20 | awk '{print $NF}'`


----------



## jjrabbit (Oct 12, 2019)

Thanks DutchDaemon! Unfortunately that command still includes the pesky period at the end ( it prints `vps.fqdn.name.` instead of just `vps.fqdn.name` )

It's okay, maybe I just won't automate this part of the deployment


----------



## D-FENS (Oct 12, 2019)

jjrabbit said:


> Thanks DutchDaemon! Unfortunately that command still includes the pesky period at the end ( it prints `vps.fqdn.name.` instead of just `vps.fqdn.name` )
> 
> It's okay, maybe I just won't automate this part of the deployment


The pesky period at the end is a legitimate part of the FQDN. Sometimes it is omitted but every DNS capable program should be able to understand the trailing dot. It stands for the root zone: "."


----------



## jjrabbit (Oct 13, 2019)

roccobaroccoSC said:


> The pesky period at the end is a legitimate part of the FQDN. Sometimes it is omitted but every DNS capable program should be able to understand the trailing dot. It stands for the root zone: "."



Thanks rocco, but this is not being piped into a DNS capable program. If my use case allowed for it obviously I would have left it in there.


----------



## Donald Baud (Oct 13, 2019)

You can also remove the ending period (.) with the following:
`host 10.10.10.20 | awk '{print $NF}' | sed -r 's=\.$=='`


----------



## gpw928 (Oct 14, 2019)

I can't resist polishing that a bit to get the exit status right:
`host 10.10.10.20 | grep "domain name pointer" | awk '{print $NF}' | sed -r 's=\.$=='`


----------



## leebrown66 (Oct 14, 2019)

simplify, expanding DutchDaemon's answer:
`host 10.1.10.175 | awk '{print substr($NF,1,length($NF)-1)}'`


----------

