0

How can i detect the servers domain name and automatically have it inputted so i can just press enter

    echo "
Please enter your domain name : "
read Domain
echo $Domain > /root/domain.info
Hostname=server.$Domain

Thanks, Chris

2
  • 1
    assuming this is a linux server, and not Microsoft Windows, depending on your linux distribution see if you have the environment variable $dnsdomainname or $domainname. If so problem solved. Also look at unix.stackexchange.com/questions/38209/… Commented Dec 19, 2016 at 0:26
  • 1
    can also do a if [ -z "$domainname" ] for example to check if there is a domain name set, if not then prompt user to enter something. stackoverflow.com/questions/11686208/… Commented Dec 19, 2016 at 0:30

2 Answers 2

1
Hostname=$(uname -n) #or $(hostname -f)
Nameserver=$(cat /etc/resolv.conf)

To check if it is set:

if [ -z "$Hostname" ];then  
read -p "Give name " name
Hostname=$name
fi

Also notice that Linux / UNIX comes with the following utilities to display hostname / domain name:

a) hostname – show or set hostname
b) domainname – show or set NIS/YP domain name
c) dnsdomainname – show DNS domain name
d) nisdomainname – show or set NIS/YP domain name
e) ypdomainname – show or set NIS/YP domain name

Sign up to request clarification or add additional context in comments.

1 Comment

While this does indeed get your host name, very frequently that ISN"T the FQDN that "the world" uses to get to the box.
1

It sounds like you want to prefill the edit buffer when you use the read builtin.

Bash v4+ allows you to do that by combining the -e option (which activates readline library support) with -i <editBufferDefault>; e.g.:

editBufferDefault=$(domainname)  # get default value to offer to the user
read -e -p "Please enter your domain name: " -i "$editBufferDefault"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.