Bash 是否有选择地从“iptables-nvL”命令输出中删除不必要的列空白? 目标
我们希望更改以下在Ubuntu 18.04上运行的基于iptables8的脚本生成的输出,该脚本解析: …更窄,列之间不必要的空白更少:Bash 是否有选择地从“iptables-nvL”命令输出中删除不必要的列空白? 目标,bash,awk,sed,formatting,iptables,Bash,Awk,Sed,Formatting,Iptables,我们希望更改以下在Ubuntu 18.04上运行的基于iptables8的脚本生成的输出,该脚本解析: …更窄,列之间不必要的空白更少: Chain INPUT (policy DROP 2525 packets, 130K bytes) target prot opt in out source destination ufw-before-logging-input all -- * *
Chain INPUT (policy DROP 2525 packets, 130K bytes)
target prot opt in out source destination
ufw-before-logging-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-before-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-logging-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-reject-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-track-input all -- * * 0.0.0.0/0 0.0.0.0/0
我们更喜欢bash脚本,在这里我们可以比在
下面的脚本会随着时间的推移调整屏幕上每个列间距的宽度
a以每列为基础。我们还不能得到基于awk的东西,比如,或者做我们想做的事情
完成类似/相同任务的非bash解决方案也可以工作
更具挑战性的输入
由于最右边的自由格式列(内容中唯一带有空格的列)的缘故,下面脚本未分析的这一部分更为困难:
Chain ufw-after-input (1 references)
pkts bytes target prot opt in out source destination
1 78 ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:137
0 0 ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:138
0 0 ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:139
5 260 ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:445
0 0 ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:67
0 0 ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
0 0 ufw-skip-to-policy-input all -- * * xxx.xxx.xxx.xxx/yy xxx.xxx.xxx.xxx/yy ADDRTYPE match dst-type BROADCAST
Chain ufw-after-logging-forward (1 references)
pkts bytes target prot opt in out source destination
0 0 LOG all -- * * xxx.xxx.xxx.xxx/yy xxx.xxx.xxx.xxx/yy limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] "
以上各项的良好转换如下所示:
Chain ufw-after-input (1 references)
target prot opt in out source destination
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:137
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:138
ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:139
ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:445
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:67
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
ufw-skip-to-policy-input all -- * * xxx.xxx.xxx.xxx/yy xxx.xxx.xxx.xxx/yy ADDRTYPE match dst-type BROADCAST
Chain ufw-after-logging-forward (1 references)
target prot opt in out source destination
LOG all -- * * xxx.xxx.xxx.xxx/yy xxx.xxx.xxx.xxx/yy limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] "
原始iptables bash脚本
生成上述输出的第一个版本的脚本;这可以说是一种不优雅的黑客行为:
#!/usr/bin/env bash
#
# Pretty-print 'iptables -nvL' output.
# 1. remove the 'pkts' and 'bytes' columns
# 2. make column-aligned, table-based output per
# https://gist.github.com/Airdawg5571/1a8c49ca5dd97af55ab9
# 3. attempt to make narrower, removing unnecessary whitespace
# between columns, to save previous display-screen realestate
#
iptables -nvL | \
# remove the 'pkts' and 'bytes' columns
awk '{ if ($1 != "Chain") { $1=""; $2=""; print } else print}' | \
# make tabular (table) output
column -t | \
sed 's/^Chain/\n&/g' | \
sed '/^Chain/ s/[ \t]\{1,\}/ /g' | \
sed '/^[0-9]/ s/[ \t]\{1,\}/ /10g' | \
# reduce the unnecessary whitespace after 'prot' column
perl -pe 's|^(?!Chain)([^\s]+\s+\w+)\s+|\1 \t|' | \
# arbitrarily truncate line length
cut -c -120
使用:
每列格式:a开关控制格式样式,但不能设置单个列宽。示例-右对齐第一列,对下四列使用默认对齐方式,然后右对齐其余列:
align -e '!/^Chain/' -a r4dr < file.txt
使用:
每列格式:a开关控制格式样式,但不能设置单个列宽。示例-右对齐第一列,对下四列使用默认对齐方式,然后右对齐其余列:
align -e '!/^Chain/' -a r4dr < file.txt
鉴于您更新的问题和评论,并使用cat文件代替我系统上没有的iptables-nvL::
$ cat tst.awk
BEGIN { nf = split("0 0 27 7 6 5 5 20 20",w) }
NF && !/^Chain/ {
for (i=3; i<=nf; i++) {
printf "%-*s", w[i], $i
}
sub("^([[:space:]]*[^[:space:]]+){"nf"}[[:space:]]*","")
}
{ print }
当针对在上提供的示例输入运行时,我得到以下结果,我在中没有看到任何问题,因此如果您说此输出不正确,您必须指出问题所在:
$ awk -f tst.awk file
Chain INPUT (policy DROP 35 packets, 1771 bytes)
target prot opt in out source destination
ufw-before-logging-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-before-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-logging-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-reject-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-track-input all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy DROP 0 packets, 0 bytes)
target prot opt in out source destination
ufw-before-logging-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-before-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-logging-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-reject-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-track-forward all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
target prot opt in out source destination
ufw-before-logging-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-before-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-logging-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-reject-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-track-output all -- * * 0.0.0.0/0 0.0.0.0/0
Chain ufw-after-forward (1 references)
target prot opt in out source destination
Chain ufw-after-input (1 references)
target prot opt in out source destination
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:137
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:138
ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:139
ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:445
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:67
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
ufw-skip-to-policy-input all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type BROADCAST
Chain ufw-after-logging-forward (1 references)
target prot opt in out source destination
LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] "
Chain ufw-after-logging-input (1 references)
target prot opt in out source destination
LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] "
Chain ufw-after-logging-output (1 references)
target prot opt in out source destination
Chain ufw-after-output (1 references)
target prot opt in out source destination
Chain ufw-before-forward (1 references)
target prot opt in out source destination
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
ACCEPT all -- wg0 * 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 3
ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 11
ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 12
ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
ufw-user-forward all -- * * 0.0.0.0/0 0.0.0.0/0
Chain ufw-before-input (1 references)
target prot opt in out source destination
ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
ufw-logging-deny all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
ACCEPT icmp -- * * xxx.xxx.xxx.xxx/yy 0.0.0.0/0 icmptype 8
ACCEPT icmp -- * * xxx.xxx.xxx.xxx/yy 0.0.0.0/0 icmptype 8
ACCEPT icmp -- * * xxx.xxx.xxx.xxx/yy 0.0.0.0/0 icmptype 8
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 3
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 11
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 12
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
DROP udp -- * * 0.0.0.0/0 0.0.0.0/0 udp spt:67 dpt:68
ufw-not-local all -- * * 0.0.0.0/0 0.0.0.0/0
DROP udp -- * * 0.0.0.0/0 224.0.0.251 udp dpt:5353
DROP udp -- * * 0.0.0.0/0 239.255.255.250 udp dpt:1900
ufw-user-input all -- * * 0.0.0.0/0 0.0.0.0/0
Chain ufw-before-logging-forward (1 references)
target prot opt in out source destination
Chain ufw-before-logging-input (1 references)
target prot opt in out source destination
Chain ufw-before-logging-output (1 references)
target prot opt in out source destination
Chain ufw-before-output (1 references)
target prot opt in out source destination
ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
鉴于您更新的问题和评论,并使用cat文件代替我系统上没有的iptables-nvL::
$ cat tst.awk
BEGIN { nf = split("0 0 27 7 6 5 5 20 20",w) }
NF && !/^Chain/ {
for (i=3; i<=nf; i++) {
printf "%-*s", w[i], $i
}
sub("^([[:space:]]*[^[:space:]]+){"nf"}[[:space:]]*","")
}
{ print }
当针对在上提供的示例输入运行时,我得到以下结果,我在中没有看到任何问题,因此如果您说此输出不正确,您必须指出问题所在:
$ awk -f tst.awk file
Chain INPUT (policy DROP 35 packets, 1771 bytes)
target prot opt in out source destination
ufw-before-logging-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-before-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-logging-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-reject-input all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-track-input all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy DROP 0 packets, 0 bytes)
target prot opt in out source destination
ufw-before-logging-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-before-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-logging-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-reject-forward all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-track-forward all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
target prot opt in out source destination
ufw-before-logging-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-before-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-after-logging-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-reject-output all -- * * 0.0.0.0/0 0.0.0.0/0
ufw-track-output all -- * * 0.0.0.0/0 0.0.0.0/0
Chain ufw-after-forward (1 references)
target prot opt in out source destination
Chain ufw-after-input (1 references)
target prot opt in out source destination
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:137
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:138
ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:139
ufw-skip-to-policy-input tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:445
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:67
ufw-skip-to-policy-input udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
ufw-skip-to-policy-input all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type BROADCAST
Chain ufw-after-logging-forward (1 references)
target prot opt in out source destination
LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] "
Chain ufw-after-logging-input (1 references)
target prot opt in out source destination
LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 10 LOG flags 0 level 4 prefix "[UFW BLOCK] "
Chain ufw-after-logging-output (1 references)
target prot opt in out source destination
Chain ufw-after-output (1 references)
target prot opt in out source destination
Chain ufw-before-forward (1 references)
target prot opt in out source destination
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
ACCEPT all -- wg0 * 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 3
ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 11
ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 12
ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
ufw-user-forward all -- * * 0.0.0.0/0 0.0.0.0/0
Chain ufw-before-input (1 references)
target prot opt in out source destination
ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
ufw-logging-deny all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
ACCEPT icmp -- * * xxx.xxx.xxx.xxx/yy 0.0.0.0/0 icmptype 8
ACCEPT icmp -- * * xxx.xxx.xxx.xxx/yy 0.0.0.0/0 icmptype 8
ACCEPT icmp -- * * xxx.xxx.xxx.xxx/yy 0.0.0.0/0 icmptype 8
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 3
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 11
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 12
DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
DROP udp -- * * 0.0.0.0/0 0.0.0.0/0 udp spt:67 dpt:68
ufw-not-local all -- * * 0.0.0.0/0 0.0.0.0/0
DROP udp -- * * 0.0.0.0/0 224.0.0.251 udp dpt:5353
DROP udp -- * * 0.0.0.0/0 239.255.255.250 udp dpt:1900
ufw-user-input all -- * * 0.0.0.0/0 0.0.0.0/0
Chain ufw-before-logging-forward (1 references)
target prot opt in out source destination
Chain ufw-before-logging-input (1 references)
target prot opt in out source destination
Chain ufw-before-logging-output (1 references)
target prot opt in out source destination
Chain ufw-before-output (1 references)
target prot opt in out source destination
ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
这项工作:
#!/usr/bin/env bash
#
# Pretty-print iptables(8) output.
#
# source:
# https://gitlab.com/johnnyutahh/swmisc/-/blob/master/sysadmin/networking/iptables/iptables-list-pretty.sh
#
# (The following script was Ubuntu-18.04 tested on 2020-05-17.)
#
# Derivered from
# https://www.reddit.com/r/bash/comments/gl61yb/selectively_remove_unnecessary_column_whitespace/fqw19tv
# Adjust these values to resize column widths
column_widths=(0 0 27 5 4 8 8 17 17)
iptables_align()
{
while read line; do
if [[ $line =~ Chain ]]; then
echo "$line"
else
line=${line//\*/\\\*}
array=($line)
for n in {2..8}; do
w=${column_widths[$n]}
printf "%-${w}s" "${array[$n]}"
done
lastcol_with_spaces_in_content=("${array[@]:9}")
printf "%s" "${lastcol_with_spaces_in_content[*]}"
echo
fi
done
}
iptables -nvL | iptables_align | sed -s 's|\\\*|* |g' | less
资料来源:
这项工作:
#!/usr/bin/env bash
#
# Pretty-print iptables(8) output.
#
# source:
# https://gitlab.com/johnnyutahh/swmisc/-/blob/master/sysadmin/networking/iptables/iptables-list-pretty.sh
#
# (The following script was Ubuntu-18.04 tested on 2020-05-17.)
#
# Derivered from
# https://www.reddit.com/r/bash/comments/gl61yb/selectively_remove_unnecessary_column_whitespace/fqw19tv
# Adjust these values to resize column widths
column_widths=(0 0 27 5 4 8 8 17 17)
iptables_align()
{
while read line; do
if [[ $line =~ Chain ]]; then
echo "$line"
else
line=${line//\*/\\\*}
array=($line)
for n in {2..8}; do
w=${column_widths[$n]}
printf "%-${w}s" "${array[$n]}"
done
lastcol_with_spaces_in_content=("${array[@]:9}")
printf "%s" "${lastcol_with_spaces_in_content[*]}"
echo
fi
done
}
iptables -nvL | iptables_align | sed -s 's|\\\*|* |g' | less
资料来源:
这不是“删除第1行并使用第t列”问题吗?这是head-n1文件和sed 1d文件| column-t。但是您的脚本中有-t列,所以我不确定问题出在哪里。您的问题是如何提供示例输入,即iptables-nvL的输出在其管道传输到所有其他命令之前,因为我们将除去所有这些命令,除了maybe列和给定输入的预期输出。冗余打印:awk'{if$1!=Chain{$1=;$2=;print}else print}'可以简化为awk'{if$1!=Chain{$1=;$2=}print}@EdMorton-原始,原始iptables-上面添加了nvL输出。这里是链接:请不要发布链接或图片,只需显示一个包含简洁、可测试的示例输入和预期输出的链接,因为该输入在您的问题中是文本。看看这是否还不清楚。我只是浏览了一下您发布的链接中的文本,该链接提示了这一点——在发布您的示例输入/输出时,请确保它是最小的,但仍然代表您真正的问题。因此,如果您的实际数据是20个块,每个块包含10-20行,请创建并发布一个具有代表性的示例,使用2或3个块,每个块包含4或5行。让它对我们来说清晰、简洁、简单。这不是一个删除第1行并使用第t列的问题吗?这是head-n1文件和sed 1d文件| column-t。但是您的脚本中有-t列,所以我不确定问题出在哪里。您的问题是如何提供示例输入,即iptables-nvL的输出在其管道传输到所有其他命令之前,因为我们将除去所有这些命令,除了maybe列和给定输入的预期输出。冗余打印:awk'{if$1!=Chain{$1=;$2=;print}else print}'可以简化为awk'{if$1!=Chain{$1=;$2=}print}@EdMorton-原始,原始iptables-上面添加了nvL输出。这里是链接:请不要发布链接或图片,只需显示一个包含简洁、可测试的示例输入和预期输出的链接,因为该输入在您的问题中是文本。看看这是否还不清楚。我只是浏览了一下您发布的链接中的文本,该链接提示了这一点——在发布您的示例输入/输出时,请确保它是最小的,但仍然代表您真正的问题。因此,如果您的实际数据是20个块,每个块包含10-20行,请创建并发布一个具有代表性的示例,使用2或3个块,每个块包含4或5行。第一个脚本相当于awk'NR==1{print;next}{print | column-t-o\\},第二个awk'NR==1{print;next}{print | column-t-R\1,6,7\}“使用GNU列,因此不需要为其添加私有工具,您可以在AWK脚本中使用AWK’NR=1 {打印OR $ 0)在AWK脚本中的链行之前轻松添加空白行;这是一个奇妙的发现。不确定它是否能处理最后一个最右边的自由格式列。但是我当然可以用
在其他领域也是如此。谢谢@agc@EdMorton,在我的系统Mint v19.3,mawk v1.3.3-17ubuntu3上,前两个awk等效项失败,列为:invalid option。。。错误。@argc我说的是使用GNU列,显然您没有使用GNU列。awk版本是不相关的,它将与任何awk表现相同。我的观点是,如果您可以获得类似的功能,那么安装/使用成千上万人使用的GNU工具对于可移植性、可靠性等来说会更好?一个人的私人工具可能只被少数人使用,可能包含一些人们还没有被绊倒的bug,并且这些bug的可用性/支持可能会消失。@EdMorton,GNU专栏的好消息,我很惊讶地得知Mint使用bsdmainutils来提供列。第一个脚本相当于awk'NR==1{print;next}{print | column-t-o\\},第二个awk'NR==1{print;next}{print | column-t-R\1,6,7\}“使用GNU列,因此不需要为其添加私有工具,您可以在AWK脚本中使用AWK’NR=1 {打印OR $ 0)在AWK脚本中的链行之前轻松添加空白行;这是一个奇妙的发现。不确定它是否能处理最后一个最右边的自由格式列。但我肯定可以在其他领域使用它。谢谢@agc@EdMorton,在我的系统Mint v19.3,mawk v1.3.3-17ubuntu3上,前两个awk等效项失败,列为:invalid option。。。错误。@argc我说的是使用GNU列,显然您没有使用GNU列。awk版本是不相关的,它将与任何awk表现相同。我的观点是,如果您可以获得类似的功能,那么安装/使用成千上万人使用的GNU工具对于可移植性、可靠性等来说会更好?一个人的私人工具可能只被少数人使用,可能包含一些人们还没有被绊倒的bug,并且这些bug的可用性/支持可能会消失。@EdMorton,GNU专栏的好消息,我很惊讶地得知Mint使用bsdmainutils来提供column.Hi-Ed-上面的tst.awk脚本都不能完全工作。我需要它来完成整个过程。非常感谢您的帮助和努力,非常感谢,您的回答和笔记极大地帮助我学习awk-这是我一直在寻找的东西。如果您告诉我其中一个最接近您的需求的方式不完全有效,那么我可以帮助您纠正任何问题。如果您链接的输出中有一些内容在您在问题中发布的示例中不存在,并且脚本无法正确处理缺少的内容,那么请修复问题中的示例。不要使用你发布的shell脚本,看。嗨,Ed,我已经提供了。如果您想了解更多信息,欢迎您针对这些数据运行,并清楚地看到这些问题非常明显。shell循环可能不是最优的,但它解决了我的问题,没有任何安全问题。从这个帖子中退出-再次非常感谢Ed对您的帮助@Johnnyutah我的判断是正确的,我确实运行了我发布的最后一个脚本,该脚本针对您提供的链接中的示例输入,并将输出发布到脚本下我答案的顶部。我看不出有什么问题。如果你看到任何问题,请告诉我在哪里。我只是删除了原始答案中的所有内容,以避免进一步混淆我建议的哪个脚本可以解决你的问题。嗨,Ed-上面的tst.awk脚本都不完全有效。我需要它来完成整个过程。非常感谢您的帮助和努力,非常感谢,您的回答和笔记极大地帮助我学习awk-这是我一直在寻找的东西。如果您告诉我其中一个最接近您的需求的方式不完全有效,那么我可以帮助您纠正任何问题。如果您链接的输出中有一些内容在您在问题中发布的示例中不存在,并且脚本无法正确处理缺少的内容,那么请修复问题中的示例。不要使用你发布的shell脚本,看。嗨,Ed,我已经提供了。如果您想了解更多信息,欢迎您针对这些数据运行,并清楚地看到这些问题非常明显。shell循环可能不是最优的,但它解决了我的问题,没有任何安全问题。从这个帖子中退出-再次非常感谢Ed对您的帮助@Johnnyutah我的判断是正确的,我确实运行了我发布的最后一个脚本,该脚本针对您提供的链接中的示例输入,并将输出发布到脚本下我答案的顶部。我看不出有什么问题。如果你看到任何问题,请告诉我在哪里。我只是删除了原始答案中的所有内容,以避免进一步混淆我建议的哪个脚本可以解决你的问题。我知道你是哈
ppy,因此这更适用于有类似问题的其他人。答案如下:您应该通过shellcheck运行该功能,例如,作为查找和解决某些问题的起点。即使您修复了这些问题,并且发现/修复了shellcheck无法识别的任何不太明显的其他问题,它仍然会非常缓慢、不可移植并且很长。有关更多信息,请参阅。我知道您对此很满意,因此这更适用于其他遇到类似问题的人。答案如下:您应该通过shellcheck运行该功能,例如,作为查找和解决某些问题的起点。即使您修复了这些问题,并且发现/修复了shellcheck无法识别的任何不太明显的其他问题,它仍然会非常缓慢、不可移植并且很长。有关更多信息,请参阅。
#!/usr/bin/env bash
#
# Pretty-print iptables(8) output.
#
# source:
# https://gitlab.com/johnnyutahh/swmisc/-/blob/master/sysadmin/networking/iptables/iptables-list-pretty.sh
#
# (The following script was Ubuntu-18.04 tested on 2020-05-17.)
#
# Derivered from
# https://www.reddit.com/r/bash/comments/gl61yb/selectively_remove_unnecessary_column_whitespace/fqw19tv
# Adjust these values to resize column widths
column_widths=(0 0 27 5 4 8 8 17 17)
iptables_align()
{
while read line; do
if [[ $line =~ Chain ]]; then
echo "$line"
else
line=${line//\*/\\\*}
array=($line)
for n in {2..8}; do
w=${column_widths[$n]}
printf "%-${w}s" "${array[$n]}"
done
lastcol_with_spaces_in_content=("${array[@]:9}")
printf "%s" "${lastcol_with_spaces_in_content[*]}"
echo
fi
done
}
iptables -nvL | iptables_align | sed -s 's|\\\*|* |g' | less