Process ISC DHCP lease file

670阅读 0评论2014-08-13 weilanse
分类:LINUX

#!/bin/bash


echo $1


awk 'BEGIN{
    while( (getline line < "maclist") > 0){
        mac[line]
    }
    RS="}"
    FS="\n"
}
/lease/{
    for(i=1;i<=NF;i++){
        gsub(";","",$i)
        hostname=""
        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 ( $i ~ /starts/){
            m=split($i,starts, " ")
            st_d=starts[3]
            st_t=starts[4]
        }
        if ( $i ~ /ends/){
            m=split($i,ends, " ")
            ed_d=ends[3]
            ed_t=ends[4]
        }
    }
                                                         }
    }
    {
        print st_d " " st_t " - " ed_d " " ed_t " ip: " sprintf("%-12s",ip)" ether: "ether " hostname: "hostname "  "
    }
    if ( ether in mac ){
        print "ip: "ip " hostname: "hostname " ether: "ether " uid: "uid
    }
} ' $1
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                              

上一篇:linux shell 字符串操作(长度,查找,替换)详解
下一篇:Stop SSH terminal's title changed.