在花括号中解析块 [英] Parse blocks in curly brackets

查看:140
本文介绍了在花括号中解析块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析一个如下所示的dhcpd.conf文件:

  authoritative; 

子网x.x.x.x网络掩码x.x.x.x {
范围x.x.x.x x.x.x.x;
拒绝未知客户;
default-lease-time 86400;
max-lease-time 86400;
选项域名bla;
选项domain-name-servers x.x.x.x;
选项broadcast-address x.x.x.x;
选项子网掩码x.x.x.x;
选项路由器x.x.x.x;

host host1 {
硬件以太网00:e1:4c:68:00:53;
fixed-address 1.1.1.1;
}

host host2 {
硬件以太网01:e2:4d:69:01:54;
fixed-address 2.2.2.2;
}

host host3 {
硬件以太网02:e3:4e:70:02:55;
固定地址3.3.3.3;
}

host host4 {
硬件以太网03:e4:4f:71:03:56;
fixed-address 4.4.4.4;
}

host host5 {
hardware ethernet 04:e5:5f:72:04:57;
固定地址5.5.5.5;




$ b $ p
$ b

最后,我需要循环访问主机块(不管他们的名字),并将MAC地址和IP地址分配给变量以处理组合。到目前为止,我只设置了一个变量:

 在`cat /etc/dhcp/dhcpd.conf | grep硬件以太网| awk'{print $ 3}'| tr; 
do
echo发现MAC地址:$ MAC发现IP:<我需要IP变量在这里......>
完成

也许最好以某种方式grep主机块以循环这些,但我不知道如何做到这一点。

有人可以告诉我如何做到这一点吗?

谢谢

解决方案

格式(MAC后跟IP),跟着 sed 一个班轮将给出MAC,IPcsv对列表。你可以解析它并做你想做的事。

  sed -n'/ \s * hardware ethernet / {s / \ *硬件以太网\(。* \); / \ 1 /; N; s / \([a-z0-9:] * \)\s * fixed-address \( 。* \); / \ 1,\ 2 / p}'/etc/dhcp/dhcpd.conf 

输出:

  00:e1:4c:68:00:53,1.1.1.1 
01:e2:4d:69:01:54,2.2.2.2
02:e3:4e:70:02:55,3.3.3.3
03:e4:4f:71:03:56, 4.4.4.4
04:e5:5f:72:04:57,5.5.5.5

按照你的例子生成确切的输出,

  sed -n'/ \s * hardware ethernet / {s / \ *硬件以太网\(。* \); / \ 1 /; N; s / \([a-z0-9:] * \)\s * fixed-address \( * \); /找到MAC地址:\ 1,找到IP:\ 2 / p}'/etc/dhcp/dhcpd.conf 

输出:

 找到MAC地址:00:e1:4c:68: 00:53,Found IP:1.1.1.1 
找到MAC地址:01:e2:4d:69:01 :54,Found IP:2.2.2.2
找到MAC地址:02:e3:4e:70:02:55,找到IP:3.3.3.3
找到MAC地址:03:e4:4f:71 :03:56,Found IP:4.4.4.4
找到MAC地址:04:e5:5f:72:04:57,找到IP:5.5.5.5
pre>




编辑

您可以从每对中提取MAC和IP,并按如下方式对它们进行操作。

  for v in $(sed -n' / \s * hardware ethernet / {s / \s * hardware ethernet \(。* \); / \ 1 /; N; s / \([a-z0-9:] * \ )\s * fixed-address \(。* \); / \ 1,\ 2 / p}'/etc/dhcp/dhcpd.conf);做
mac =$ {v%,*}
ip =$ {v#*,}
echoMAC:$ mac
echoIP: $ ip
完成


I'm trying to parse a dhcpd.conf file that looks like this:

authoritative;

subnet x.x.x.x netmask x.x.x.x {
    range x.x.x.x x.x.x.x;
    deny unknown-clients;
    default-lease-time 86400;
    max-lease-time 86400;
    option domain-name "bla";
    option domain-name-servers x.x.x.x;
    option broadcast-address x.x.x.x;
    option subnet-mask x.x.x.x;
    option routers x.x.x.x;

    host host1 {
            hardware ethernet 00:e1:4c:68:00:53;
            fixed-address 1.1.1.1;
    }

    host host2 {
            hardware ethernet 01:e2:4d:69:01:54;
            fixed-address 2.2.2.2;
    }

    host host3 {
            hardware ethernet 02:e3:4e:70:02:55;
            fixed-address 3.3.3.3;
    }

    host host4 {
            hardware ethernet 03:e4:4f:71:03:56;
            fixed-address 4.4.4.4;
    }

    host host5 {
            hardware ethernet 04:e5:5f:72:04:57;
            fixed-address 5.5.5.5;
    }
}

In the end I need to loop though the host blocks (no matter their name) and assign the MAC address and IP address to variables in order to process the combination. So far I managed to do this with only one variable:

for MAC in `cat /etc/dhcp/dhcpd.conf | grep "hardware ethernet" | awk '{ print $3 }' | tr ";" " "`
do
    echo "Found MAC address: " $MAC "Found IP: <I need the IP Variable here...>" 
done

Maybe it's better to somehow "grep" the host blocks in order to loop through these, but I don't know how to do this.

Could anybody give me a hint on how to do this?

Thanks

解决方案

Given that the input file is in the exact format (MAC followed by IP), following sed one liner will give a list of "MAC,IP" csv pairs. You can parse it and do what ever you want.

sed -n '/\s*hardware ethernet/{s/\s*hardware ethernet \(.*\);/\1/;N;s/\([a-z0-9:]*\)\s*fixed-address \(.*\);/\1,\2/p}' /etc/dhcp/dhcpd.conf

Output:

00:e1:4c:68:00:53,1.1.1.1
01:e2:4d:69:01:54,2.2.2.2
02:e3:4e:70:02:55,3.3.3.3
03:e4:4f:71:03:56,4.4.4.4
04:e5:5f:72:04:57,5.5.5.5

To produce the exact output as in your example,

sed -n '/\s*hardware ethernet/{s/\s*hardware ethernet \(.*\);/\1/;N;s/\([a-z0-9:]*\)\s*fixed-address \(.*\);/Found MAC address: \1, Found IP: \2/p}' /etc/dhcp/dhcpd.conf

Output:

Found MAC address: 00:e1:4c:68:00:53, Found IP: 1.1.1.1
Found MAC address: 01:e2:4d:69:01:54, Found IP: 2.2.2.2
Found MAC address: 02:e3:4e:70:02:55, Found IP: 3.3.3.3
Found MAC address: 03:e4:4f:71:03:56, Found IP: 4.4.4.4
Found MAC address: 04:e5:5f:72:04:57, Found IP: 5.5.5.5


EDIT

You can extract MAC and IP from each pair and do something with them as follows.

for v in $(sed -n '/\s*hardware ethernet/{s/\s*hardware ethernet \(.*\);/\1/;N;s/\([a-z0-9:]*\)\s*fixed-address \(.*\);/\1,\2/p}' /etc/dhcp/dhcpd.conf); do
    mac="${v%,*}"
    ip="${v#*,}"
    echo "MAC: $mac"
    echo "IP: $ip"
done

这篇关于在花括号中解析块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