如何解析netstat命令从行中获取send-q号

时间:2013-09-11 11:55:35

标签: linux bash netstat

我有来自netstat -naputeo的输出:

    tcp        0      0 :::44500                    :::*                        LISTEN      2000       773788772  18117/java          off (0.00/0/0)
    tcp        0      0 :::22                       :::*                        LISTEN      0          9419       4186/sshd           off (0.00/0/0)
    tcp        0      0 ::ffff:127.0.0.1:61666      ::ffff:127.0.0.1:43940      ESTABLISHED 2000       788032760  18122/java          off (0.00/0/0)
    tcp        0      0 ::ffff:192.168.1.202:56510  ::ffff:192.168.1.202:3000   ESTABLISHED 0          791652028  6804/java_ndsagent  keepalive (7185.05/0/0)
    tcp        0      0 ::ffff:192.168.1.202:56509  ::ffff:192.168.1.202:3000   TIME_WAIT   0          0          -                   timewait (41.13/0/0)
    tcp        0      0 ::ffff:192.168.1.202:56508  ::ffff:192.168.1.202:3000   TIME_WAIT   0          0          -                   timewait (21.13/0/0)
    tcp        0   4656 ::ffff:192.168.1.202:22     ::ffff:84.208.36.125:48507  ESTABLISHED 0          791474860  24141/1             on (0.19/0/0)
    tcp        0      0 ::ffff:127.0.0.1:61616      ::ffff:127.0.0.1:45121      ESTABLISHED 2000       788032761  18117/java          off (0.00/0/0)
    tcp        0      0 ::ffff:192.168.1.202:3000   ::ffff:192.168.1.202:56510  ESTABLISHED 0          791651217  8044/rmiregistry    off (0.00/0/0)

