线条操纵&排序

时间:2015-03-30 00:50:37

标签: string bash sorting tshark

我可以编写Linux脚本,但可以使用一些建议。我知道这个问题有点模糊,所以如果你能提供任何帮助我会很感激!

以下问题适用于个人成长,因为我正在编写一些有趣/学习的网络工具。没有涉及作业(我是大学的高年级,我的课程都不需要这些东西!)

我正在使用tshark来获取有关数据包捕获的信息。这就是它的样子:

rachel@Ubuntu-1:~/PCAP$ tshark -r LargeTorrent.pcap -q -z io,phs

===================================================================
Protocol Hierarchy Statistics
Filter: 

eth                                      frames:4309 bytes:3984321
  ip                                     frames:4119 bytes:3969006
    icmp                                 frames:1316 bytes:1308988
    udp                                  frames:1408 bytes:1350786
      data                               frames:1368 bytes:1346228
      dns                                frames:16 bytes:1176
      nbns                               frames:14 bytes:1300
      http                               frames:8 bytes:1596
      nbdgm                              frames:2 bytes:486
        smb                              frames:2 bytes:486
          mailslot                       frames:2 bytes:486
            browser                      frames:2 bytes:486
    tcp                                  frames:1395 bytes:1309232
      data                               frames:1300 bytes:1294800
      http                               frames:6 bytes:3763
        data-text-lines                  frames:2 bytes:324
        xml                              frames:2 bytes:3205
          tcp.segments                   frames:1 bytes:787
      nbss                               frames:34 bytes:5863
        smb                              frames:17 bytes:3047
          pipe                           frames:4 bytes:686
            lanman                       frames:4 bytes:686
        smb2                             frames:13 bytes:2444
      bittorrent                         frames:10 bytes:1709
        tcp.segments                     frames:2 bytes:433
          bittorrent                     frames:2 bytes:433
            bittorrent                   frames:1 bytes:258
        bittorrent                       frames:2 bytes:221
          bittorrent                     frames:2 bytes:221
  arp                                    frames:146 bytes:8760
  ipv6                                   frames:44 bytes:6555
    udp                                  frames:40 bytes:6211
      dns                                frames:18 bytes:1711
      dhcpv6                             frames:14 bytes:2114
      http                               frames:6 bytes:1014
      data                               frames:2 bytes:1372
    icmpv6                               frames:4 bytes:344
===================================================================

我希望它看起来像:

rachel@Ubuntu-1:~/PCAP$ tshark -r LargeTorrent.pcap -q -z io,phs

===================================================================
Protocol Hierarchy Statistics
Filter: 

Protocol                   Bytes
=====================================
eth                        984321
  ip                       3969006
    icmp                   1308988
    udp                    1350786
      data                 1346228
      dns                  1176
      nbns                 1300
      http                 1596
      nbdgm                486
        smb                486
          mailslot         486
            browser        486
    tcp                    1309232
      data                 1294800
      http                 3763
        data-text-lines    324
        xml                3205
          tcp.segments     787
      nbss                 5863
        smb                3047
          pipe             686
            lanman         686
        smb2               2444
      bittorrent           1709
        tcp.segments       433
          bittorrent       433
            bittorrent     258
        bittorrent         221
          bittorrent       221
  arp                      8760
  ipv6                     6555
    udp                    6211
      dns                  1711
      dhcpv6               2114
      http                 1014
      data                 1372
    icmpv6                 344
===================================================================



编辑:我将添加原始问题,以便理解所提供的(好)答案。

最初,我只想打印“叶子”的统计数据,因为eth,ip等都是父母,他们的统计数据对我来说并不是必需的。另外,我没有让只有空格来显示层次结构的神奇可怕的文本块,而是想删除父母的所有统计数据,并将它们显示为孩子背后的面包屑。

示例:

eth                                      frames:4309 bytes:3984321
  ip                                     frames:4119 bytes:3969006
    icmp                                 frames:1316 bytes:1308988
    udp                                  frames:1408 bytes:1350786
      data                               frames:1368 bytes:1346228
      dns                                frames:16 bytes:1176

应该成为

