2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
24 function cli_help_requested
() {
27 if [ -n "${argument}" ]; then
28 if listmatch
${argument} help -h --help; then
36 function cli_run_help
() {
39 print
"Run \"${command} help\" to get more information."
43 function cli_device_headline
() {
49 while [ $# -gt 0 ]; do
59 if zone_exists
${device}; then
61 elif port_exists
${device}; then
70 headline_prefix
="Zone ${device}"
73 headline_prefix
="Port ${device} ($(device_get_type ${device}))"
76 headline_prefix
="Device ${device} ($(device_get_type ${device}))"
80 # Print the hook for all zones.
81 if [ "${type}" = "zone" ]; then
83 zone_is_enabled
"${device}"
93 local hook
="$(zone_get_hook "${device}")"
94 headline_prefix
="${headline_prefix} (${enabled}, ${hook})"
96 cli_headline
1 "${headline_prefix}"
98 # Print the device status.
100 case "$(device_get_status ${device})" in
102 status
=${MSG_DEVICE_STATUS_UP}
105 status
=${MSG_DEVICE_STATUS_DOWN}
108 status
=${MSG_DEVICE_STATUS_NOCARRIER}
111 status
=${MSG_DEVICE_STATUS_UNKNOWN}
114 cli_print_fmt1
1 "Status" "${status}"
115 if enabled long
; then
116 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
118 if device_is_up
${device}; then
119 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
121 if enabled long
; then
122 device_is_promisc
${device} &>/dev
/null
123 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
128 # Print the device stats.
129 device_is_up
${device} && cli_device_stats
2 ${device}
131 if enabled long
; then
133 device_is_vlan
${device} && cli_device_vlan
${device}
136 device_is_bonded
${device} && cli_device_bonded
${device}
139 device_is_bonding
${device} && cli_device_bonding
${device}
143 function cli_device_stats
() {
147 # This section will print statistical data from the device.
148 local packets bytes errors
150 cli_headline
${level} "Statistics"
151 local format
="%-10s %9d packets %6s (%d errors)"
154 packets
=$
(device_get_rx_packets
${device})
155 bytes
=$
(device_get_rx_bytes
${device})
156 errors
=$
(device_get_rx_errors
${device})
158 cli_print
${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
161 packets=$(device_get_tx_packets ${device})
162 bytes=$(device_get_tx_bytes ${device})
163 errors=$(device_get_tx_errors ${device})
165 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
169 function cli_device_vlan
() {
172 cli_headline
2 "VLAN"
174 cli_print_fmt1
2 "Parent" "$(vlan_get_parent ${device})"
175 cli_print_fmt1
2 "VID" "$(vlan_get_id ${device})"
179 function cli_device_bonded
() {
182 cli_headline
2 "Bonding information"
184 local master
=$
(bonding_slave_get_master
${port})
185 cli_print_fmt1
2 "Master" "${master}"
187 local mode
=$
(bonding_get_mode
${master})
188 if [ "${mode}" = "active-backup" ]; then
189 local active_slaves
=$
(bonding_get_slaves
${master} --active)
191 if list_match
"${device}" ${active_slaves}; then
194 cli_print_fmt1
2 "Active slave" "$(cli_print_yesno ${active})"
200 function cli_device_bonding
() {
204 cli_headline
2 "Bonding information"
206 local mode
=$
(bonding_get_mode
${device})
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}"
215 local slave slave_suffix
216 local active_slaves
=$
(bonding_get_slaves
${device} --active)
217 for slave
in $
(bonding_get_slaves
${device}); do
218 # Print the device status.
220 case "$(device_get_status ${slave})" in
222 status
=${MSG_DEVICE_STATUS_UP}
225 status
=${MSG_DEVICE_STATUS_DOWN}
228 status
=${MSG_DEVICE_STATUS_NOCARRIER}
231 status
=${MSG_DEVICE_STATUS_UNKNOWN}
235 if list_match
"${slave}" ${active_slaves}; then
236 slave_suffix
="(active)"
240 cli_print_fmt1
2 "Slave ${slave}" "${status} ${slave_suffix}"
245 function cli_headline
() {
250 local ident
=$
(cli_ident
${level})
253 printf -v out
"${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
257 function cli_statusline
() {
264 cli_print $
(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
267 function cli_print
() {
272 local ident
=$
(cli_ident $
(( ${level} + 1 )))
275 printf -v out
"${ident}${format}\n" "$@"
279 function cli_print_fmt1
() {
283 local space
=$
(( 34 - (${level} * ${#IDENT}) ))
284 local format
="%-${space}s %s"
286 cli_print
${level} "${format}" "$@"
289 function cli_print_bool
() {
290 if [ "${1}" = "${EXIT_TRUE}" ]; then
297 function cli_print_yesno
() {
298 if [ "${1}" = "${EXIT_TRUE}" ]; then
305 function cli_print_enabled
() {
311 function cli_print_warning
() {
315 cli_print
${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@
"
318 function cli_space() {
322 function cli_ident() {
324 assert isinteger level
327 while [ ${level} -gt 1 ]; do
328 ident="${ident}${IDENT}"
329 level=$(( ${level} - 1 ))
335 function cli_yesno() {
336 local message="$@
[y
/n
] "
340 printf "\n${message}"
344 if listmatch ${yesno} y Y yes YES Yes; then
348 elif listmatch ${yesno} n N no NO No; then
354 function cli_get_key() {
359 function cli_get_val() {
363 function cli_get_bool() {
364 local value="$
(cli_get_val
"$@")"
366 if enabled value; then
375 function cli_usage() {
377 local basename="$
(basename ${0})"
379 if ! isset command; then
380 command="${basename} help"
383 echo "The given
command was not understood by
${basename}.
" >&2
384 echo "Please run
'${command}' for detailed
help.
" >&2
387 function cli_show_man() {
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.
"