Skip to content

Instantly share code, notes, and snippets.

@vimagick
Created July 18, 2024 05:27
Show Gist options
  • Save vimagick/893e7baa5ccc166ebbec2ca926773e7a to your computer and use it in GitHub Desktop.
Save vimagick/893e7baa5ccc166ebbec2ca926773e7a to your computer and use it in GitHub Desktop.
#!/bin/bash
CONTAINER_NAME=${1:?container name is empty}
NETWORK_MODE=$(docker inspect -f "{{.HostConfig.NetworkMode}}" $CONTAINER_NAME)
STATE_PID=$(docker inspect --format '{{.State.Pid}}' $CONTAINER_NAME)
VETH_NAME=
if [[ $NETWORK_MODE = "host" ]]; then
VETH_NAME="host"
else
IF_INDEX=$(nsenter -t $STATE_PID -n ip addr | awk -F: '/eth0@/{print $1}')
if [ -z "$IF_INDEX" ]; then
VETH_NAME="not found"
else
VETH_NAME=$(ip addr | awk -F '[: @]' -vIF_NAME="if$IF_INDEX" '$4==IF_NAME{print $3}')
fi
fi
echo "$VETH_NAME"
@vimagick
Copy link
Author

IF_INDEX=$(docker exec $CONTAINER_NAME cat /sys/class/net/eth0/iflink)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment