]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.cli
ipv4-static: Update hook
[people/stevee/network.git] / src / functions / 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
ccbc0dd4 29 return ${EXIT_TRUE}
1d7bc4f3 30 fi
866de228 31 fi
1d7bc4f3 32
ccbc0dd4 33 return ${EXIT_FALSE}
1d7bc4f3
MT
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
f139f146
MT
82 local enabled
83 zone_is_enabled "${device}"
84 case "$?" in
85 ${EXIT_TRUE})
86 enabled="enabled"
87 ;;
88 ${EXIT_FALSE})
89 enabled="disabled"
90 ;;
91 esac
92
93 local hook="$(zone_get_hook "${device}")"
94 headline_prefix="${headline_prefix} (${enabled}, ${hook})"
3cb2fc42 95 fi
8e3508ac 96 cli_headline 1 "${headline_prefix}"
3cb2fc42
MT
97
98 # Print the device status.
8e3508ac
MT
99 local status
100 case "$(device_get_status ${device})" in
101 ${STATUS_UP})
102 status=${MSG_DEVICE_STATUS_UP}
103 ;;
104 ${STATUS_DOWN})
105 status=${MSG_DEVICE_STATUS_DOWN}
106 ;;
107 ${STATUS_NOCARRIER})
108 status=${MSG_DEVICE_STATUS_NOCARRIER}
109 ;;
110 *)
111 status=${MSG_DEVICE_STATUS_UNKNOWN}
112 ;;
113 esac
114 cli_print_fmt1 1 "Status" "${status}"
f8f476d8 115 if enabled long; then
fe8e6d69
MT
116 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
117 fi
3cb2fc42
MT
118 if device_is_up ${device}; then
119 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
120 fi
f8f476d8 121 if enabled long; then
fe8e6d69
MT
122 device_is_promisc ${device} &>/dev/null
123 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
124 fi
3cb2fc42
MT
125
126 cli_space
ec63256a 127
3cb2fc42
MT
128 # Print the device stats.
129 device_is_up ${device} && cli_device_stats 2 ${device}
fe8e6d69 130
f8f476d8 131 if enabled long; then
fe8e6d69 132 # Virtual devices.
7951525a 133 device_is_vlan ${device} && cli_device_vlan ${device}
fe8e6d69
MT
134
135 # Bonded devices.
136 device_is_bonded ${device} && cli_device_bonded ${device}
137
138 # Bonding devices.
139 device_is_bonding ${device} && cli_device_bonding ${device}
140 fi
3cb2fc42
MT
141}
142
143function cli_device_stats() {
144 local level=${1}
145 local device=${2}
146
147 # This section will print statistical data from the device.
148 local packets bytes errors
149
150 cli_headline ${level} "Statistics"
151 local format="%-10s %9d packets %6s (%d errors)"
152
153 # RX
154 packets=$(device_get_rx_packets ${device})
155 bytes=$(device_get_rx_bytes ${device})
156 errors=$(device_get_rx_errors ${device})
157
158 cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
159
160 # TX
161 packets=$(device_get_tx_packets ${device})
162 bytes=$(device_get_tx_bytes ${device})
163 errors=$(device_get_tx_errors ${device})
164
165 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
166 cli_space
ec63256a
MT
167}
168
7951525a 169function cli_device_vlan() {
fe8e6d69
MT
170 local device=${1}
171
172 cli_headline 2 "VLAN"
173
7951525a
MT
174 cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
175 cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
fe8e6d69
MT
176 cli_space
177}
178
179function cli_device_bonded() {
180 local device=${1}
181
182 cli_headline 2 "Bonding information"
183
184 local master=$(bonding_slave_get_master ${port})
185 cli_print_fmt1 2 "Master" "${master}"
186
d1e71061
MT
187 local mode=$(bonding_get_mode ${master})
188 if [ "${mode}" = "active-backup" ]; then
189 local active_slaves=$(bonding_get_slaves ${master} --active)
190 local active="false"
191 if list_match "${device}" ${active_slaves}; then
192 active="true"
193 fi
194 cli_print_fmt1 2 "Active slave" "$(cli_print_yesno ${active})"
195 fi
196
fe8e6d69
MT
197 cli_space
198}
199
200function cli_device_bonding() {
201 local device=${1}
d1e71061 202 assert isset device
fe8e6d69
MT
203
204 cli_headline 2 "Bonding information"
205
d1e71061
MT
206 local mode=$(bonding_get_mode ${device})
207
208 cli_print_fmt1 2 "Mode" "${mode}"
209 if [ "${mode}" = "802.3ad" ]; then
210 local lacp_rate=$(bonding_get_lacp_rate ${device})
211 cli_print_fmt1 2 "LACP rate" "${lacp_rate}"
212 fi
fe8e6d69
MT
213 cli_space
214
d1e71061
MT
215 local slave slave_suffix
216 local active_slaves=$(bonding_get_slaves ${device} --active)
fe8e6d69 217 for slave in $(bonding_get_slaves ${device}); do
d1e71061
MT
218 # Print the device status.
219 local status
220 case "$(device_get_status ${slave})" in
221 ${STATUS_UP})
222 status=${MSG_DEVICE_STATUS_UP}
223 ;;
224 ${STATUS_DOWN})
225 status=${MSG_DEVICE_STATUS_DOWN}
226 ;;
227 ${STATUS_NOCARRIER})
228 status=${MSG_DEVICE_STATUS_NOCARRIER}
229 ;;
230 *)
231 status=${MSG_DEVICE_STATUS_UNKNOWN}
232 ;;
233 esac
234
235 if list_match "${slave}" ${active_slaves}; then
236 slave_suffix="(active)"
fe8e6d69 237 else
d1e71061 238 slave_suffix=""
fe8e6d69 239 fi
d1e71061 240 cli_print_fmt1 2 "Slave ${slave}" "${status} ${slave_suffix}"
fe8e6d69
MT
241 done
242 cli_space
243}
244
8e3508ac
MT
245function cli_headline() {
246 local level=${1}
247 local format=${2}
248 shift 2
9178284d 249
8e3508ac 250 local ident=$(cli_ident ${level})
9178284d 251
8e3508ac 252 local out
a45d972f 253 printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
8e3508ac 254 printf "${out}"
9178284d
MT
255}
256
8e3508ac 257function cli_statusline() {
ec63256a
MT
258 local level=${1}
259 shift
260
8e3508ac
MT
261 local head=${1}
262 shift
ec63256a 263
8e3508ac 264 cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
ec63256a
MT
265}
266
267function cli_print() {
268 local level=${1}
269 local format=${2}
270 shift 2
271
272 local ident=$(cli_ident $(( ${level} + 1 )))
273
274 local out
275 printf -v out "${ident}${format}\n" "$@"
276 printf "${out}"
277}
278
279function cli_print_fmt1() {
280 local level=${1}
281 shift
282
e6993835 283 local space=$(( 34 - (${level} * ${#IDENT}) ))
ec63256a
MT
284 local format="%-${space}s %s"
285
286 cli_print ${level} "${format}" "$@"
287}
288
289function cli_print_bool() {
290 if [ "${1}" = "${EXIT_TRUE}" ]; then
291 echo "true"
292 else
293 echo "false"
294 fi
295}
296
297function cli_print_yesno() {
298 if [ "${1}" = "${EXIT_TRUE}" ]; then
299 echo "yes"
300 else
301 echo "false"
302 fi
303}
304
e145f826
MT
305function cli_print_enabled() {
306 enabled ${1}
307
308 cli_print_bool $?
309}
310
3cb2fc42
MT
311function cli_print_warning() {
312 local level=${1}
313 shift
314
fcbf6823 315 cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
3cb2fc42
MT
316}
317
ec63256a
MT
318function cli_space() {
319 printf "\n"
320}
321
322function cli_ident() {
323 local level=${1}
111c94e2 324 assert isinteger level
ec63256a
MT
325
326 local ident=""
327 while [ ${level} -gt 1 ]; do
e145f826 328 ident="${ident}${IDENT}"
ec63256a
MT
329 level=$(( ${level} - 1 ))
330 done
331
111c94e2 332 print "${ident}"
9178284d 333}
f90e550b
MT
334
335function cli_yesno() {
39f552f5 336 local message="$@ [y/n] "
f90e550b
MT
337 local yesno
338
39f552f5
MT
339 while true; do
340 printf "\n${message}"
341 read yesno
f90e550b 342
39f552f5
MT
343 # Check for "yes".
344 if listmatch ${yesno} y Y yes YES Yes; then
345 return ${EXIT_TRUE}
f90e550b 346
39f552f5
MT
347 # Check for "no".
348 elif listmatch ${yesno} n N no NO No; then
349 return ${EXIT_FALSE}
350 fi
351 done
f90e550b 352}
d76f5107
MT
353
354function cli_get_key() {
355 local key="${1%%=*}"
356 echo "${key/--/}"
357}
358
359function cli_get_val() {
204bb6ad 360 echo "${@#*=}"
d76f5107 361}
de28a630 362
08e40c8c
MT
363function cli_get_bool() {
364 local value="$(cli_get_val "$@")"
365
366 if enabled value; then
367 print "true"
368 return ${EXIT_TRUE}
369 fi
370
371 print "false"
372 return ${EXIT_FALSE}
373}
374
de28a630
MT
375function cli_usage() {
376 local command="$@"
377 local basename="$(basename ${0})"
378
379 if ! isset command; then
380 command="${basename} help"
381 fi
382
383 echo "The given command was not understood by ${basename}." >&2
384 echo "Please run '${command}' for detailed help." >&2
385}
386
387function cli_show_man() {
388 local manpage=${1}
389 assert isset manpage
390
391 if ! binary_exists man; then
392 error "The man package is not installed on this system."
393 error "Please install 'man' in order to view the help."
394 exit ${EXIT_ERROR}
395 fi
396
397 man ${manpage}
398}