-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnettree.sh
executable file
·104 lines (90 loc) · 2.01 KB
/
nettree.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
#!/bin/bash
DEFAULT_INDENT=" "
DIRECTION="UP"
FILE=""
function usage()
{
cat << USAGEEND
The script prints network devices hierarchy as a tree view.
Possible arguments:
-u prints tree from bottom to up (default). Physical devices are roots of the tree.
-d prints tree from up to bottom. Logica, devices are roots of the tree.
-f <file> use this file as an input file instead of parsing localy "ip -o link" command.
USAGEEND
}
function printdown()
{
local indent="$1"
devs="$2"
for indev in $devs
do
echo "$indent" "$indev"
printdown "$DEFAULT_INDENT$indent" "${devicesdown[$indev]}"
done
}
function printup()
{
local indent="$1"
devs="$2"
for indev in $devs
do
echo "$indent" "$indev"
printup "$DEFAULT_INDENT$indent" "${devicesup[$indev]}"
done
}
while getopts "duhf:" option; do
case "$option" in
d) DIRECTION=DOWN
;;
u) DIRECTION=UP
;;
f) fileflag=1 ;
FILE=${OPTARG}
;;
h) usage
exit 0
;;
*) usage
exit 1
;;
esac
done
declare -A devicesup
declare -A devicesdown
while read line
do
dev=${line#*: }
dev=${dev%%:*}
devicesup[$dev]=""
if [ -z "${devicesdown[$dev]}" ]
then
devicesdown[$dev]=""
fi
if [[ "$line" == *" master "* ]]
then
master=${line#* master *}
master=${master%% *}
devicesup[$dev]="${devicesup[$dev]} $master"
devicesdown[$master]="${devicesdown[$master]} $dev"
fi
done < <( if [ $fileflag ] ; then cat $FILE |grep BROADCAST ; else ip -o link ; fi )
if [ "$DIRECTION" == "UP" ]
then
for dev in ${!devicesup[@]}
do
if [ -z "${devicesdown[$dev]}" ]
then
echo $dev
printup "$DEFAULT_INDENT" "${devicesup[$dev]}"
fi
done
else
for dev in "${!devicesdown[@]}"
do
if [ -z "${devicesup[$dev]}" ]
then
echo $dev
printdown "$DEFAULT_INDENT" "${devicesdown[$dev]}"
fi
done
fi