Send-Q是第3个字段,此处的攻击者是端口22和4656KB。 问题是我需要将该特定行和该数字/端口/进程输出到输出文件[仅当它高于4000时,才会发送到我的收件箱并提醒我。

我见过类似的答案,但我无法使用这些建议提取线条。我不知道什么过程会填补Q但我知道端口。它不仅仅是22,它可以在任何给定时间更多。

我试过了:

netstat -naputeo | awk '$3 == 0 && $4 ~ /[^0-9]22$/'

但这给了我错误的界限。 [即::: 22]

netstat -naputeo | awk '{if(($3)>0) print $3;}'

这是错的,因为它以某种方式产生了该领域的所有行。

我需要的是将数字和行发送到csv,这就是全部。我可以稍后处理错误检查,也许可以对其进行改进。

有什么建议吗?

使用它并且它现在有效,但仍有改进的空间

filterQs() {
    while read recv send address pid_program; do
        ip=${address%%:*}
        port=${address##*:}
        pid=${pid_program%%/*}
        program=${pid_program#*/}
        echo "recv=${recv} send=${send} ip=${ip} port=${port} pid=${pid} program=${program}"


        if [[ ${port} -eq 35487||  ${port} -eq 65485||  ${port} -eq CalorisPort || ${port} -eq 22 ]]
                then
                        echo "recv=${recv} send=${send} ip=${ip} port=${port} pid=${pid} program=${program}" >> Qmonitor.txt

        fi


done < <(netstat -napute 2>/dev/null | awk '$1 ~ /^(tcp|udp)/ && ($2 > 500 || $3 > 500) { print $2, $3, $4, $9 }')

}

全部谢谢

4 个答案:

答案 0 :(得分:0)

这样的东西
$ netstat -naputeo 2>/dev/null | awk -v OFS=';' '$1 ~ /^tcp/ && $3 > 4000 { sub(/^.+:/, "", $4); print $3, $4, $9 }'

那将输出第3列(Send-Q),第4列(Local Address)的端口部分和第9列(PID/Program name)如果Send-Q > 4000分开用分号表示你可以将它管道输入你的CSV。

E.g。 (我的盒子上的Send-Q > 0

$ netstat -naputeo 2>/dev/null | awk -v OFS=';' '$1 ~ /^tcp/ && $3 > 0 { sub(/^.+:/, "", $4); print $3, $4, $9 }'
52;22;4363/sshd:

修改

如果你真的需要进一步处理bash中的值,那么你可以通过awk打印相应的列并迭代这样的行:

#!/bin/bash

while read recv send address pid_program; do
        ip=${address%%:*}
        port=${address##*:}
        pid=${pid_program%%/*}
        program=${pid_program#*/}
        echo "recv=${recv} send=${send} ip=${ip} port=${port} pid=${pid} program=${program}"
        # do stuff here
done < <(netstat -naputeo 2>/dev/null | awk '$1 ~ /^(tcp|udp)/ && ($2 > 4000 || $3 > 4000) { print $2, $3, $4, $9 }')

E.g:

$ ./t.sh
recv=0 send=52 ip=x.x.x.x port=22 pid=12345 program=sshd:

注意:我不明白为什么你需要-o切换到netstat,因为你似乎对定时器输出不感兴趣,所以你可能会放弃它。

答案 1 :(得分:0)

试试这个:

netstat -naputeo | awk '{ if (($3 + 0) >= 4000) { sub(/.*:/, "", $4); print $3, $4, $9;} }'

这会过滤掉标题行,并从字段$ 4中提取端口号。

答案 2 :(得分:0)

Pure bash解决方案:

#!/bin/bash

filterHuge() {
    while read -r -a line; do
        if (( line[2] > 4000 )) && [[ ${line[3]##*:} == '22' ]]; then # if Send-Q is higher than 4000 and port number is 22
            echo "Size: ${line[2]} Whole line: ${line[@]}"
        fi
    done
}

netstat -naputeo | filterHuge

答案 3 :(得分:-1)

我有一个lineage2服务器,并且sent-q

有些问题

我使用你的脚本......:

Size: 84509 Whole line: tcp 0 84509 144.217.255.80:6254 179.7.212.0:35176 ESTABLISHED 0 480806 2286/java on (46.42/11/0)
Size: 12130 Whole line: tcp 0 12130 144.217.255.80:6254 200.120.203.238:52295 ESTABLISHED 0 410043 2286/java on (0.69/0/0)
Size: 13774 Whole line: tcp 0 13774 144.217.255.80:6254 190.30.75.253:63749 ESTABLISHED 0 469361 2286/java on (0.76/0/0)
Size: 12319 Whole line: tcp 0 12319 144.217.255.80:6254 200.120.203.238:52389 ESTABLISHED 0 487569 2286/java on (0.37/0/0)
Size: 9800 Whole line: tcp 0 9800 144.217.255.80:6254 186.141.200.7:63572 ESTABLISHED 0 478974 2286/java on (0.38/0/0)
Size: 12150 Whole line: tcp 0 12150 144.217.255.80:6254 200.120.203.238:52298 ESTABLISHED 0 410128 2286/java on (0.26/0/0)
Size: 9626 Whole line: tcp 0 9626 144.217.255.80:6254 186.141.200.7:63569 ESTABLISHED 0 482721 2286/java on (0.44/0/0)
Size: 11443 Whole line: tcp 0 11443 144.217.255.80:6254 200.120.203.238:52291 ESTABLISHED 0 411061 2286/java on (0.89/0/0)
Size: 79254 Whole line: tcp 0 79254 144.217.255.80:6254 179.7.212.0:6014 ESTABLISHED 0 501998 2286/java on (89.42/10/0)
Size: 10722 Whole line: tcp 0 10722 144.217.255.80:6254 179.7.111.208:12925 ESTABLISHED 0 488352 2286/java on (0.23/0/0)
Size: 126708 Whole line: tcp 0 126708 144.217.255.80:6254 190.11.106.181:3481 ESTABLISHED 0 487867 2286/java on (85.32/7/0)

问题出在一个端口:62​​54

我可以将大于4000的连接发送到重启为0或丢弃它们