Script to show lan ports and vlan config, small issue help needed

Hi,

I have created a script to show on the swtich with opentwrt (zyxel 1900-10HP) the interfaces, state, speed, and vlan config. I have with the last one 1 issue. I get the vlans, only the first vlan, I can get it script for the correct.

I use the command bridge vlan show dev interface.

I should get for interface lan1 this

root@GS1900-10HP:~# bridge vlan show dev lan1
port              vlan-id
lan1              10
                  70
                  90
                  99 PVID Egress Untagged
                  100

Running the script I get this, and missing vlan 10

root@GS1900-10HP:~# ./lan1.sh
LAN-port info

| Port            | Speed      | State           | VLANS                          |
|-----------------|------------|-----------------|--------------------------------|
| lan1            | 100Mb/s    | connected       | 70,90,99(u),100                |
| lan10           | ---        | notconnected    |                                |
| lan2            | 1000Mb/s   | connected       | 70,90,99(u),100                |
| lan3            | 1000Mb/s   | connected       | 70,90,99(u),100                |
| lan4            | 1000Mb/s   | connected       |                                |
| lan5            | 1000Mb/s   | connected       | 70,90,99(u),100                |
| lan6            | ---        | notconnected    | 70,90,99(u),100                |
| lan7            | 1000Mb/s   | connected       |                                |
| lan8            | 1000Mb/s   | connected       | 70,90,99(u),100                |
| lan9            | 1000Mb/s   | notconnected    |                                |

I tried resolving it with chatgpt, but didn't work. Hopefully someone like the script and will help to solve the issue.

#!/bin/sh

print_row() {
  printf "| %-15s | %-10s | %-15s | %-30s |\n" "$1" "$2" "$3" "$4"
}

echo "LAN-port info"
echo ""

interfaces=$(ls /sys/class/net | grep -E '^lan')

printf "| %-15s | %-10s | %-15s | %-30s |\n" "Port" "Speed" "State" "VLANS"
printf "|-----------------|------------|-----------------|--------------------------------|\n"

for interface in $interfaces; do
  speed=$(ethtool $interface | grep "Speed:" | awk '{print $2}')
  if [ "$speed" = "Unknown!" ]; then
    speed="---"
  fi

  status=$(cat /sys/class/net/$interface/operstate)

  vlan_config=$(bridge vlan show dev "$interface" | awk -v intf="$interface" "{ if (\$1 == intf) { flag=1; next } if (flag) { if (\$NF == \"Untagged\") { printf \"%s(u),\", \$1 } else { printf \"%s,\", \$1 } } }" | sed 's/,$//')
  vlan_config=${vlan_config%,} # Verwijder de laatste komma

  if [ "$status" = "up" ]; then
    status="connected"
  else
    status="notconnected"
  fi

  print_row "$interface" "$speed" "$status" "$vlan_config"
done

1 Like

I know it didn't work, so that is why I ask for help here.

Better to just ask for help in the first place than to use ChatGPT.

1 Like

Learning point for me.

Hopefully someone can help me instead of AI

You have read the post of OP?
The only issue is that chatgpt could not help with the awk script....

Hi
sorry, i am not expert in AWK but ... maybe some idea

as you see, only difference between vlan10 ad 70 is that "left field" is not empty, aka, there is "lan1" text
in my head, maybe a dirty solution is to echo output of "bridge vlan show" to /tmp/something file, cut the first line
and replace "lan1" text with space
this way you will get "clean" file for processing
...again, maybe i am wrong

When the interface name is found, due to the next statement, awk stops processing the current record and moves on to the next one.

The problem is that you need the value in the second column (next to the interface name), so just swap the columns by replacing next with \$1 = \$2

root@MikroTik:~# interface="lan3"
root@MikroTik:~# bridge vlan show dev "$interface"
port              vlan-id
lan3              10 PVID Egress Untagged
                  20
                  30
root@MikroTik:~#
root@MikroTik:~#
root@MikroTik:~# vlan_config=$(bridge vlan show dev "$interface" | awk -v intf="$interface" "{ if (\$1 == intf) { flag=1; \$1 = \$2 } if (flag) { if (\$NF == \"Untagged
\") { printf \"%s(u),\", \$1 } else { printf \"%s,\", \$1 } } }" | sed 's/,$//')
root@MikroTik:~# echo $vlan_config
10(u),20,30
root@MikroTik:~#
3 Likes

thx that works. Now I can go te extend the script with Poe enabled port and usage in watt

LAN-port info

| Port            | Speed      | State           | VLANS                          |
|-----------------|------------|-----------------|--------------------------------|
| lan1            | 100Mb/s    | connected       | 10,70,90,99(u),100             |
| lan10           | ---        | notconnected    |                                |
| lan2            | 1000Mb/s   | connected       | 10,70,90,99(u),100             |
| lan3            | 1000Mb/s   | connected       | 10,70,90,99(u),100             |
| lan4            | 1000Mb/s   | connected       | 10(u)                          |
| lan5            | 1000Mb/s   | connected       | 10,70,90,99(u),100             |
| lan6            | ---        | notconnected    | 10,70,90,99(u),100             |
| lan7            | 1000Mb/s   | connected       | 10(u)                          |
| lan8            | 1000Mb/s   | connected       | 10,70,90,99(u),100             |
| lan9            | 1000Mb/s   | notconnected    |                                |

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.