interface_information.sh
2.64 KB
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
#!/bin/dash
##
# This creates function that will gather some system informations
# and propagate them as environment variables.
#
. ./utils.sh
##
# retrieve interfaces. I prefer using ip if it is
# available, else I fallback to ifconfig... don't know
# how to retrieve this information on other systems.
# This also exports the environment variables.
#
get_interface_data() {
local NO=1
if [ ${IP} ]
then
get_interface_data_ip
else
if [ ${IFCONFIG} ]
then
get_interface_data_ifconfig
else
${LOGGER} -p local0.warn 'Found no way to retrieve interface information.'
fi
fi
export NINTERFACES
export CLASSES
while [ ${NO} -le ${NINTERFACES:=0} ]
do
export IF${NO}_NAME IF${NO}_MAC IF${NO}_STATE IF${NO}_IPV4 IF${NO}_IPV6
NO=$((NO+1))
done
}
##
# get the interface information from the ip tool
#
get_interface_data_ip() {
eval $(${IP} -o link | ${AWK} '{
sub(/:/,"",$1);
no=$1;
sub(/:/,"",$2);
name=$2;
for (i=3; i<NF; i++) {
if ($i == "state") {
i++; state=$i
}
if ($i ~ /link/) {
i++; mac=$i;
classes=classes mac " "
}
}
print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";";
if ("UP" == state) classes=classes mac " "
}
BEGIN {
classes=""
}
END {
print "CLASSES=\"${CLASSES}" classes " \";";
print "NINTERFACES=" FNR ";"
}')
eval $(${IP} -o addr | ${AWK} '{
sub(/:/,"",$1);
no=$1;
if ($3 == "inet") {
sub(/[\/%].*/,"",$4);
print "IF" no "_IPV4=\"${IF" no "_IPV4}" $4 " \";";
classes=classes $4 " "
}
if ($3 == "inet6") {
sub(/[\/%].*/,"",$4);
print "IF" no "_IPV6=\"${IF" no "_IPV6}" $4 " \";";
classes=classes $4 " "
}
}
BEGIN {
classes=""
}
END {
print "CLASSES=\"${CLASSES}" classes " \";"
}')
}
##
# get interface data via the ifconfig tool
#
get_interface_data_ifconfig() {
eval $(${IFCONFIG} -a | ${AWK} '
/ether/ { mac=$2 }
/inet / { ipv4=ipv4 $2 " " }
/inet6/ { ipv6=ipv6 $2 " " }
/^[^ \t]/ {
if ("" != ipv4 || "" != ipv6) state="UP"; else state="DOWN";
if ("" != name) {
print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";" \
"IF" no "_IPV4=\"${IF" no "_IPV4}" ipv4 " \";" \
"IF" no "_IPV6=\"${IF" no "_IPV6}" ipv6 " \";";
no++;
}
ipv4=ipv6="";
sub(/:/,"",$1);
name=$1
}
BEGIN {
no=1;
}
END {
if ("" != ipv4 || "" != ipv6) state="UP"; else state="DOWN";
print "IF" no "_NAME=" name ";IF" no "_STATE=" state ";IF" no "_MAC=" mac ";" \
"IF" no "_IPV4=\"${IF" no "_IPV4}" ipv4 " \";" \
"IF" no "_IPV6=\"${IF" no "_IPV6}" ipv6 " \";";
print "NINTERFACES=" no
}')
}
##
# autorun this if sourced.
#
[ -z "${NINTERFACES}" ] && get_interface_data
# vim: set ts=4 sw=4: