Simplest way to verify if a given string is a valid FQDN name?
Solution 1
Host seems to work:
jalderman@mba:/tmp$ cat test.sh
#!/bin/bash
for h in "bert" "ernie" "www.google.com"
do
host $h 2>&1 > /dev/null
if [ $? -eq 0 ]
then
echo "$h is a FQDN"
else
echo "$h is not a FQDN"
fi
done
jalderman@mba:/tmp$ ./test.sh
bert is not a FQDN
ernie is not a FQDN
www.google.com is a FQDN
Solution 2
Unfortunately, I can not comment, but this is in fact a comment to the accepted answer: The solution does not check if the provided argument is really a FQDN. Simple host names that can actually be resolved will also qualify (simplest example: "localhost"). So perhaps the title of the question should be changed accordingly, if the question is not for a fqdn but simply for a resolving name. To actually add to the solution: If a fqdn is really required, you could simply check for a dot in the given name, like this:
is_fqdn() {
hostname=$1
[[ $hostname == *"."* ]] || return 1
host $hostname > /dev/null 2>&1 || return 1
}
This will still allow for "localhost.", but thats technically a fqdn, anyways.
Related videos on Youtube
CR0W3
Updated on September 18, 2022Comments
-
CR0W3 over 1 year
I need to supply either
'-name'
or'-sname'
when starting an Erlang VM depending on whether the given string is a fully qualified name (FQDN) or not. What would be the quickest and environment-independent way of validating that? I am more interested in using some bash command (like'getent'
or'nslookup'
or others) rather than a regex expression. Best if it works in Ubuntu, FreeBSD and Solaris without changes and can be used in bash's'if'
easily.-
Paul almost 10 yearsPretty much any string can be a valid fqdn provided it has a dot in it or at the end, so a lookup is the only way to be certain.
-
CR0W3 almost 10 yearsYou mean nslookup? That's what I mean. I want to make a lookup to be certain. I am just not sure if 'nslookup' is the way to go or maybe 'host' instead or something else?
-
Frank Thomas almost 10 yearsit seems like it might be a better idea to target a python or perl method instead of shelling a utility. shells change all the time, but a system with python3 will always run the same method correctly, regardless of what platform its installed on, and how that platform is configured.
-
CR0W3 almost 10 yearsI just need to use it in an existing bash script when starting an Erlang node. Adding dependency on Python or Perl would be an overkill in my opinion.
-
-
CR0W3 almost 10 yearsLooks like host could be replaced with nslookup and the script would work pretty much the same. Thanks.
-
CR0W3 about 8 yearsGood point. This is definitely an improvement over the previous answer. This answer could be the accepted answer if only the previous answer didn't help me to resolve my problem earlier :)
-
johnnyB almost 8 yearsI like this answer also but should't it check for more than one "."?
[[ $hostname == *"."*"."* ]]
-
Nicolai Ehemann almost 8 yearsNo. Ensuring a single dot is present makes sure no plain hostname is provided (except for the already mentioned "localhost.", which is technically the fqdn for localhost, as localhost does not belong to any domain (except for the root label ".")). Any string containing at least one dot is potentially a valid fqdn. The usage of the "host" command makes sure the string resolves to an ip address, which means it is actually a fqdn.
-
CR0W3 almost 7 yearsDoesn't seem to work on FreeBSD. Can you add an example output so that I know what to expect?
-
MaXi32 almost 3 yearsI think this answer might not correct after few years coming back.
localhost.
is not FQDN butlocalhost.somestring
is FQDN.