-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdocker-veth.sh
executable file
·54 lines (42 loc) · 1.13 KB
/
docker-veth.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
51
52
53
54
#!/bin/bash
usage () {
cat <<HELP_USAGE
$0 Show the veth interface associated of containers
Examples:
$0 # shows all containers
$0 --filter "name=nginx" # filter containers
See docker ps --help docs for more info on filters
HELP_USAGE
}
case "$1" in
-h|--help)
usage
exit 0
;;
*)
;;
esac
veth=""
containers=$(docker ps --format '{{.ID}} {{.Names}}' "$@")
get_veth () {
# This function expects docker container ID as the first argument
veth=""
networkmode=$(docker inspect -f "{{.HostConfig.NetworkMode}}" $1)
if [ "$networkmode" == "host" ]; then
veth="host"
else
pid=$(docker inspect --format '{{.State.Pid}}' "$1")
ifindex=$(nsenter -t $pid -n ip link | sed -n -e 's/.*eth0@if\([0-9]*\):.*/\1/p')
if [ -z "$ifindex" ]; then
veth="not found"
else
veth=$(ip -o link | grep ^$ifindex | sed -n -e 's/.*\(veth[[:alnum:]]*@if[[:digit:]]*\).*/\1/p')
fi
fi
}
while IFS= read -r line
do
containerid=$(echo $line | awk '{ print $1 }')
get_veth $containerid
echo "$veth $line"
done <<< "$containers"