Bug in searching the results of "ip link"
The line: https://github.com/samos123/docker-veth/blob/068bd73535ee565793dc0a91b8f814b9cb251c16/docker-veth.sh#L44
finds the line starting with the correct $ifindex. But my veth interfaces are all connected to ifndex=2 (pretty normal), and I have, currently, up to ifindex=99.
So, the grep is matching not only index 2, but indices 20-28! I end up getting all the correct output, plus 8 lines showing interfaces described elsewhere but without container ID and name (I'm not sure why 28 isn't showing twice, but it is the next correct line):
> sudo bash Downloads/docker-veth.sh
veth3cbe7a1@if2 e186c3e0bfd2 c******-web
veth2373ae8@if2 6f382ab9fc46 q**********
veth4118ed3@if2 dd94d5e5cfe6 r*****r
veth2373ae8@if2 dd68aded8406 vpn
vetha7d7d59@if2 868d8283a2bb c*****e
veth02f663f@if2 2cf919a203a1 b****r
veth23b5eda@if2 038f7bb2bf65 p******r
veth9c64055@if2 3b2098fce507 j******n
veth16035eb@if2 087a8e74167f r****r
vethf044d12@if2 28a1cd1b4391 s****r
vethb85b7d1@if2 55e3d848ce4e open-*****
veth3288060@if2 19839291a283 homepage
vethcb2dcb1@if2 72d2a9bf8eb7 a******a
veth02f663f@if2
veth4118ed3@if2
veth9c64055@if2
veth16035eb@if2
vethf044d12@if2
vethb85b7d1@if2
veth3cbe7a1@if2
veth23b5eda@if2
vethb9898f7@if3 d6835495ff7f pihole
vethd7c4071@if2 5edece7f54b5 k*******p
veth98c088e@if2 fafe709a837d portainer
vethb76e431@if2 fce51687b48f traefik
vethe848f38@if2 ba8e5c3848e6 meili
veth85407d8@if2 3d030b1f302f chrome
host 39254d0007de dns-helper
veth19078cf@if2 1aa4eb370a66 cvmc-*****
vetha7591a4@if2 4ad8296797db p******s
I believe that result for pihole is also wrong:
An alternative method of finding the interface from the container name gives a different result:
$ docker inspect pihole --format '{{.State.Pid}}'
543008
$ sudo ip netns list | grep ns-543008
ns-543008 (id: 19)
$ ip link show | grep -B1 "link-netnsid 19"
48: veth4b6e92d@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-5e1cd0eada6c state UP mode DEFAULT group default
link/ether 56:b9:e9:83:9f:12 brd ff:ff:ff:ff:ff:ff link-netnsid 19
[It seems my last statement above should have been "…it is the next apparently correct line"!]
pihole is attached to two networks and, and for precisely the reason that most distros no longer use eth0, eth1, etc, you're finding the wrong interface. eth0 is connected to the host's ethernet interface via a macvlan. eth1 is connected to my reverse proxy. Honestly, I don't know why either is wrong, but the above method only finds the one on eth1