]> git.ipfire.org Git - people/ms/network.git/blame - functions.cli
stp: Correctly set protocol version.
[people/ms/network.git] / functions.cli
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
e145f826
MT
22IDENT=" "
23
1d7bc4f3 24function cli_help_requested() {
866de228
MT
25 local argument="${1}"
26
27 if [ -n "${argument}" ]; then
28 if listmatch ${argument} help -h --help; then
1d7bc4f3
MT
29 return ${EXIT_OK}
30 fi
866de228 31 fi
1d7bc4f3
MT
32
33 return ${EXIT_ERROR}
34}
35
4fedddef
MT
36function cli_run_help() {
37 local command="$@"
38
39 print "Run \"${command} help\" to get more information."
40 return ${EXIT_OK}
41}
42
3cb2fc42
MT
43function cli_device_headline() {
44 local device=${1}
45 assert isset device
46
fe8e6d69
MT
47 local long=0
48 shift
49 while [ $# -gt 0 ]; do
50 case "${1}" in
51 --long)
52 long=1
53 ;;
54 esac
55 shift
56 done
57
3cb2fc42
MT
58 local type
59 if zone_exists ${device}; then
60 type="zone"
61 elif port_exists ${device}; then
62 type="port"
63 else
64 type="unknown"
65 fi
66
67 local headline_prefix
68 case "${type}" in
69 zone)
70 headline_prefix="Zone ${device}"
71 ;;
72 port)
fe8e6d69 73 headline_prefix="Port ${device} ($(device_get_type ${device}))"
3cb2fc42
MT
74 ;;
75 *)
fe8e6d69 76 headline_prefix="Device ${device} ($(device_get_type ${device}))"
3cb2fc42
MT
77 ;;
78 esac
79
3cb2fc42
MT
80 # Print the hook for all zones.
81 if [ "${type}" = "zone" ]; then
8e3508ac 82 headline_prefix="${headline_prefix} ($(zone_get_hook ${device}))"
3cb2fc42 83 fi
8e3508ac 84 cli_headline 1 "${headline_prefix}"
3cb2fc42
MT
85
86 # Print the device status.
8e3508ac
MT
87 local status
88 case "$(device_get_status ${device})" in
89 ${STATUS_UP})
90 status=${MSG_DEVICE_STATUS_UP}
91 ;;
92 ${STATUS_DOWN})
93 status=${MSG_DEVICE_STATUS_DOWN}
94 ;;
95 ${STATUS_NOCARRIER})
96 status=${MSG_DEVICE_STATUS_NOCARRIER}
97 ;;
98 *)
99 status=${MSG_DEVICE_STATUS_UNKNOWN}
100 ;;
101 esac
102 cli_print_fmt1 1 "Status" "${status}"
f8f476d8 103 if enabled long; then
fe8e6d69
MT
104 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
105 fi
3cb2fc42
MT
106 if device_is_up ${device}; then
107 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
108 fi
f8f476d8 109 if enabled long; then
fe8e6d69
MT
110 device_is_promisc ${device} &>/dev/null
111 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
112 fi
3cb2fc42
MT
113
114 cli_space
ec63256a 115
3cb2fc42
MT
116 # Print the device stats.
117 device_is_up ${device} && cli_device_stats 2 ${device}
fe8e6d69 118
f8f476d8 119 if enabled long; then
fe8e6d69 120 # Virtual devices.
7951525a 121 device_is_vlan ${device} && cli_device_vlan ${device}
fe8e6d69
MT
122
123 # Bonded devices.
124 device_is_bonded ${device} && cli_device_bonded ${device}
125
126 # Bonding devices.
127 device_is_bonding ${device} && cli_device_bonding ${device}
128 fi
3cb2fc42
MT
129}
130
131function cli_device_stats() {
132 local level=${1}
133 local device=${2}
134
135 # This section will print statistical data from the device.
136 local packets bytes errors
137
138 cli_headline ${level} "Statistics"
139 local format="%-10s %9d packets %6s (%d errors)"
140
141 # RX
142 packets=$(device_get_rx_packets ${device})
143 bytes=$(device_get_rx_bytes ${device})
144 errors=$(device_get_rx_errors ${device})
145
146 cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
147
148 # TX
149 packets=$(device_get_tx_packets ${device})
150 bytes=$(device_get_tx_bytes ${device})
151 errors=$(device_get_tx_errors ${device})
152
153 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
154 cli_space
ec63256a
MT
155}
156
7951525a 157function cli_device_vlan() {
fe8e6d69
MT
158 local device=${1}
159
160 cli_headline 2 "VLAN"
161
7951525a
MT
162 cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
163 cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
fe8e6d69
MT
164 cli_space
165}
166
167function cli_device_bonded() {
168 local device=${1}
169
170 cli_headline 2 "Bonding information"
171
172 local master=$(bonding_slave_get_master ${port})
173 cli_print_fmt1 2 "Master" "${master}"
174
175 local active
176 [ "$(bonding_get_active_slave ${master})" = "${port}" ]
177 cli_print_fmt1 2 "Active slave" "$(cli_print_yesno $?)"
178 cli_space
179}
180
181function cli_device_bonding() {
182 local device=${1}
183
184 cli_headline 2 "Bonding information"
185
186 cli_print_fmt1 2 "Mode" "$(bonding_get_mode ${port})"
187 # XXX lacp rate
188 cli_space
189
190 local slave slave_prefix
191 local slave_active=$(bonding_get_active_slave ${device})
192 for slave in $(bonding_get_slaves ${device}); do
193 if [ "${slave_active}" = "${slave}" ]; then
194 slave_prefix="Slave (active)"
195 else
196 slave_prefix="Slave"
197 fi
198 cli_print_fmt1 2 "${slave_prefix}" "${slave}"
199 done
200 cli_space
201}
202
8e3508ac
MT
203function cli_headline() {
204 local level=${1}
205 local format=${2}
206 shift 2
9178284d 207
8e3508ac 208 local ident=$(cli_ident ${level})
9178284d 209
8e3508ac 210 local out
a45d972f 211 printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
8e3508ac 212 printf "${out}"
9178284d
MT
213}
214
8e3508ac 215function cli_statusline() {
ec63256a
MT
216 local level=${1}
217 shift
218
8e3508ac
MT
219 local head=${1}
220 shift
ec63256a 221
8e3508ac 222 cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
ec63256a
MT
223}
224
225function cli_print() {
226 local level=${1}
227 local format=${2}
228 shift 2
229
230 local ident=$(cli_ident $(( ${level} + 1 )))
231
232 local out
233 printf -v out "${ident}${format}\n" "$@"
234 printf "${out}"
235}
236
237function cli_print_fmt1() {
238 local level=${1}
239 shift
240
e145f826 241 local space=$(( 30 - (${level} * ${#IDENT}) ))
ec63256a
MT
242 local format="%-${space}s %s"
243
244 cli_print ${level} "${format}" "$@"
245}
246
247function cli_print_bool() {
248 if [ "${1}" = "${EXIT_TRUE}" ]; then
249 echo "true"
250 else
251 echo "false"
252 fi
253}
254
255function cli_print_yesno() {
256 if [ "${1}" = "${EXIT_TRUE}" ]; then
257 echo "yes"
258 else
259 echo "false"
260 fi
261}
262
e145f826
MT
263function cli_print_enabled() {
264 enabled ${1}
265
266 cli_print_bool $?
267}
268
3cb2fc42
MT
269function cli_print_warning() {
270 local level=${1}
271 shift
272
273 cli_print ${level} "${COLOUR_WARN}%s${COLOUR_NORMAL}" "$@"
274}
275
ec63256a
MT
276function cli_space() {
277 printf "\n"
278}
279
280function cli_ident() {
281 local level=${1}
111c94e2 282 assert isinteger level
ec63256a
MT
283
284 local ident=""
285 while [ ${level} -gt 1 ]; do
e145f826 286 ident="${ident}${IDENT}"
ec63256a
MT
287 level=$(( ${level} - 1 ))
288 done
289
111c94e2 290 print "${ident}"
9178284d 291}
f90e550b
MT
292
293function cli_yesno() {
39f552f5 294 local message="$@ [y/n] "
f90e550b
MT
295 local yesno
296
39f552f5
MT
297 while true; do
298 printf "\n${message}"
299 read yesno
f90e550b 300
39f552f5
MT
301 # Check for "yes".
302 if listmatch ${yesno} y Y yes YES Yes; then
303 return ${EXIT_TRUE}
f90e550b 304
39f552f5
MT
305 # Check for "no".
306 elif listmatch ${yesno} n N no NO No; then
307 return ${EXIT_FALSE}
308 fi
309 done
f90e550b 310}
d76f5107
MT
311
312function cli_get_key() {
313 local key="${1%%=*}"
314 echo "${key/--/}"
315}
316
317function cli_get_val() {
204bb6ad 318 echo "${@#*=}"
d76f5107 319}
de28a630
MT
320
321function cli_usage() {
322 local command="$@"
323 local basename="$(basename ${0})"
324
325 if ! isset command; then
326 command="${basename} help"
327 fi
328
329 echo "The given command was not understood by ${basename}." >&2
330 echo "Please run '${command}' for detailed help." >&2
331}
332
333function cli_show_man() {
334 local manpage=${1}
335 assert isset manpage
336
337 if ! binary_exists man; then
338 error "The man package is not installed on this system."
339 error "Please install 'man' in order to view the help."
340 exit ${EXIT_ERROR}
341 fi
342
343 man ${manpage}
344}