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
82 headline_prefix
="${headline_prefix} ($(zone_get_hook ${device}))"
84 cli_headline
1 "${headline_prefix}"
86 # Print the device status.
88 case "$(device_get_status ${device})" in
90 status
=${MSG_DEVICE_STATUS_UP}
93 status
=${MSG_DEVICE_STATUS_DOWN}
96 status
=${MSG_DEVICE_STATUS_NOCARRIER}
99 status
=${MSG_DEVICE_STATUS_UNKNOWN}
102 cli_print_fmt1
1 "Status" "${status}"
103 if enabled long
; then
104 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
106 if device_is_up
${device}; then
107 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
109 if enabled long
; then
110 device_is_promisc
${device} &>/dev
/null
111 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
116 # Print the device stats.
117 device_is_up
${device} && cli_device_stats
2 ${device}
119 if enabled long
; then
121 device_is_vlan
${device} && cli_device_vlan
${device}
124 device_is_bonded
${device} && cli_device_bonded
${device}
127 device_is_bonding
${device} && cli_device_bonding
${device}
131 function cli_device_stats
() {
135 # This section will print statistical data from the device.
136 local packets bytes errors
138 cli_headline
${level} "Statistics"
139 local format
="%-10s %9d packets %6s (%d errors)"
142 packets
=$
(device_get_rx_packets
${device})
143 bytes
=$
(device_get_rx_bytes
${device})
144 errors
=$
(device_get_rx_errors
${device})
146 cli_print
${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
149 packets=$(device_get_tx_packets ${device})
150 bytes=$(device_get_tx_bytes ${device})
151 errors=$(device_get_tx_errors ${device})
153 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
157 function cli_device_vlan
() {
160 cli_headline
2 "VLAN"
162 cli_print_fmt1
2 "Parent" "$(vlan_get_parent ${device})"
163 cli_print_fmt1
2 "VID" "$(vlan_get_id ${device})"
167 function cli_device_bonded
() {
170 cli_headline
2 "Bonding information"
172 local master
=$
(bonding_slave_get_master
${port})
173 cli_print_fmt1
2 "Master" "${master}"
175 local mode
=$
(bonding_get_mode
${master})
176 if [ "${mode}" = "active-backup" ]; then
177 local active_slaves
=$
(bonding_get_slaves
${master} --active)
179 if list_match
"${device}" ${active_slaves}; then
182 cli_print_fmt1
2 "Active slave" "$(cli_print_yesno ${active})"
188 function cli_device_bonding
() {
192 cli_headline
2 "Bonding information"
194 local mode
=$
(bonding_get_mode
${device})
196 cli_print_fmt1
2 "Mode" "${mode}"
197 if [ "${mode}" = "802.3ad" ]; then
198 local lacp_rate
=$
(bonding_get_lacp_rate
${device})
199 cli_print_fmt1
2 "LACP rate" "${lacp_rate}"
203 local slave slave_suffix
204 local active_slaves
=$
(bonding_get_slaves
${device} --active)
205 for slave
in $
(bonding_get_slaves
${device}); do
206 # Print the device status.
208 case "$(device_get_status ${slave})" in
210 status
=${MSG_DEVICE_STATUS_UP}
213 status
=${MSG_DEVICE_STATUS_DOWN}
216 status
=${MSG_DEVICE_STATUS_NOCARRIER}
219 status
=${MSG_DEVICE_STATUS_UNKNOWN}
223 if list_match
"${slave}" ${active_slaves}; then
224 slave_suffix
="(active)"
228 cli_print_fmt1
2 "Slave ${slave}" "${status} ${slave_suffix}"
233 function cli_headline
() {
238 local ident
=$
(cli_ident
${level})
241 printf -v out
"${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
245 function cli_statusline
() {
252 cli_print $
(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
255 function cli_print
() {
260 local ident
=$
(cli_ident $
(( ${level} + 1 )))
263 printf -v out
"${ident}${format}\n" "$@"
267 function cli_print_fmt1
() {
271 local space
=$
(( 34 - (${level} * ${#IDENT}) ))
272 local format
="%-${space}s %s"
274 cli_print
${level} "${format}" "$@"
277 function cli_print_bool
() {
278 if [ "${1}" = "${EXIT_TRUE}" ]; then
285 function cli_print_yesno
() {
286 if [ "${1}" = "${EXIT_TRUE}" ]; then
293 function cli_print_enabled
() {
299 function cli_print_warning
() {
303 cli_print
${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@
"
306 function cli_space() {
310 function cli_ident() {
312 assert isinteger level
315 while [ ${level} -gt 1 ]; do
316 ident="${ident}${IDENT}"
317 level=$(( ${level} - 1 ))
323 function cli_yesno() {
324 local message="$@
[y
/n
] "
328 printf "\n${message}"
332 if listmatch ${yesno} y Y yes YES Yes; then
336 elif listmatch ${yesno} n N no NO No; then
342 function cli_get_key() {
347 function cli_get_val() {
351 function cli_get_bool() {
352 local value="$
(cli_get_val
"$@")"
354 if enabled value; then
363 function cli_usage() {
365 local basename="$
(basename ${0})"
367 if ! isset command; then
368 command="${basename} help"
371 echo "The given
command was not understood by
${basename}.
" >&2
372 echo "Please run
'${command}' for detailed
help.
" >&2
375 function cli_show_man() {
379 if ! binary_exists man; then
380 error "The man package is not installed on this system.
"
381 error "Please
install 'man' in order to view the
help.
"