There can be many situations where you update your /etc/hosts file in your local machine. That may be due to some software development, network routine tests, or any usecase. However, you can verify whether you have added the correct IP address for a specific host name using "ping" as discussed in this short article.
Welcome you to a new, useful, and simple article from WitCentre. In this article we will see how to write a small bash script in Ubuntu to validate your host name against the IP address you have added in /etc/hosts in local machine.
The content will be in the following order.
- Create a new host name - IP address pair in /etc/hosts using vi.
- Write the bash script including ping and logic.
- Execute bash script and verify the host name - IP address pair.
1.Create a new host name - IP address pair in /etc/hosts using vi
1. Press 'Ctrl + Alt + t' to open the terminal.
2. Type 'sudo vi /etc/hosts' in terminal and press Enter. Then enter your root your password and press Enter again.
3. Press Insert button on your keyboard. The add your new entries (can paste too).
e.g. let's add three new entries as below.
_______________________________
10.0.0.2 learn.witcentre.com
172.32.3.122 video.witcentre.com
15.124.12.1 tutor.witcentre.com
_______________________________
4. After adding pairs, press Esc button on the keyboard to abort inserting mode.
5. Then, type ':wq' and press Enter. This will take you back to earlier point on the terminal.
Now you have added three new host name - IP address pairs in /etc/hosts file. Now let's see how to write our bash script!
2. Write the bash script including ping and logic
1. Create a new file named as host-checker.sh.
2. Open that file and copy the content below. There is everything we need to validate our host names against IP addresses.
__________________________________________________________________________________
IP1=$1
IP2=$2
IP3=$3
HOST_IP1=$(ping -c 1 learn.witcentre.com | grep PING | cut -d' ' -f3 | tail -c +2 | head -c -2)
HOST_IP2=$(ping -c 1 video.witcentre.com | grep PING | cut -d' ' -f3 | tail -c +2 | head -c -2)
HOST_IP3=$(ping -c 1 tutor.witcentre.com | grep PING | cut -d' ' -f3 | tail -c +2 | head -c -2)
if [ "$IP1" = "$HOST_IP1" ]; then echo 'Host-1 is valid!'; else echo '**Host-1 is INVALID**'; fi
if [ "$IP2" = "$HOST_IP2" ]; then echo 'Host-2 is valid!'; else echo '**Host-2 is INVALID**'; fi
if [ "$IP3" = "$HOST_IP3" ]; then echo 'Host-3 is valid!'; else echo '**Host-3 is INVALID**'; fi
IP2=$2
IP3=$3
HOST_IP1=$(ping -c 1 learn.witcentre.com | grep PING | cut -d' ' -f3 | tail -c +2 | head -c -2)
HOST_IP2=$(ping -c 1 video.witcentre.com | grep PING | cut -d' ' -f3 | tail -c +2 | head -c -2)
HOST_IP3=$(ping -c 1 tutor.witcentre.com | grep PING | cut -d' ' -f3 | tail -c +2 | head -c -2)
if [ "$IP1" = "$HOST_IP1" ]; then echo 'Host-1 is valid!'; else echo '**Host-1 is INVALID**'; fi
if [ "$IP2" = "$HOST_IP2" ]; then echo 'Host-2 is valid!'; else echo '**Host-2 is INVALID**'; fi
if [ "$IP3" = "$HOST_IP3" ]; then echo 'Host-3 is valid!'; else echo '**Host-3 is INVALID**'; fi
__________________________________________________________________________________
NOTE:
- 'IP1=$1' is that we are assigning the first input parameter we are giving when executing the file, to the IP1 variable. (others are in the same way)
- 'ping -c 1' is used to transmit only one packet and terminate the ping session automatically. Thus, this doesn't require to enter 'Ctrl + c' to terminate the session manually.
- 'grep PING' will retrieve only the first line with the IP address of the pinging host.
- 'cut -d' ' -f3' is used to extract third portion of the line after splitting the line by spaces. this will be the IP within brackets. e.g. for learn.witcentre.com, it will be (10.0.0.2).
- 'tail -c +2 | head -c +2' will remove the brackets wrapped the IP address.
- The 'if' conditions are there to validate the IP retrieved from pinging vs IP address provided via input parameters.
3. Execute bash script and verify the host name - IP address pair
Here, we just have to execute the bash file that we created above. Be careful when provide the IP addresses in the correct order when executing the bash script. Since this is a simple script, you have to be careful on this :) Try to modify the script to avoid this if you are using this for a very important task!
1. Open a terminal and navigate to the directory where your host-checker.sh file exists.
2. Then type the below command to execute the script.
_____________________________________________________
sh host-checker.sh 10.0.0.2 172.32.3.122 15.124.12.1
_____________________________________________________
Enter space separated IP addresses here. Script will take IP addresses as input parameters from left to right accordingly.
If your /etc/hosts entry has been updated with the correct host name - IP address pairs, you will get the output as,
_______________
Host-1 is valid!
Host-2 is valid!
Host-3 is valid!
_______________
Try giving a wrong IP address and check the output too!
That is all for this article. Though, this is a small validation, you can use this when you have many host name - IP address pairs defined in your /etc/hosts entry. Since checking them manually might cause to have errors, this will be definitely helpful for an automated check-up for a vast number of pairs.
Hope this could help you!
Comment below if you have anything to be clarified or your ideas on this. You can reach WitCentre anytime, anywhere by subscribing us or contacting us via the contact form.
Stay safe and touched!
Post A Comment:
0 comments: