forked from micahculpepper/dockerveth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerveth.sh
144 lines (122 loc) · 3.87 KB
/
dockerveth.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh
# Copyright (c) 2017 Micah Culpepper
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#####################
# DECLARE CONSTANTS #
#####################
NL=$'\n'
####################
# DEFINE FUNCTIONS #
####################
usage () {
printf %s \
"dockerveth.sh - Show which docker containers are attached to which
\`veth\` interfaces.
Usage: dockerveth.sh [DOCKER PS OPTIONS] | [-h, --help]
Options:
DOCKER PS OPTIONS Pass any valid \`docker ps\` flags. Do not pass
a '--format' flag.
-h, --help Show this help and exit.
Output:
If stdout is not a tty, column headers are omitted.
"
}
get_container_data () {
# Get data about the running containers. Accepts arbitrary arguments, so you can pass
# a filter to `docker ps` if desired.
# Input: `docker ps` arguments (optional)
# Output: A multi-line string where each line contains the container id, followed by
# a space, and then any friendly names.
docker ps --format '{{.ID}} {{.Names}}' "$@"
}
get_veth () {
# Get the host veth interface attached to a container.
# Input: docker container ID; also needs $dockerveth__addrs
# Output: the veth name, like "veth6638cfa"
c_if_index=$(get_container_if_index "$1")
a="${dockerveth__addrs%%@if${c_if_index}:*}"
b="${a##*${NL}}"
printf "${b#* }"
}
get_container_if_index () {
# Get the index number of a docker container's first veth interface (typically eth0)
# Input: the container ID
# Output: The index number, like "42"
c_pid=$(get_pid "$1")
ip_netns_export "$c_pid"
ils=$(ip netns exec "ns-${c_pid}" ip link show type veth)
printf "${ils%%:*}"
}
ip_netns_export () {
# Make a docker container's networking info available to `ip netns`
# Input: the container's PID
# Output: None (besides return code), but performs the set-up so that `ip netns` commands
# can access this container's namespace.
if [ ! -d /var/run/netns ]; then
mkdir -p /var/run/netns
fi
ln -sf "/proc/${1}/ns/net" "/var/run/netns/ns-${1}"
}
get_pid () {
# Get the PID of a docker container
# Input: the container ID
# Output: The PID, like "2499"
docker inspect --format '{{.State.Pid}}' "$1"
}
make_row () {
# Produce a table row for output
# Input:
# 1 - The container ID
# 2 - The container's friendly name
# Output: A row of data, like "1e8656e195ba veth1ce04be thirsty_meitner"
id="${1}"
name="${2}"
veth=$(get_veth "$id")
printf "${id}\t${veth}\t${name}"
}
make_table () {
# Produce a table for output
# Input: raw data rows, like `c26682fe4545 friendly-name`
# Output: A multi-line string consisting of rows from `make_row`. Does not
# contain table column headers.
for i in $@; do
id="${i%% *}"
name="${i#* }"
r=$(make_row "$id" "$name")
printf "${r}\n"
done
}
######################
# PARSE COMMAND LINE #
######################
case "$1" in
-h|--help)
usage
exit 0
;;
*)
;;
esac
##################
# EXECUTE SCRIPT #
##################
set -e
container_data=$(get_container_data "$@")
dockerveth__addrs="$(ip address show)"
table=$(IFS="$NL"; make_table $container_data)
if [ -t 1 ]; then
printf "CONTAINER ID\tVETH \tNAMES\n"
fi
printf "${table}\n"