]>
git.ipfire.org Git - people/ms/network.git/blob - functions.cli
579a263fc986202f4bd804924e44d0e3f6cf1522
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
() {
39 if zone_exists
${device}; then
41 elif port_exists
${device}; then
50 headline_prefix
="Zone ${device}"
53 headline_prefix
="Port ${device}"
56 headline_prefix
="Device ${device}"
60 cli_headline
1 "${headline_prefix}:"
62 # Print the hook for all zones.
63 if [ "${type}" = "zone" ]; then
64 cli_print_fmt1
1 "Hook" "$(zone_get_hook ${device})"
67 # Print the device status.
68 local status
=$
(device_get_status
${device})
69 cli_print_fmt1
1 "Status" "${STATUS_COLOUR[${status}]}${STATUS_TEXT[${status}]}${COLOUR_NORMAL}"
70 if device_is_up
${device}; then
71 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
76 # Print the device stats.
77 device_is_up
${device} && cli_device_stats
2 ${device}
80 function cli_device_stats
() {
84 # This section will print statistical data from the device.
85 local packets bytes errors
87 cli_headline
${level} "Statistics"
88 local format
="%-10s %9d packets %6s (%d errors)"
91 packets
=$
(device_get_rx_packets
${device})
92 bytes
=$
(device_get_rx_bytes
${device})
93 errors
=$
(device_get_rx_errors
${device})
95 cli_print
${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
98 packets=$(device_get_tx_packets ${device})
99 bytes=$(device_get_tx_bytes ${device})
100 errors=$(device_get_tx_errors ${device})
102 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
106 function cli_status_headline
() {
107 echo "XXX THIS FUNCTION IS DEPRECATED"
111 local state
="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
112 zone_is_up
${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
114 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
117 function cli_headline() {
122 local ident=$(cli_ident ${level})
124 printf "${ident}${COLOUR_BOLD}$@${COLOUR_NORMAL}\n"
127 function cli_print() {
132 local ident=$(cli_ident $(( ${level} + 1 )))
135 printf -v out "${ident}${format}\n" "$@
"
139 function cli_print_fmt1() {
143 local space=$(( 30 - (${level} * 4) ))
144 local format="%-${space}s
%s
"
146 cli_print ${level} "${format}" "$@
"
149 function cli_print_bool() {
150 if [ "${1}" = "${EXIT_TRUE}" ]; then
157 function cli_print_yesno() {
158 if [ "${1}" = "${EXIT_TRUE}" ]; then
165 function cli_print_warning() {
169 cli_print ${level} "${COLOUR_WARN}%s${COLOUR_NORMAL}" "$@"
172 function cli_space
() {
176 function cli_ident
() {
181 while [ ${level} -gt 1 ]; do
183 level
=$
(( ${level} - 1 ))
189 function cli_yesno
() {
190 local message
="$@ [y/n] "
194 printf "\n${message}"
198 if listmatch
${yesno} y Y
yes YES Yes
; then
202 elif listmatch
${yesno} n N no NO No
; then
208 function cli_get_key
() {
213 function cli_get_val
() {
217 function cli_usage
() {
219 local basename="$(basename ${0})"
221 if ! isset
command; then
222 command="${basename} help"
225 echo "The given command was not understood by ${basename}." >&2
226 echo "Please run '${command}' for detailed help." >&2
229 function cli_show_man
() {
233 if ! binary_exists man
; then
234 error
"The man package is not installed on this system."
235 error
"Please install 'man' in order to view the help."