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_device_headline
() {
40 while [ $# -gt 0 ]; do
50 if zone_exists
${device}; then
52 elif port_exists
${device}; then
61 headline_prefix
="Zone ${device}"
64 headline_prefix
="Port ${device} ($(device_get_type ${device}))"
67 headline_prefix
="Device ${device} ($(device_get_type ${device}))"
71 # Print the hook for all zones.
72 if [ "${type}" = "zone" ]; then
73 headline_prefix
="${headline_prefix} ($(zone_get_hook ${device}))"
75 cli_headline
1 "${headline_prefix}"
77 # Print the device status.
79 case "$(device_get_status ${device})" in
81 status
=${MSG_DEVICE_STATUS_UP}
84 status
=${MSG_DEVICE_STATUS_DOWN}
87 status
=${MSG_DEVICE_STATUS_NOCARRIER}
90 status
=${MSG_DEVICE_STATUS_UNKNOWN}
93 cli_print_fmt1
1 "Status" "${status}"
94 if enabled
${long}; then
95 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
97 if device_is_up
${device}; then
98 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
100 if enabled
${long}; then
101 device_is_promisc
${device} &>/dev
/null
102 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
107 # Print the device stats.
108 device_is_up
${device} && cli_device_stats
2 ${device}
110 if enabled
${long}; then
112 device_is_virtual
${device} && cli_device_virtual
${device}
115 device_is_bonded
${device} && cli_device_bonded
${device}
118 device_is_bonding
${device} && cli_device_bonding
${device}
122 function cli_device_stats
() {
126 # This section will print statistical data from the device.
127 local packets bytes errors
129 cli_headline
${level} "Statistics"
130 local format
="%-10s %9d packets %6s (%d errors)"
133 packets
=$
(device_get_rx_packets
${device})
134 bytes
=$
(device_get_rx_bytes
${device})
135 errors
=$
(device_get_rx_errors
${device})
137 cli_print
${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
140 packets=$(device_get_tx_packets ${device})
141 bytes=$(device_get_tx_bytes ${device})
142 errors=$(device_get_tx_errors ${device})
144 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
148 function cli_device_virtual
() {
151 cli_headline
2 "VLAN"
153 cli_print_fmt1
2 "Parent" "$(virtual_get_parent ${device})"
154 cli_print_fmt1
2 "VID" "$(virtual_get_id ${device})"
158 function cli_device_bonded
() {
161 cli_headline
2 "Bonding information"
163 local master
=$
(bonding_slave_get_master
${port})
164 cli_print_fmt1
2 "Master" "${master}"
167 [ "$(bonding_get_active_slave ${master})" = "${port}" ]
168 cli_print_fmt1
2 "Active slave" "$(cli_print_yesno $?)"
172 function cli_device_bonding
() {
175 cli_headline
2 "Bonding information"
177 cli_print_fmt1
2 "Mode" "$(bonding_get_mode ${port})"
181 local slave slave_prefix
182 local slave_active
=$
(bonding_get_active_slave
${device})
183 for slave
in $
(bonding_get_slaves
${device}); do
184 if [ "${slave_active}" = "${slave}" ]; then
185 slave_prefix
="Slave (active)"
189 cli_print_fmt1
2 "${slave_prefix}" "${slave}"
194 function cli_headline
() {
199 local ident
=$
(cli_ident
${level})
202 printf -v out
"${ident}${COLOUR_BOLD}${format}${COLOUR_NORMAL}\n" "$@"
206 function cli_statusline
() {
213 cli_print $
(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
216 function cli_print
() {
221 local ident
=$
(cli_ident $
(( ${level} + 1 )))
224 printf -v out
"${ident}${format}\n" "$@"
228 function cli_print_fmt1
() {
232 local space
=$
(( 30 - (${level} * 4) ))
233 local format
="%-${space}s %s"
235 cli_print
${level} "${format}" "$@"
238 function cli_print_bool
() {
239 if [ "${1}" = "${EXIT_TRUE}" ]; then
246 function cli_print_yesno
() {
247 if [ "${1}" = "${EXIT_TRUE}" ]; then
254 function cli_print_warning
() {
258 cli_print
${level} "${COLOUR_WARN}%s${COLOUR_NORMAL}" "$@
"
261 function cli_space() {
265 function cli_ident() {
270 while [ ${level} -gt 1 ]; do
272 level=$(( ${level} - 1 ))
278 function cli_yesno() {
279 local message="$@
[y
/n
] "
283 printf "\n${message}"
287 if listmatch ${yesno} y Y yes YES Yes; then
291 elif listmatch ${yesno} n N no NO No; then
297 function cli_get_key() {
302 function cli_get_val() {
306 function cli_usage() {
308 local basename="$
(basename ${0})"
310 if ! isset command; then
311 command="${basename} help"
314 echo "The given
command was not understood by
${basename}.
" >&2
315 echo "Please run
'${command}' for detailed
help.
" >&2
318 function cli_show_man() {
322 if ! binary_exists man; then
323 error "The man package is not installed on this system.
"
324 error "Please
install 'man' in order to view the
help.
"