Skip to content

Commit f524502

Browse files
committed
tutorials/mtu: move NS and iface logic to common
Move logic to check for NSs and interfaces into common.sh so that it can be used by other scripts.
1 parent 2dbb5a1 commit f524502

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

tutorials/net/mtu/.bashrc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
source /bp/common.sh
2+
13
SCENARIO="error"
24
if [ -f /bp/.scenario ]; then
35
SCENARIO=$(< /bp/.scenario)
@@ -24,13 +26,6 @@ alias _exec='exec'
2426
alias help="/bp/help.sh"
2527
alias describe="/bp/describe.sh"
2628

27-
_check_ns(){
28-
if ! ip netns list | grep -qw ${1}; then
29-
echo "Invalid namespace: '${1}'"
30-
return 1
31-
fi
32-
}
33-
3429
exec(){
3530
if [ -z "${1:-}" ] || [ -z "${2:-}" ] ; then
3631
echo "Usage: exec <namespace_name> <command>"
@@ -43,7 +38,7 @@ exec(){
4338
}
4439

4540
export IGNOREEOF=3
46-
exit() {
41+
exit(){
4742
if [ "${NS}" != "" ]; then
4843
builtin exit;
4944
fi
@@ -66,7 +61,7 @@ shell(){
6661
ip netns exec ${1} bash --rcfile /bp/.bashrc -i
6762
}
6863

69-
_shell_completion() {
64+
_shell_completion(){
7065
local cur
7166
cur="${COMP_WORDS[COMP_CWORD]}"
7267
local namespaces=($(ip netns list | awk '{print $1}'))

tutorials/net/mtu/common.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@ log_debug(){
2424
_log "DBG" "${1}"
2525
}
2626

27+
# $1 namespace name
28+
_check_ns(){
29+
if ! ${SUDO} ip netns exec ${1} true >/dev/null 2>&1 ; then
30+
log_err "Invalid namespace: '${1}'"
31+
return 1
32+
fi
33+
}
34+
35+
# $1 namespace
36+
# $2 iface
37+
_check_ns_iface(){
38+
if ! ${SUDO} ip netns exec ${1} ip link show dev ${2} >/dev/null 2>&1 ; then
39+
log_err "Invalid iface: '${2}' in namespace '${1}'!"
40+
return 1
41+
fi
42+
43+
}
44+
45+
# Parses namespace and interface
46+
# and sets them to CMD_NS and CMD_IFACE
47+
# $1 namespace.interface
48+
_parse_ns_iface(){
49+
CMD_NS="$(echo ${1} | awk -F. '{print $1}')"
50+
CMD_IFACE="$(echo ${1} | awk -F. '{print $2}')"
51+
echo "Checking ${CMD_NS} and ${CMD_IFACE}"
52+
_check_ns ${CMD_NS}
53+
_check_ns_iface ${CMD_NS} ${CMD_IFACE}
54+
}
55+
2756
# $1 namespace name
2857
create_ns() {
2958
${SUDO} ip netns add ${1}

tutorials/net/mtu/describe.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ echo""
1616

1717
cat /bp/${SCENARIO}/TOPOLOGY.md
1818
echo""
19-
for ns in $(ip netns list | awk '{print $1}'); do
19+
for ns in $(${SUDO} ip netns list | awk '{print $1}'); do
2020
echo "================================================="
2121
echo -e "\033[1;34mNamespace: $ns\033[0m"
2222
echo "-------------------------------------------------"
2323
echo "IP addressing:"
24-
ip netns exec ${ns} ip -brief addr
24+
${SUDO} ip netns exec ${ns} ip -brief addr
2525
echo ""
2626
echo "Routing table:"
27-
ip netns exec ${ns} ip -brief route
27+
${SUDO} ip netns exec ${ns} ip -brief route
2828
echo -e "\n"
2929
done

0 commit comments

Comments
 (0)