Need to grep /etc/hosts with a known hostname, and then capture the ip address for the hostname from /etc/hosts
Solution 1
Simple answer
ip=$(grep 'www.example.com' /etc/hosts | awk '{print $1}')
Better answer The simple answer returns all matching IP, even those on comment lines. You probably only want the first non-comment match, in which case just use awk outright:
ip=$(awk '/^[[:space:]]*($|#)/{next} /www.example.com/{print $1; exit}' /etc/hosts)
One other thing If you, at some point, care to resolve www.example.com whether your system is configured to use hosts, dns, etc, then consider the lesser known getent
command:
ip=$(getent hosts 'www.example.com' | awk '{print $1}')
Edit in response to update
$ cat script.sh
#!/bin/bash
host_to_find=${1:?"Please tell me what host you want to find"}
while read ip host; do
echo "IP=[$ip] and host=[$host]"
done < <(awk "/^[[:space:]]*($|#)/{next} /$host_to_find/{print \$1 \" \" \$2}" /etc/hosts)
$ ./script.sh testsrv01
IP=[192.168.80.192] and host=[testsrv01-maint]
IP=[192.168.120.192] and host=[testsrv01-ilo]
IP=[192.168.150.192] and host=[testsrv01-pri]
IP=[192.168.200.192] and host=[testsrv01-sec]
Solution 2
You could use grep
with a Perl regex to output the IP of your target hostname.
grep -oP '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?=.*hostname)' /etc/hosts
Explanation:
-
^
finds the start of a line -
\d{1,3}
finds one through three digits -
\.
finds a dot -
(?=something)
findssomething
but doesn't include it in the match ("zero-width positive look-ahead assertion") -
.
without a preceding backslash finds any character -
*
repeats the preceding expression (in this case "any character") zero or more times
In other words, this will find a series of four one through three-digit numbers separated by dots, and print them (grep -o
) if they are followed by any string and then hostname
), all on this on one line.
justlearning
Updated on July 14, 2022Comments
-
justlearning almost 2 years
Need to grep /etc/hosts with a known hostname, and then capture the ip address for the hostname from /etc/hosts.
I am not a programmer, and don't know how to go about this. I have very limited experience with regex, but think that this might work somehow. I am not using DNS, just managing with /etc/hosts file.
I need to grep the /etc/hosts file with the known hostname, and then capture the IP address for the hosts entry. The host file is standard format:
Please help!
UPDATE:
#
Maintenance Network192.168.80.192 testsrv01-maint 192.168.80.193 testsrv02-maint 192.168.80.194 testsrv03-maint
#
Lights Out Network192.168.120.192 testsrv01-ilo 192.168.120.193 testsrv02-ilo 192.168.120.194 testsrv03-ilo
#
Primary Data Network192.168.150.192 testsrv01-pri 192.168.150.193 testsrv02-pri 192.168.150.194 testsrv03-pri
#
Secondary Data Network192.168.200.192 testsrv01-sec 192.168.200.193 testsrv02-sec 192.168.200.194 testsrv03-sec
I need to be able to capture the ip address and full host name entry for every machine into a variable that I can use. For instance run through the file looking to match " testsrv01* ", and capture all of the ip addresses and name for that search. Then same for " testsrv02* " , and so on.
-
tripleee over 9 years
grep|awk
is almost always useless.awk '/www.example.com/{print $1}' /etc/hosts
-
tripleee over 9 yearsMight want to add
$1 ~ /^#/ { next }
as the first line of the Awk script to skip any comments. -
bishop over 9 years@tripleee: Oh, I know, but OP so deliberately asked to grep. :) Good call on the comments, too.
-
bishop over 9 years+1 for using
-o
and positive look-ahead. I'd recommend-m 1
to limit to the first match. Also, note that this won't work for IPv6 addresses which might be in/etc/hosts
(though the OP didn't state he needed v6).