下面的命令输出一个以空格分隔的文本表,是否有一个工具可以在此处消除不必要的间距,同时保持列对齐?
$ sudo ss -ltpn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 32 10.218.108.1:53 *:*
users:(("dnsmasq",pid=10242,fd=9))
LISTEN 0 128 *:22 *:*
users:(("sshd",pid=1111,fd=3))
LISTEN 0 32 fd42:9324:ab98:50fb::1:53 :::*
users:(("dnsmasq",pid=10242,fd=13))
LISTEN 0 32 fe80::c024:c5ff:fe68:999e%lxdbr0:53 :::*
users:(("dnsmasq",pid=10242,fd=11))
LISTEN 0 128 :::22 :::*
事实证明,ss
如果您只是通过管道输出或将其重定向到文件,则可以为您完成此操作。例如,在没有管道的系统上,我得到以下信息:
$ sudo ss -ltpn
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:53939 0.0.0.0:* users:(("spotify",pid=4152748,fd=115))
LISTEN 0 10 0.0.0.0:57621 0.0.0.0:* users:(("spotify",pid=4152748,fd=96))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=822,fd=3))
LISTEN 0 128 127.0.0.1:10391 0.0.0.0:* users:(("Enpass",pid=2193055,fd=38))
LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=818,fd=8))
LISTEN 0 5 127.0.0.1:9292 0.0.0.0:* users:(("emacs",pid=178419,fd=13))
LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=314,fd=4),("systemd",pid=1,fd=106))
LISTEN 0 5 127.0.0.1:34512 0.0.0.0:* users:(("purevpnd",pid=839,fd=6))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=822,fd=4))
LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=818,fd=7))
LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=314,fd=6),("systemd",pid=1,fd=128))
cat
但是,如果我只是通过管道传输到,则会得到:
$ sudo ss -ltpn | cat
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:53939 0.0.0.0:* users:(("spotify",pid=4152748,fd=115))
LISTEN 0 10 0.0.0.0:57621 0.0.0.0:* users:(("spotify",pid=4152748,fd=96))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=822,fd=3))
LISTEN 0 128 127.0.0.1:10391 0.0.0.0:* users:(("Enpass",pid=2193055,fd=38))
LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=818,fd=8))
LISTEN 0 5 127.0.0.1:9292 0.0.0.0:* users:(("emacs",pid=178419,fd=13))
LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=314,fd=4),("systemd",pid=1,fd=106))
LISTEN 0 5 127.0.0.1:34512 0.0.0.0:* users:(("purevpnd",pid=839,fd=6))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=822,fd=4))
LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=818,fd=7))
LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=314,fd=6),("systemd",pid=1,fd=128))
如果我只是重定向到文件,也会得到相同的输出:sudo ss -ltpn > file
。
有关更一般的解决方案,可以使用column
。例如,给定此输入文件:
$ cat file
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:53939 0.0.0.0:* users:(("spotify",pid=4152748,fd=115))
LISTEN 0 10 0.0.0.0:57621 0.0.0.0:* users:(("spotify",pid=4152748,fd=96))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=822,fd=3))
LISTEN 0 128 127.0.0.1:10391 0.0.0.0:* users:(("Enpass",pid=2193055,fd=38))
LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=818,fd=8))
LISTEN 0 5 127.0.0.1:9292 0.0.0.0:* users:(("emacs",pid=178419,fd=13))
LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=314,fd=4),("systemd",pid=1,fd=106))
LISTEN 0 5 127.0.0.1:34512 0.0.0.0:* users:(("purevpnd",pid=839,fd=6))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=822,fd=4))
LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=818,fd=7))
LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=314,fd=6),("systemd",pid=1,fd=128))
我可以通过它column -t
来漂亮地打印它:
$ column -t -N"State,Recv-Q,Send-Q,Local Address:Port,Peer Address:Port,Process" <(tail -n +2 file)
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:53939 0.0.0.0:* users:(("spotify",pid=4152748,fd=115))
LISTEN 0 10 0.0.0.0:57621 0.0.0.0:* users:(("spotify",pid=4152748,fd=96))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=822,fd=3))
LISTEN 0 128 127.0.0.1:10391 0.0.0.0:* users:(("Enpass",pid=2193055,fd=38))
LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=818,fd=8))
LISTEN 0 5 127.0.0.1:9292 0.0.0.0:* users:(("emacs",pid=178419,fd=13))
LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=314,fd=4),("systemd",pid=1,fd=106))
LISTEN 0 5 127.0.0.1:34512 0.0.0.0:* users:(("purevpnd",pid=839,fd=6))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=822,fd=4))
LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=818,fd=7))
LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=314,fd=6),("systemd",pid=1,fd=128))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句