Hi,
I want a shell command through which I can parse an FQDN (Fully Qualified Domain Name) and check whether it is correct or not?
The FQDN can accept alphanumeric, . and - only.
I tried grep -E "^[:alnum:]|\-|." <file name>, but I am not able to get the correct result.
Please provide any suggestions as to how can this be done as fast as possible?
This checks for a well-formed domain name:
echo "www.rugters.edu" | egrep -q '^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$'
if [[ $? -eq 0 ]] ; then
echo "ok"
else
echo "not ok"
fi
Note it works for a limited number of things: e.g.: .com .edu You can add some if needed.
well-formed ip address :
echo '127.0.0.1' | grep -q '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$'
if [[ $? -eq 0 ]] ; then
echo "ok"
else
echo "not ok"
fi
Note that well-formed is NOT the same as valid. To see if it is a valid domain/ip use nslookup or ping. No regex can check beyond well-formed.