Shell-script - Bash script to add dns in etc hosts, Add DNS if host does not exists
Bash script to add dns in etc hosts
shell script to add DNS or host entry in /etc/hostsThe below scrip will search for the given IP and the DNS or host in /etc/hosts file
# Declare the ip which you want to add ip="127.0.0.1" # Execute grep command to find IP and store it found=$(grep -lr "$ip" /etc/hosts) # Evaluate the IP or DNS is found or not if [[ $found != "/etc/hosts" ]]; then echo "127.0.0.1 localhost.localdomain localhost" >> testecho echo 'DNS or host is added in /etc/hosts' else echo 'ip exists!!' fi
In this script the grep command searches for given ip and lists the file /etc/hosts
The result is stored in the variable found. If the value stored in the variable found is equal to /etc/hosts then the DNS or host value will not be added or appended into the /etc/hosts. Else the DNS or host will be added into the file /etc/hosts
OUTPUT:
[shell-script]# sh add-dns.sh DNS or host is added in /etc/hosts [shell-script]# cat /etc/hosts ::1 localhost6.localdomain6 localhost6 127.0.0.1 localhost.localdomain localhostHere when the script is executed the host or DNS value is added / appended to the file /etc/hosts
The topic on Shell-script - Bash script to add dns in etc hosts is posted by - Math
Hope you have enjoyed, Shell-script - Bash script to add dns in etc hostsThanks for your time