Need to grep /etc/hosts with a known hostname, and then capture the ip address for the hostname from /etc/hosts

南楼画角 提交于 2019-12-11 03:42:56

问题


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 Network

192.168.80.192  testsrv01-maint
192.168.80.193  testsrv02-maint
192.168.80.194  testsrv03-maint

# Lights Out Network

192.168.120.192  testsrv01-ilo
192.168.120.193  testsrv02-ilo
192.168.120.194  testsrv03-ilo

# Primary Data Network

192.168.150.192  testsrv01-pri
192.168.150.193  testsrv02-pri
192.168.150.194  testsrv03-pri

# Secondary Data Network

192.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.


回答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]



回答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) finds something 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.



来源:https://stackoverflow.com/questions/25277426/need-to-grep-etc-hosts-with-a-known-hostname-and-then-capture-the-ip-address-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!