Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 显示按进程分组的打开端口_Bash_Shell - Fatal编程技术网

Bash 显示按进程分组的打开端口

Bash 显示按进程分组的打开端口,bash,shell,Bash,Shell,给定netstat输出,如何显示按进程分组的选定打开端口 到目前为止,我得到的是: :~# netstat -tnlp | awk '/25|80|443|465|636|993/ {proc=split($7,pr,"/"); port=split($4,po,":"); print pr[2], po[port]}' haproxy 636 haproxy 993 haproxy 993 haproxy 465 haproxy 465 exim4 25 apache2 80 exim4

给定
netstat
输出,如何显示按进程分组的选定打开端口

到目前为止,我得到的是:

:~# netstat -tnlp | awk '/25|80|443|465|636|993/ {proc=split($7,pr,"/"); port=split($4,po,":"); print pr[2], po[port]}' 

haproxy 636
haproxy 993
haproxy 993
haproxy 465
haproxy 465
exim4 25
apache2 80
exim4 25
apache2 443
所需输出(在一行中):

请注意:

  • 我有重复的行,因为它们在不同的IP上侦听,但我只需要一个(
    sort-u
    可以)
  • 如果可能的话,我想先按进程排序,然后按端口排序
主要目标是在ssh登录时使用
motd
向用户显示这一行(我已经介绍了这一部分)

试试这个,以后再加

sort|uniq|awk '{a[$1]=a[$1](" "$2" "$3)}END{for (i in a) printf "%s (%s),",i,a[i]}'

+感谢你的努力。回答得很好,但是端口没有排序,行中的最后一个字符是逗号。另外,是否有可能将管道通过2个awk的管道减少到仅仅1个?
netstat-netstat-tnlp(awk)awk“/25 | 80 124;443 | 443 1246 1246 1246 | 466 1246 1246 | 636 1246 1246 1246 1246 1246 1246 1243 1243 1243 1243 1243/{993/{{{{{{{{{proc从从从从从管道通过2到管道通过2到管道到管道到管道到管道到管道到管道到管道到管道到管道到管道到管道到管道到管道到管道到2到管道到管道到管道到管道到管道到管道到管道到2到管道到管道到管道到管道到管道到管道到管道到仅仅仅仅仅仅仅仅1是一个一我无法通过1 awk,我只能在这里帮忙
netstat -tnlp|awk '/25|80|443|465|636|993/ {proc=split($7,pr,"/"); port=split($4,po,":"); print pr[2], po[port]}'|sort|uniq|awk '{a[$1]=a[$1](" "$2" "$3)}END{for (i in a) printf "%s (%s),",i,a[i]}'
sort|uniq|awk '{a[$1]=a[$1](" "$2" "$3)}END{for (i in a) printf "%s (%s),",i,a[i]}'