【发布时间】:2019-11-07 08:25:39
【问题描述】:
我想在评论中捕获以特定模式开头的 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)
【问题讨论】:
-
你真的允许root登录吗?