用Bash解析dhcpd.lease文件 [英] Parse a dhcpd.lease File with Bash

查看:520
本文介绍了用Bash解析dhcpd.lease文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用巴塞尔解析我的dhcpd.lease文件。典型的条目如下所示:

  lease 192.168.20.4 {
starts 6 2009/06/27 00:40 :00;
结束6 2009/06/27 12:40:00;
硬件以太网00:00:00:00:00:00;
uid 00:00:00:00:00:00;
client-hostnameexamle-workstation1;
}

我得到的所有信息都是MAC,我想要的是IP和客户端主机名。但也许,没有客户端主机名。该条目看起来像这样:

  lease 192.168.20.5 {
starts 6 2009/06/27 00:40: 00;
结束6 2009/06/27 12:40:00;
硬件以太网00:00:00:00:00:00;
}

我的第一个想法是grep的租赁属性,硬件以太网属性和uid属性并将其全部放在一行上。然后解析它。



但是我的问题是,我有一个很多文件中分配了很多条目的大文件。树看起来像这样:

  dhcpd-leases 
- 192.168.20.0
- 192.168。 30.0
- 192.168.40.0
[...]

我得到的是MAC从另一个文件解析到列表中。因此,我从这个列表开始,希望用我的MAC grep属性IP,MAC:

 用于ENTRY的$ MACLIST 

VAR $(catdhcpd-leases / 10.148。$ NETWORK.2 / dhcpd.leases| grep -E$ MAC | lease | client-hostname)
echo $ VAR
完成

但是由于$ VAR中有许多entrys,文件无法解析它的权利。



可以帮助吗?



最好的问候
Peter

解决方案

假设你的maclist文件看起来像这样(例如只有一个条目) $ p> $ cat maclist
00:00:00:00:00:01

和您的租约文件就像这样

  $ cat file 
lease 192.168.20.4 {
开始6 2009/06/27 00:40:00;
结束6 2009/06/27 12:40:00;
硬件以太网00:00:00:00:00:00;
uid 00:00:00:00:00:00;
client-hostnameexamle-workstation1;
}

租赁192.168.20.5 {
开始6 2009/06/27 00:40:00;
结束6 2009/06/27 12:40:00;
硬件以太网00:00:00:00:00:00;
}

租赁192.168.20.6 {
开始6 2009/06/27 00:40:00;
结束6 2009/06/27 12:40:00;
硬件以太网00:00:00:00:00:01;
uid 00:00:00:00:00:01;
client-hostnameexamle-workstation2;
}


租赁192.168.20.7 {
开始6 2009/06/27 00:40:00;
结束6 2009/06/27 12:40:00;
硬件以太网01:00:00:00:00:00;
}

您可以试试这个

($(getline line<maclist)> 0){
mac [line]
} $ {
' b = b $ b RS =}
FS =\\\

}
/ lease / {
for(i = 1; i <= NF; i ++){
gsub(;,,$ i)
if($ i〜/ lease /){
m = split($ i,IP,)
ip如果($ i〜/ hardware /){
m = split($ i,hw,)
ether = hw [3] $,那么= IP [2]
}

}
if($ i〜/ client-hostname /){
m = split($ i,ch,)
hostname = ch [2]
}
if($ i〜/ uid /){
m = split($ i,ui,)
uid = ui [2]
}
}
if(mac in ether){
printip:iphostname:hostnameether:etheruid:uid
}
}'文件

输出

  $ ./shell.sh 
hostname:examle-workstation2ether:00:00:00:00:00:01 uid:00:00:00:00:01


I Try to parse my dhcpd.lease File with Basel. A typical entry looks like this:

lease 192.168.20.4 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
    uid 00:00:00:00:00:00;
    client-hostname "examle-workstation1";
}

All information i get is the MAC and what i want ist the IP and the client-hostname. But maybe, there is no client-hostname. The entry looks like this:

lease 192.168.20.5 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
}

My first idea was to grep the lease attribute, the hardware ethernet attribute and the uid attribute and putt it all on one line. And then parse it.

But my problem is, i have a big file with many entries allocated in many files. The tree looks like this:

dhcpd-leases
-- 192.168.20.0
-- 192.168.30.0
-- 192.168.40.0
[...]

And all what i get ist the MACs parsed from another files in to a list. So i start with this list and want to grep the Attributes ip, mac with my MAC:

for ENTRY in $MACLIST
do
    VAR$(cat "dhcpd-leases/10.148.$NETWORK.2/dhcpd.leases" | grep -E "$MAC|lease|client-hostname")
    echo $VAR
done

But it because of the many entrys in the $VAR and the files im unable to Parse it out right.

Can somenone help?

Best regards Peter

解决方案

assuming your maclist file look like this (just one entry for example)

$ cat maclist
00:00:00:00:00:01

and your lease file like this

$ cat file
lease 192.168.20.4 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
    uid 00:00:00:00:00:00;
    client-hostname "examle-workstation1";
}

lease 192.168.20.5 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:00;
}

lease 192.168.20.6 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 00:00:00:00:00:01;
    uid 00:00:00:00:00:01;
    client-hostname "examle-workstation2";
}


lease 192.168.20.7 {
    starts 6 2009/06/27 00:40:00;
    ends 6 2009/06/27 12:40:00;
    hardware ethernet 01:00:00:00:00:00;
}

you can try this

awk 'BEGIN{
    while( (getline line < "maclist") > 0){
        mac[line]
    }
    RS="}"
    FS="\n"
}
/lease/{
    for(i=1;i<=NF;i++){
        gsub(";","",$i)
        if ($i ~ /lease/) {
            m=split($i, IP," ")
            ip=IP[2]
        }
        if( $i ~ /hardware/ ){
            m=split($i, hw," ")
            ether=hw[3]
        }
        if ( $i ~ /client-hostname/){
            m=split($i,ch, " ")
            hostname=ch[2]
        }
        if ( $i ~ /uid/){
            m=split($i,ui, " ")
            uid=ui[2]
        }
    }
    if ( ether in mac ){
        print "ip: "ip " hostname: "hostname " ether: "ether " uid: "uid
    }
} ' file

output

$ ./shell.sh
hostname: "examle-workstation2" ether: 00:00:00:00:00:01 uid: 00:00:00:00:00:01

这篇关于用Bash解析dhcpd.lease文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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