bash - How to get hostname from IP address from file similar to /etc/hosts -


i have file maps ip address hostname. format similar hosts file , contains list of ipaddress hostname mapping.

eg.

10.200.99.1    master1 10.200.99.2    master2 10.200.99.3    master3 10.200.99.4    slave1 10.200.99.5    slave2 10.200.99.6    slave3 ... ... ... 

i obtain hostname given ipaddress using bash script.

how can so?

you can try :

sh script.sh listofip

#!/bin/bash          echo  "ip ?"         echo -n "(value , press enter) :"         read ip   while read line #variables file1=$line mip=$(echo $file1 | awk '{print $1}') name=$(echo $file1 | awk '{print $2}')       if [ "$mip" = "$ip" ]                 echo "machine name " $name     fi  done < $1 

results :

ip ? (value , press enter) :10.200.99.2 machine name  master2 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -