我想捕获一些以注释中的特定模式开头的iptables规则,然后将其删除。这是我想要实现的。这是我的bash脚本
ssh -o "StrictHostKeyChecking no" root@$ip_address << EOF
echo "Now Removing your IPTables";
#storing output in input variable
input=$(iptables -nL INPUT --line-number | grep ip.* | cut -d " " -f1 | xargs)
#converting variable into an array
arr1=($input);
#loop through each element of array
echo "length:${#arr1[@]}";
for (( i="${#arr1[@]}"-1;i >=0; i-- ));
do
echo "$i:${arr1[$i]}"
iptables -D INPUT $i;
done;
EOF
问题是远程计算机上没有执行iptables命令,并且输出显示arr1的长度为0。但是我确定iptables的规则符合我的期望模式。
在终端中显示错误:
-bash: line 9: 3: command not found
在命令末尾添加2>&1
也不起作用:
input=$(iptables -nL INPUT --line-number | grep ip.* | cut -d " " -f1 | xargs 2>&1)