OSDN Git Service

5db6c897af778f99820c2c33708630b8f1900c90
[lxcf/lxcf.git] / lxcf / lib / lxcf-net-list
1
2 #!/bin/bash
3 # copyright (C) 2015 FUJITSU LIMITED All Rights Reserved
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; version 2
8 # of the License.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
18 # 02110-1301, USA.
19
20 # check distro
21 DISTRO=`/usr/lib64/lxcf/lxcf-distro`
22
23 # check root
24 if [ ${EUID:-${UID}} != 0 ]; then
25     echo "error: Because you are not root, you cannot execute this command. "
26     exit 1
27 fi
28
29 usage()
30 {
31         echo "usage: net-list [ -b ] [ -v ]"
32 }
33
34 # check options
35 FLG_B=0 
36 FLG_V=0 
37
38 while getopts bv OPT ; do
39   case $OPT in
40   b) FLG_B=1 ;;
41   v) FLG_V=1 ;;
42   esac
43 done
44 shift $((OPTIND - 1))
45
46 list_br()
47 {
48         brctl show
49 }
50
51 list_veth()
52 {
53         VETH_H=`ip a show | egrep -e "^[1-9]" -e inet | \
54           awk '{if (($1 == "inet") || ($1 == "inet6")){printf "\t%s",$2}else{printf "\n%s\t",$2}}END{printf "\n"}' | \
55           egrep ^vh | sed 's/://g'`
56         if [ x"$VETH_H" == x"" ]; then
57                 exit 0
58         fi
59         H_SRV=`for i in $VETH_H ; do echo $i | sed 's/^vh//g'  ; done`
60
61         for LXCNAME in $H_SRV
62         do
63                 brctl show  |& egrep ${LXCNAME} >& /dev/null
64                 if [ $? -ne 0 ]; then
65                         echo "error: can't find" ${LXCNAME}
66                         exit -1
67                 fi
68
69                 LXCPROC=`/usr/lib64/lxcf/lxcf-find-init ${LXCNAME}`
70                 if [ $? -ne 0 ]; then
71                         echo "error: can't find" ${LXCNAME}
72                         exit -1
73                 fi
74
75                 mkdir -p /var/run/netns/
76                 ln -s /proc/${LXCPROC}/ns/net /var/run/netns/${LXCNAME}
77
78                 ip netns exec ${LXCNAME} ip a show | egrep -e "^[1-9]" -e inet | \
79                   awk '{if (($1 == "inet") || ($1 == "inet6")){printf "\t%s",$2}else{printf "\n%s\t",$2}}END{printf "\n"}' | \
80                   egrep ^vg
81
82                 rm -f /var/run/netns/${LXCNAME}
83         done
84
85         exit 0
86 }
87
88 # check args
89 if [ $FLG_B -eq 1 ]; then
90         if [ $# -ne 0 ]; then
91                 usage
92                 exit -1
93         else
94                 list_br
95                 exit 0
96         fi
97 fi
98 if [ $FLG_V -eq 1 ]; then
99         if [ $# -ne 0 ]; then
100                 usage
101                 exit -1
102         else
103                 list_veth
104                 exit 0
105         fi
106 fi
107
108 if [ $# -ne 0 ]; then
109         usage
110         exit -1
111 fi
112
113 ip a show | egrep -e "^[1-9]" -e inet | \
114   awk '{if (($1 == "inet") || ($1 == "inet6")){printf "\t%s",$2}else{printf "\n%s\t",$2}}END{printf "\n"}'
115
116
117 exit 0