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