forked from cslev/find_veth_docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_veth_docker.sh
executable file
·60 lines (49 loc) · 1.36 KB
/
find_veth_docker.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
#!/bin/bash
source sources/extra.sh
function show_help
{
c_print "Green" "This script finds out which vethXXXX is connected to what container!"
c_print "Bold" "Example: sudo ./find_veth_docker.sh -n <CONTAINER_NAME> -i <INTEFACE_IN_CONTAINER>"
c_print "Bold" "\t\t-n <CONTAINER_NAME>: set here the name of the container (Default: No name specified, printing all containers' data)."
c_print "Bold" "\t\t-i <INTERFACE_IN_CONTAINER>: set here the name of the interace in the container (Default: eth0)."
exit
}
NAME=""
INTF=""
while getopts "h?n:i:" opt
do
case "$opt" in
h|\?)
show_help
;;
n)
NAME=$OPTARG
;;
i)
INTF=$OPTARG
;;
*)
show_help
;;
esac
done
if [ -z $NAME ]
then
# c_print "Yellow" "No container name specified...looking for all veths...!"
cmd="sudo docker ps --format {{.Names}}"
else
cmd="sudo docker ps --format {{.Names}} -f name=$NAME"
fi
if [ -z $INTF ]
then
# c_print "Yellow" "No interface name specified in the container...Using default: ${INTF}!"
INTF="eth0"
fi
#getting the container names and interface data
c_print "BBlue" "VETH@HOST\tCONTAINER"
for i in $($cmd)
do
veth_in_container=$(sudo docker exec $i ip a|grep ${INTF}@|cut -d ':' -f 1)
veth_in_host=$(sudo ip a|grep "if${veth_in_container}:"|cut -d ":" -f 2|cut -d '@' -f 1|sed "s/ //g")
echo -e "${veth_in_host}\t${i}"
done