-
Notifications
You must be signed in to change notification settings - Fork 0
/
getveth.sh
50 lines (42 loc) · 1004 Bytes
/
getveth.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env sh
# Copyright (c) 2024 nggit
if [ $# -gt 1 ]; then
echo "expected at most 1 argument" 1>&2
exit 1
fi
usage() {
cat << EOF
getveth.sh - https://github.com/nggit/container-tools
Usage:
${0##*/} [CONTAINER]
sh getveth.sh [CONTAINER]
EOF
}
lookup() {
iflink=
if [ "$(id -u)" -eq 0 ]; then
pid=$(docker inspect --format "{{.State.Pid}}" "$1") && \
iflink=$(nsenter -t "$pid" -m cat /sys/class/net/eth0/iflink)
else
iflink=$(docker exec "$1" cat /sys/class/net/eth0/iflink | xargs)
fi
if [ -z "$iflink" ] || \
! veth=$(grep -l "^$iflink\$" /sys/class/net/*/ifindex | cut -d/ -f5); then
exit 1
fi
printf " %d: %s: %s\n" "$iflink" "$veth" "$1"
}
if [ $# -eq 1 ]; then
case $1 in
-h|--help)
usage
;;
*)
lookup "$1"
;;
esac
else
for container in $(docker ps --format "{{.Names}}"); do
lookup "$container"
done
fi