bash 需要使用已知的主机名 grep /etc/hosts,然后从 /etc/hosts 中捕获主机名的 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25277426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need to grep /etc/hosts with a known hostname, and then capture the ip address for the hostname from /etc/hosts
提问by justlearning
Need to grep /etc/hosts with a known hostname, and then capture the ip address for the hostname from /etc/hosts.
需要使用已知的主机名 grep /etc/hosts,然后从 /etc/hosts 中捕获主机名的 IP 地址。
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.
我不是程序员,不知道该怎么做。我对正则表达式的经验非常有限,但认为这可能会以某种方式起作用。我没有使用 DNS,只是使用 /etc/hosts 文件进行管理。
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:
我需要使用已知主机名对 /etc/hosts 文件进行 grep,然后捕获主机条目的 IP 地址。主机文件是标准格式:
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.
我需要能够将每台机器的 IP 地址和完整主机名条目捕获到我可以使用的变量中。例如,运行查找匹配“testrv01*”的文件,并捕获该搜索的所有 IP 地址和名称。然后对于“ testsrv02* ”也是如此,依此类推。
回答by bishop
Simple answer
简单的回答
ip=$(grep 'www.example.com' /etc/hosts | awk '{print $1}')
ip=$(grep 'www.example.com' /etc/hosts | awk '{print $1}')
Better answerThe 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,即使是注释行中的 IP。您可能只想要第一个非注释匹配,在这种情况下,只需直接使用 awk:
ip=$(awk '/^[[:space:]]*($|#)/{next} /www.example.com/{print $1; exit}' /etc/hosts)
ip=$(awk '/^[[:space:]]*($|#)/{next} /www.example.com/{print $1; exit}' /etc/hosts)
One other thingIf 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:
另一件事如果您在某个时候关心解决 www.example.com 是否您的系统配置为使用主机、dns 等,那么请考虑鲜为人知的getent
命令:
ip=$(getent hosts 'www.example.com' | awk '{print $1}')
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]
回答by John B
You could use grep
with a Perl regexto output the IP of your target hostname.
你可以使用grep
一个Perl的正则表达式来输出目标主机的IP地址。
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
^
找到一行的开始\d{1,3}
查找一到三位数\.
找到一个点(?=something)
找到something
但不将其包含在匹配中(“零宽度正前瞻断言”).
没有前面的反斜杠查找任何字符*
重复前面的表达式(在本例中为“任何字符”)零次或多次
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.
换句话说,这将找到一系列由点分隔的 1 到 3 位数字,grep -o
如果它们后跟任何字符串,则打印它们 ( ) 然后hostname
),所有这些都在一行上。