[英]Need to grep /etc/hosts with a known hostname, and then capture the ip address for the hostname from /etc/hosts
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. 我需要使用已知的主机名grep / etc / hosts文件,然后捕获hosts条目的IP地址。 The host file is standard format: 主机文件是标准格式:
Please help! 请帮忙!
#
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. 我需要能够将每台机器的ip地址和完整主机名条目捕获到我可以使用的变量中。 For instance run through the file looking to match " testsrv01* ", and capture all of the ip addresses and name for that search. 例如,运行文件寻找匹配“testsrv01 *”,并捕获该搜索的所有IP地址和名称。 Then same for " testsrv02* " , and so on. 然后同样为“testsrv02 *”,依此类推。
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. 更好的答案简单的答案返回所有匹配的IP,甚至是评论行上的IP。 You probably only want the first non-comment match, in which case just use awk outright: 您可能只想要第一个非评论匹配,在这种情况下只需使用awk:
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: 另一件事如果你在某个时候关心解决www.example.com你的系统是否配置为使用主机,dns等,那么考虑一下鲜为人知的getent
命令:
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]
You could use grep
with a Perl regex to 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 \\d{1,3}
找到一到三位数字 \\.
finds a dot 找到一个点 (?=something)
finds something
but doesn't include it in the match (" zero-width positive look-ahead assertion ") (?=something)
找到了something
但是在匹配中没有包含它(“ 零宽度正向前瞻断言 ”) .
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. 换句话说,这将找到一系列由点分隔的四位一位到三位数字,如果它们后跟任何字符串然后是hostname
,则打印它们( grep -o
),所有这些都在一行上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.