eth:ip:icmp - 1308988 bytes
eth:ip:udp:data - 1346228 bytes
eth:ip:udp:dns - 1176 bytes

保留层次结构并避免打印无用的统计信息。

无论如何,Etan批准的答案完美地解决了这个问题!对于那些在我这个级别上不确定如何继续这个答案的人,这将有助于你完成:

  1. 将给定脚本另存为filename.awk文件
  2. 将您要操作的文本块另存为filename.txt文件
  3. 致电awk -f filename.awk filename.txt
  4. (可选)将输出通过管道传输到文件(awk -f filename.awk filename.txt >> output.txt

2 个答案:

答案 0 :(得分:2)

我最初认为您想要的输出可以通过此awk脚本实现。 (我认为这可能会更干净,但这似乎运作得很好。)

function entry() {
    # Don't want to print empty entries.
    if (ind[0]) {
        printf "%s", ind[0]
        for (i = 1; i <= ls; i++) {
            printf ":%s", ind[i]
        }
        split(b, a, /:/)
        printf " - %s %s\n", a[2], a[1]
    }
}

# Found our data marker. Note that and print the current line.
$1 == "Filter:" {d=1; print; next}
# Print lines until we see our data marker.
!d {print; next}
# Print empty lines.
!NF {print; next}
# Save our trailing line for later.
/===/ {suf=$0; next}

{
    # Save our previous indentation level.
    ls = s
    # Find our new indentation level (by where the first field starts).
    s = (match($0, /[^[:space:]]/)-1) / 2

    # If the current line is at or below the last indent level print the last line.
    if (s <= ls) {
        entry()
    }

    # Save the current line's byte count.
    b=$NF
    # Save the current line's field name.
    ind[s] = $1
}

END {
    # Print a final line if we had one.
    entry()
    # Print the suffix line if we have one.
    if (suf) {
        print suf
    }
}

在样本输入上,获取此输出。

===================================================================
Protocol Hierarchy Statistics
Filter:

eth:ip:icmp - 1308988 bytes
eth:ip:udp:data - 1346228 bytes
eth:ip:udp:dns - 1176 bytes
eth:ip:udp:nbns - 1300 bytes
eth:ip:udp:http - 1596 bytes
eth:ip:udp:nbdgm:smb:mailslot:browser - 486 bytes
eth:ip:tcp:data - 1294800 bytes
eth:ip:tcp:http:data-text-lines - 324 bytes
eth:ip:tcp:http:xml:tcp.segments - 787 bytes
eth:ip:tcp:nbss:smb:pipe:lanman - 686 bytes
eth:ip:tcp:nbss:smb2 - 2444 bytes
eth:ip:tcp:bittorrent:tcp.segments:bittorrent:bittorrent - 258 bytes
eth:ip:tcp:bittorrent:bittorrent:bittorrent - 221 bytes
eth:arp - 8760 bytes
eth:ipv6:udp:dns - 1711 bytes
eth:ipv6:udp:dhcpv6 - 2114 bytes
eth:ipv6:udp:http - 1014 bytes
eth:ipv6:udp:data - 1372 bytes
eth:ipv6:icmpv6:data - 344 bytes
===================================================================

尽管使用sed可能更容易处理您编辑的内容以表明您想要的内容。

/Filter:/a \
Protocol                   Bytes \
=====================================
s/frames:[^ ]*//
s/               b/b/
s/bytes:\([^ ]*\)/\1/

以输出结束。

===================================================================
Protocol Hierarchy Statistics
Filter:
Protocol                   Bytes
=====================================

eth                        3984321
  ip                       3969006
    icmp                   1308988
    udp                    1350786
      data                 1346228
      dns                  1176
      nbns                 1300
      http                 1596
      nbdgm                486
        smb                486
          mailslot         486
            browser        486
    tcp                    1309232
      data                 1294800
      http                 3763
        data-text-lines    324
        xml                3205
          tcp.segments     787
      nbss                 5863
        smb                3047
          pipe             686
            lanman         686
        smb2               2444
      bittorrent           1709
        tcp.segments       433
          bittorrent       433
            bittorrent     258
        bittorrent         221
          bittorrent       221
  arp                      8760
  ipv6                     6555
    udp                    6211
      dns                  1711
      dhcpv6               2114
      http                 1014
      data                 1372
    icmpv6                 344
===================================================================

答案 1 :(得分:2)

使用sed的简单脚本也可以。

$ printf "\n==========================================================\n"; printf "Protocol Hierarchy Statistics\nFilter:\n\n";printf "\nProtocol\t\t\t\t Bytes\n================================================\n" && sed -e 's/\(frames[:].*bytes[:]\)\(.*$\)/\2/' dat/tshark.txt | tail -n+4 | head -n-1 && printf "================================================\n"

分解为脚本形式(其中dat/tshark.txt是保存tshark输出的文件名):

printf "\n==========================================================\n"
printf "Protocol Hierarchy Statistics\nFilter:\n\n"
printf "\nProtocol\t\t\t\t Bytes\n================================================\n"
sed -e 's/\(frames[:].*bytes[:]\)\(.*$\)/\2/' dat/tshark.txt | tail -n+4 | head -n-1
printf "================================================\n"

<强>输出

==========================================================
Protocol Hierarchy Statistics
Filter:

Protocol                                 Bytes
================================================

eth                                      3984321
  ip                                     3969006
    icmp                                 1308988
    udp                                  1350786
      data                               1346228
      dns                                1176
      nbns                               1300
      http                               1596
      nbdgm                              486
        smb                              486
          mailslot                       486
            browser                      486
    tcp                                  1309232
      data                               1294800
      http                               3763
        data-text-lines                  324
        xml                              3205
          tcp.segments                   787
      nbss                               5863
        smb                              3047
          pipe                           686
            lanman                       686
        smb2                             2444
      bittorrent                         1709
        tcp.segments                     433
          bittorrent                     433
            bittorrent                   258
        bittorrent                       221
          bittorrent                     221
  arp                                    8760
  ipv6                                   6555
    udp                                  6211
      dns                                1711
      dhcpv6                             2114
      http                               1014
      data                               1372
    icmpv6                               344
================================================

格式

根据您对bytes给定protocol tags变量长度的printf信息的评论,您可以使用printf格式化输出,如您所示。和Ethan一样,我开始研究标签合并的原始问题。我最初的方法是将不同的级别读入不同的关联数组,这些数组可以组合成您最初指定的内容。这样做,我必须使用declare -i ln=0 declare -A l1 l2 l3 l4 ## read each line in file and assing to associative arrays for each level while read -r line; do ln=${#line} # base level on length of line read [ $ln -gt 66 ] && continue; [ $ln -eq 66 ] && { iface="${line%% *}"; l1[${iface}]="${line##* }"; } [ $ln -eq 64 ] && { proto="${iface}:${line%% *}"; l2[${proto}]="${line##* }"; } [ $ln -eq 62 ] && { ptype="${proto}:${line%% *}"; l3[${ptype}]="${line##* }"; } [ $ln -le 60 ] && { data="${ptype}:${line%% *}"; l4[${data}]="${line##* }"; } done < "$1" ## output a summary of the file printf "\n4-level deep summary of file '%s':\n\n" "$1" for i in "${!l1[@]}"; do for j in "${!l2[@]}"; do printf " %-32s %s\n" "$j" "${l2[$j]}" for k in "${!l3[@]}"; do printf " %-32s %s\n" "$k" "${l3[$k]}" for l in "${!l4[@]}"; do [ "${l%:*}" == "$k" ] && printf " %-32s %s\n" "$l" "${l4[$l]}" done done done done 生成排列的输出。这是我使用tshark数据的前4个级别进行的第一次尝试:

eth:ip                              frames:4119 bytes:3969006
eth:ip:udp                          frames:1408 bytes:1350786
eth:ip:udp:data                     frames:1368 bytes:1346228
eth:ip:udp:nbdgm                    frames:2 bytes:486
eth:ip:udp:nbns                     frames:14 bytes:1300

它产生的输出是例如:

printf

您可以查看上面代码中的各种{{1}}语句,看看如何处理对齐方式。如果您还有其他问题,请与我们联系。