7

I am configuring a router running OpenWRT, but I am struggling to understand how to use the uci command (Unified Configuration Interface) to inspect an existing configuration file. I would like to automate this configuration in a shell script.

Take for example the firewall configuration at /etc/config/firewall which is 195 long, so a bit difficult to inspect with cat.

There are various rules, take the first one with:

root@OpenWrt:/etc/config# grep -B1 -A6 'Allow-DHCP-Renew' /etc/config/firewall
config rule
    option name     Allow-DHCP-Renew
    option src      wan
    option proto        udp
    option dest_port    68
    option target       ACCEPT
    option family       ipv4

root@OpenWrt:/etc/config# 

I can inspect the various fields of this first rule (@rule[0]) e.g. the field name with:

root@OpenWrt:/etc/config# uci get firewall.@rule[0].name
Allow-DHCP-Renew
root@OpenWrt:/etc/config# 

But I can not see the whole rule as output, I've tried with uci get firewall.@rule[0].* but it's not the right syntax.

There is no manual for uci (man uci), so I am not sure where to find the information on how to use that command.

Is this "group get" possible at all?

2 Answers 2

2

If you want to create a script to setup OpenWRT using all current settings on the device.

#!/bin/sh
# Create OpenWRT setup script
script="/tmp/uci_setup_script.sh"
echo "#!/bin/sh" > "$script"
echo "uci -q batch << EOI" >> "$script"
for section in $(uci show | awk -F. '{print $1}' | sort -u); do
uci show "$section" | awk -F. '{print "set "$0}' >> "$script"
echo "commit $section" >> "$script"
done
echo "EOI" >> "$script"
chmod 755 "$script"

If you are using image builder for custom firmware, then create a file for the files/etc/uci-defaults folder in the image builder directory.

#!/bin/sh
# Create OpenWRT uci-defaults file for image builder
# copy generated file to 'files/etc/uci-defaults/' folder in your image builder directory
script="/tmp/99-custom"
echo "#!/bin/sh" > "$script"
echo "uci -q batch << EOI" >> "$script"
for section in $(uci show | awk -F. '{print $1}' | sort -u); do
uci show "$section" | awk -F. '{print "set "$0}' >> "$script"
echo "commit $section" >> "$script"
echo "EOI" >> "$script"
chmod 755 "$script"
done
1

Ehm... Just found out I need to use uci show instead of uci get, see below for an example:

root@OpenWrt:/etc/config# uci show firewall.@rule[0]
firewall.cfg0592bd=rule
firewall.cfg0592bd.name='Allow-DHCP-Renew'
firewall.cfg0592bd.src='wan'
firewall.cfg0592bd.proto='udp'
firewall.cfg0592bd.dest_port='68'
firewall.cfg0592bd.target='ACCEPT'
firewall.cfg0592bd.family='ipv4'
root@OpenWrt:/etc/config#
3
  • How can you list ALL the rules without knowing ahead of time how many are in the array? uci show firewall.@rule[*] doesn't work. Commented Jun 5, 2023 at 5:13
  • You could use a bash script to increase an array index until you get an error and collect the results for each iteration Commented Jun 6, 2023 at 6:52
  • That's what I'm doing, but wow that is pretty inefficient. Commented Jun 7, 2023 at 11:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.