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 ###############################################################################
22 function cli_help_requested
() {
25 if [ -n "${argument}" ]; then
26 if listmatch
${argument} help -h --help; then
34 function cli_run_help
() {
37 print
"Run \"${command} help\" to get more information."
41 function cli_device_headline
() {
47 while [ $# -gt 0 ]; do
57 if zone_exists
${device}; then
59 elif port_exists
${device}; then
68 headline_prefix
="Zone ${device}"
71 headline_prefix
="Port ${device} ($(device_get_type ${device}))"
74 headline_prefix
="Device ${device} ($(device_get_type ${device}))"
78 # Print the hook for all zones.
79 if [ "${type}" = "zone" ]; then
80 headline_prefix
="${headline_prefix} ($(zone_get_hook ${device}))"
82 cli_headline
1 "${headline_prefix}"
84 # Print the device status.
86 case "$(device_get_status ${device})" in
88 status
=${MSG_DEVICE_STATUS_UP}
91 status
=${MSG_DEVICE_STATUS_DOWN}
94 status
=${MSG_DEVICE_STATUS_NOCARRIER}
97 status
=${MSG_DEVICE_STATUS_UNKNOWN}
100 cli_print_fmt1
1 "Status" "${status}"
101 if enabled long
; then
102 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
104 if device_is_up
${device}; then
105 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
107 if enabled long
; then
108 device_is_promisc
${device} &>/dev
/null
109 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
114 # Print the device stats.
115 device_is_up
${device} && cli_device_stats
2 ${device}
117 if enabled long
; then
119 device_is_virtual
${device} && cli_device_virtual
${device}
122 device_is_bonded
${device} && cli_device_bonded
${device}
125 device_is_bonding
${device} && cli_device_bonding
${device}
129 function cli_device_stats
() {
133 # This section will print statistical data from the device.
134 local packets bytes errors
136 cli_headline
${level} "Statistics"
137 local format
="%-10s %9d packets %6s (%d errors)"
140 packets
=$
(device_get_rx_packets
${device})
141 bytes
=$
(device_get_rx_bytes
${device})
142 errors
=$
(device_get_rx_errors
${device})
144 cli_print
${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
147 packets=$(device_get_tx_packets ${device})
148 bytes=$(device_get_tx_bytes ${device})
149 errors=$(device_get_tx_errors ${device})
151 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
155 function cli_device_virtual
() {
158 cli_headline
2 "VLAN"
160 cli_print_fmt1
2 "Parent" "$(virtual_get_parent ${device})"
161 cli_print_fmt1
2 "VID" "$(virtual_get_id ${device})"
165 function cli_device_bonded
() {
168 cli_headline
2 "Bonding information"
170 local master
=$
(bonding_slave_get_master
${port})
171 cli_print_fmt1
2 "Master" "${master}"
174 [ "$(bonding_get_active_slave ${master})" = "${port}" ]
175 cli_print_fmt1
2 "Active slave" "$(cli_print_yesno $?)"
179 function cli_device_bonding
() {
182 cli_headline
2 "Bonding information"
184 cli_print_fmt1
2 "Mode" "$(bonding_get_mode ${port})"
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)"
196 cli_print_fmt1
2 "${slave_prefix}" "${slave}"
201 function cli_headline
() {
206 local ident
=$
(cli_ident
${level})
209 printf -v out
"${ident}${COLOUR_BOLD}${format}${COLOUR_NORMAL}\n" "$@"
213 function cli_statusline
() {
220 cli_print $
(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
223 function cli_print
() {
228 local ident
=$
(cli_ident $
(( ${level} + 1 )))
231 printf -v out
"${ident}${format}\n" "$@"
235 function cli_print_fmt1
() {
239 local space
=$
(( 30 - (${level} * 4) ))
240 local format
="%-${space}s %s"
242 cli_print
${level} "${format}" "$@"
245 function cli_print_bool
() {
246 if [ "${1}" = "${EXIT_TRUE}" ]; then
253 function cli_print_yesno
() {
254 if [ "${1}" = "${EXIT_TRUE}" ]; then
261 function cli_print_warning
() {
265 cli_print
${level} "${COLOUR_WARN}%s${COLOUR_NORMAL}" "$@
"
268 function cli_space() {
272 function cli_ident() {
277 while [ ${level} -gt 1 ]; do
279 level=$(( ${level} - 1 ))
285 function cli_yesno() {
286 local message="$@
[y
/n
] "
290 printf "\n${message}"
294 if listmatch ${yesno} y Y yes YES Yes; then
298 elif listmatch ${yesno} n N no NO No; then
304 function cli_get_key() {
309 function cli_get_val() {
313 function cli_usage() {
315 local basename="$
(basename ${0})"
317 if ! isset command; then
318 command="${basename} help"
321 echo "The given
command was not understood by
${basename}.
" >&2
322 echo "Please run
'${command}' for detailed
help.
" >&2
325 function cli_show_man() {
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.
"