]> git.ipfire.org Git - people/stevee/network.git/blob - functions.cli
Don't strip "" from strings that contain spaces.
[people/stevee/network.git] / functions.cli
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
22 IDENT=" "
23
24 function cli_help_requested() {
25 local argument="${1}"
26
27 if [ -n "${argument}" ]; then
28 if listmatch ${argument} help -h --help; then
29 return ${EXIT_OK}
30 fi
31 fi
32
33 return ${EXIT_ERROR}
34 }
35
36 function cli_run_help() {
37 local command="$@"
38
39 print "Run \"${command} help\" to get more information."
40 return ${EXIT_OK}
41 }
42
43 function cli_device_headline() {
44 local device=${1}
45 assert isset device
46
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
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)
73 headline_prefix="Port ${device} ($(device_get_type ${device}))"
74 ;;
75 *)
76 headline_prefix="Device ${device} ($(device_get_type ${device}))"
77 ;;
78 esac
79
80 # Print the hook for all zones.
81 if [ "${type}" = "zone" ]; then
82 headline_prefix="${headline_prefix} ($(zone_get_hook ${device}))"
83 fi
84 cli_headline 1 "${headline_prefix}"
85
86 # Print the device status.
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}"
103 if enabled long; then
104 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
105 fi
106 if device_is_up ${device}; then
107 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
108 fi
109 if enabled long; then
110 device_is_promisc ${device} &>/dev/null
111 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
112 fi
113
114 cli_space
115
116 # Print the device stats.
117 device_is_up ${device} && cli_device_stats 2 ${device}
118
119 if enabled long; then
120 # Virtual devices.
121 device_is_vlan ${device} && cli_device_vlan ${device}
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
129 }
130
131 function 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
155 }
156
157 function cli_device_vlan() {
158 local device=${1}
159
160 cli_headline 2 "VLAN"
161
162 cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
163 cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
164 cli_space
165 }
166
167 function 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
181 function 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
203 function cli_headline() {
204 local level=${1}
205 local format=${2}
206 shift 2
207
208 local ident=$(cli_ident ${level})
209
210 local out
211 printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
212 printf "${out}"
213 }
214
215 function cli_statusline() {
216 local level=${1}
217 shift
218
219 local head=${1}
220 shift
221
222 cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
223 }
224
225 function 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
237 function cli_print_fmt1() {
238 local level=${1}
239 shift
240
241 local space=$(( 30 - (${level} * ${#IDENT}) ))
242 local format="%-${space}s %s"
243
244 cli_print ${level} "${format}" "$@"
245 }
246
247 function cli_print_bool() {
248 if [ "${1}" = "${EXIT_TRUE}" ]; then
249 echo "true"
250 else
251 echo "false"
252 fi
253 }
254
255 function cli_print_yesno() {
256 if [ "${1}" = "${EXIT_TRUE}" ]; then
257 echo "yes"
258 else
259 echo "false"
260 fi
261 }
262
263 function cli_print_enabled() {
264 enabled ${1}
265
266 cli_print_bool $?
267 }
268
269 function cli_print_warning() {
270 local level=${1}
271 shift
272
273 cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
274 }
275
276 function cli_space() {
277 printf "\n"
278 }
279
280 function cli_ident() {
281 local level=${1}
282 assert isinteger level
283
284 local ident=""
285 while [ ${level} -gt 1 ]; do
286 ident="${ident}${IDENT}"
287 level=$(( ${level} - 1 ))
288 done
289
290 print "${ident}"
291 }
292
293 function cli_yesno() {
294 local message="$@ [y/n] "
295 local yesno
296
297 while true; do
298 printf "\n${message}"
299 read yesno
300
301 # Check for "yes".
302 if listmatch ${yesno} y Y yes YES Yes; then
303 return ${EXIT_TRUE}
304
305 # Check for "no".
306 elif listmatch ${yesno} n N no NO No; then
307 return ${EXIT_FALSE}
308 fi
309 done
310 }
311
312 function cli_get_key() {
313 local key="${1%%=*}"
314 echo "${key/--/}"
315 }
316
317 function cli_get_val() {
318 echo "${@#*=}"
319 }
320
321 function 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
333 function 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 }