2

質問の言い方が100%わからないので、どこかで答えられて、見つからなかった場合はお詫び申し上げます。

私が持っているのは、他の多くの出力とともに認証パケットを含むデバッグログです。特定のMacアドレスを含むすべてのパケットを見つけるには、約200万行のログを検索する必要があります。

パケットは次のようになります(わずかに打ち切られています)。

-----------------[ header ]-----------------
Event:     Authd-Response (1900)
Sequence:  -54
Timestamp: 1969-12-31 19:30:00 (0)
---------------[ attributes ]---------------
Auth-Result = Auth-Accept
Service-Profile-SID = 53
Service-Profile-SID = 49
RADIUS-Access-Accept-Attr/WiMAX-Capability = 0x(numbers)
Session-Timeout = 3600
Service-Profile-SID = 4
Service-Profile-SID = 29
Chargeable-User-Identity = "(Numbers)"
User-Password = "(the MAC address I'm looking for)"
--------------------------------------------

ただし、可能な長さが異なる約10種類の可能なタイプがあります。それらはすべてヘッダー行で始まり、すべてダッシュ行で終わります。

私はこれを使用してコードブロック自体を取得するためにawkを使用して成功しました:

awk '/-----------------\[ header \]-----------------/,/--------------------------------------------/' filename.txt

しかし、私はそれを使用して、必要なMACアドレスを含むパケットのみを返すことができることを望んでいました。

私はこれを数日間理解しようとしてきましたが、かなり行き詰まっています。私はbashスクリプトを書いてみることができましたが、以前にawkを使用してこのようなことをしたことがあると誓うことができました...

4

4 に答える 4

2

これはあなたのために働くかもしれません(GNU awk):

awk '$0~mac{printf($0.RT)}' mac="01:23:45:67:89:ab" RS="\n[-]+\n" file

mac選んだ住所はどこですか。

于 2012-09-29T06:13:05.227 に答える
1

一方通行。

infile次のコンテンツ(異なるMACを持つ3つのヘッダー)を想定しています。

-----------------[ header ]-----------------
Event:     Authd-Response (1900)
Sequence:  -54
Timestamp: 1969-12-31 19:30:00 (0)
---------------[ attributes ]---------------
Auth-Result = Auth-Accept
Service-Profile-SID = 53
Service-Profile-SID = 49
RADIUS-Access-Accept-Attr/WiMAX-Capability = 0x(numbers)
Session-Timeout = 3600
Service-Profile-SID = 4
Service-Profile-SID = 29
Chargeable-User-Identity = "(Numbers)"
User-Password = "ab:89:67:45:23:01"
--------------------------------------------
-----------------[ header ]-----------------
Event:     Authd-Response (1900)
Sequence:  -54
Timestamp: 1969-12-31 19:30:00 (0)
---------------[ attributes ]---------------
Auth-Result = Auth-Accept
Service-Profile-SID = 53
Service-Profile-SID = 49
RADIUS-Access-Accept-Attr/WiMAX-Capability = 0x(numbers)
Session-Timeout = 3600
Service-Profile-SID = 4
Service-Profile-SID = 29
Chargeable-User-Identity = "(Numbers)"
User-Password = "01:23:45:67:89:ab"
--------------------------------------------
-----------------[ header ]-----------------
Event:     Authd-Response (1900)
Sequence:  -54
Timestamp: 1969-12-31 19:30:00 (0)
---------------[ attributes ]---------------
Auth-Result = Auth-Accept
Service-Profile-SID = 53
Service-Profile-SID = 49
RADIUS-Access-Accept-Attr/WiMAX-Capability = 0x(numbers)
Session-Timeout = 3600
Service-Profile-SID = 4
Service-Profile-SID = 29
Chargeable-User-Identity = "(Numbers)"
User-Password = "00:00:45:67:89:ab"
--------------------------------------------

awk次のスクリプトを実行します。

awk -v mac="01:23:45:67:89:ab" '
    BEGIN { 
        RS = "-+\\[ header \\]-+"; 
        FS = "\n"; 
    } 
    ## Save record separator. I must do at the beginning because later the
    ## variable is reset. ¿Bug?
    FNR == 1 {
        record_sep = RT;
    }
    { 
        ## Go throught each line searching for the MAC. If found print
        ## the whole block.
        for (i = 1; i <= NF; i++ ) { 
            if ( match( $i, mac ) > 0 ) {
                print record_sep, $0;
                break;
            }
        } 
    }
' infile

その結果、次のようになります。

-----------------[ header ]----------------- 
Event:     Authd-Response (1900)
Sequence:  -54
Timestamp: 1969-12-31 19:30:00 (0)
---------------[ attributes ]---------------
Auth-Result = Auth-Accept
Service-Profile-SID = 53
Service-Profile-SID = 49
RADIUS-Access-Accept-Attr/WiMAX-Capability = 0x(numbers)
Session-Timeout = 3600
Service-Profile-SID = 4
Service-Profile-SID = 29
Chargeable-User-Identity = "(Numbers)"
User-Password = "01:23:45:67:89:ab"
--------------------------------------------
于 2012-09-28T21:43:45.323 に答える
0

一部のawkは、複数文字のレコード区切り文字をサポートしています。'------'行が常に同じ長さである場合、

 awk 'BEGIN{ORS=RS="^---------------------$";}/macAddress/{print}' logfile 

動作するはずです。

(もちろん、実際のrecセパレーターの長さに一致するように「----」を拡張します。

IHTH

于 2012-09-28T21:30:17.150 に答える
0
awk -v mac=MACADDR '
     /^-----------------\[ header \]-----------------$/ { inpacket=1; found=0 }
     inpacket { packet = packet "\n" $0; if (/User-Password = / && $3 == mac) { found=1 } }
     /^--------------------------------------------$/ && found { print packet; inpacket=0 }'

上記の例の引用符と括弧は、実際にはファイル形式の一部ではないと想定しました。そうである場合は、最初の行を次のように変更します。

awk -v mac='"('MACADDR')"' '
于 2012-09-28T21:36:30.207 に答える