Assuming you are on a Linux system with the ip
command (but not the busybox
variant of it), we may ask it for the addr
information in JSON format using ip -j addr
.
From that result, we may use the JSON processor jq
to extract the interface name and create shell code that we can evaluate:
eval "$(
ip -j addr |
jq -r 'map(
["firewall-cmd", "--zone=trusted", "--change-interface=" + .ifname],
["printf", "added %s to trusted\\n", .ifname] | @sh )[]'
)"
On an Ubuntu virtual machine I have, this would evaluate the following commands:
'firewall-cmd' '--zone=trusted' '--change-interface=lo'
'printf' 'added %s to trusted\n' 'lo'
'firewall-cmd' '--zone=trusted' '--change-interface=enp0s3'
'printf' 'added %s to trusted\n' 'enp0s3'
'firewall-cmd' '--zone=trusted' '--change-interface=enp0s8'
'printf' 'added %s to trusted\n' 'enp0s8'
'firewall-cmd' '--zone=trusted' '--change-interface=docker0'
'printf' 'added %s to trusted\n' 'docker0'
Or, we can make a more traditional shell loop that iterates over only the interface names:
ip -j addr | jq -r 'map(.ifname)[]' |
while IFS= read -r ifn; do
firewall-cmd --zone=trusted --change-interface="$ifn"
printf 'added %s to trusted\n' "$ifn"
done