shell Luci openWRT UCI防火墙规则更新,无位置编号

ryoqjall  于 2023-02-19  发布在  Shell
关注(0)|答案(1)|浏览(158)

嘿,所有我一直试图找到一些代码,将允许我通过UCI更新我的防火墙规则。
我目前添加新规则的方式如下:

uci add firewall rule
uci set firewall.@rule[21].name='B Macbook Air'
uci set firewall.@rule[21].src='lan'
uci set firewall.@rule[21].family='ipv4'
uci set firewall.@rule[21].src_ip='192.168.1.227'
uci set firewall.@rule[21].src_mac='00:00:00:00:00:00'
uci set firewall.@rule[21].dest='wan'
uci set firewall.@rule[21].proto='all'
uci set firewall.@rule[21].target='REJECT'
uci set firewall.@rule[21].enabled='1'
uci commit firewall

这将在**luci(branch(git-22.347.45520-d30ab74))**中生成正确的规则:

我更新其中一个规则"启用"或"禁用"的方法如下:

uci set firewall.@rule[21].name="B Macbook Air"
uci set firewall.@rule[21].enabled="1"
uci commit firewall

这工作如预期,但我想知道是否有一种方法太只是调用防火墙规则的名称,而不是需要知道的位置(即:[21])?
就像这样:

uci set firewall.@rule.name="B Macbook Air"
uci set firewall.@rule.enabled="1"
uci commit firewall

当然,上面的方法不起作用。给出的误差为
root@turris:~# uci设置防火墙。@www.example.com ="B Macbook Air"rule.name="B Macbook Air"
uci:无效参数
这可能做到吗?

brvekthn

brvekthn1#

您可以编写一个循环来检查每个规则的.name
以下示例假定索引号从0开始,并且是连续的
(Not确定默认安装的是bash。示例代码在sh中。)

#!/bin/sh

name='Support-UDP-Traceroute'
option=enabled
value=1

i=0
while true; do
  rname=$( uci get "firewall.@rule[$i].name" 2> /dev/null )
  if [ $? != 0 ]; then
    # no more rules
    break
  fi

  if [ "$rname" = "$name" ]; then
    echo "found: $name: [$i]"
    # uci set firewall.@rule[$i].$option="$value"
    # uci commit firewall

    break
  fi

  let i++
done

如果你需要经常这样做,就把它变成一个函数。如果索引号不连续,你需要稍微更新它。
另一种解决方案是使用uci show firewall

i=$( uci show firewall | grep/sed/awk/... )
uci set firewall.@rule[$i].the_option="the_value"
uci commit firewall

这样,您就不关心索引号是否连续。

相关问题