]> git.ipfire.org Git - people/ms/network.git/blob - functions.cli
579a263fc986202f4bd804924e44d0e3f6cf1522
[people/ms/network.git] / functions.cli
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 function cli_help_requested() {
23 local argument="${1}"
24
25 if [ -n "${argument}" ]; then
26 if listmatch ${argument} help -h --help; then
27 return ${EXIT_OK}
28 fi
29 fi
30
31 return ${EXIT_ERROR}
32 }
33
34 function cli_device_headline() {
35 local device=${1}
36 assert isset device
37
38 local type
39 if zone_exists ${device}; then
40 type="zone"
41 elif port_exists ${device}; then
42 type="port"
43 else
44 type="unknown"
45 fi
46
47 local headline_prefix
48 case "${type}" in
49 zone)
50 headline_prefix="Zone ${device}"
51 ;;
52 port)
53 headline_prefix="Port ${device}"
54 ;;
55 *)
56 headline_prefix="Device ${device}"
57 ;;
58 esac
59
60 cli_headline 1 "${headline_prefix}:"
61
62 # Print the hook for all zones.
63 if [ "${type}" = "zone" ]; then
64 cli_print_fmt1 1 "Hook" "$(zone_get_hook ${device})"
65 fi
66
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})"
72 fi
73
74 cli_space
75
76 # Print the device stats.
77 device_is_up ${device} && cli_device_stats 2 ${device}
78 }
79
80 function cli_device_stats() {
81 local level=${1}
82 local device=${2}
83
84 # This section will print statistical data from the device.
85 local packets bytes errors
86
87 cli_headline ${level} "Statistics"
88 local format="%-10s %9d packets %6s (%d errors)"
89
90 # RX
91 packets=$(device_get_rx_packets ${device})
92 bytes=$(device_get_rx_bytes ${device})
93 errors=$(device_get_rx_errors ${device})
94
95 cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
96
97 # TX
98 packets=$(device_get_tx_packets ${device})
99 bytes=$(device_get_tx_bytes ${device})
100 errors=$(device_get_tx_errors ${device})
101
102 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
103 cli_space
104 }
105
106 function cli_status_headline() {
107 echo "XXX THIS FUNCTION IS DEPRECATED"
108
109 local zone=${1}
110
111 local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
112 zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
113
114 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
115 }
116
117 function cli_headline() {
118 local level=${1}
119 shift
120
121 local message="$@"
122 local ident=$(cli_ident ${level})
123
124 printf "${ident}${COLOUR_BOLD}$@${COLOUR_NORMAL}\n"
125 }
126
127 function cli_print() {
128 local level=${1}
129 local format=${2}
130 shift 2
131
132 local ident=$(cli_ident $(( ${level} + 1 )))
133
134 local out
135 printf -v out "${ident}${format}\n" "$@"
136 printf "${out}"
137 }
138
139 function cli_print_fmt1() {
140 local level=${1}
141 shift
142
143 local space=$(( 30 - (${level} * 4) ))
144 local format="%-${space}s %s"
145
146 cli_print ${level} "${format}" "$@"
147 }
148
149 function cli_print_bool() {
150 if [ "${1}" = "${EXIT_TRUE}" ]; then
151 echo "true"
152 else
153 echo "false"
154 fi
155 }
156
157 function cli_print_yesno() {
158 if [ "${1}" = "${EXIT_TRUE}" ]; then
159 echo "yes"
160 else
161 echo "false"
162 fi
163 }
164
165 function cli_print_warning() {
166 local level=${1}
167 shift
168
169 cli_print ${level} "${COLOUR_WARN}%s${COLOUR_NORMAL}" "$@"
170 }
171
172 function cli_space() {
173 printf "\n"
174 }
175
176 function cli_ident() {
177 local level=${1}
178 shift
179
180 local ident=""
181 while [ ${level} -gt 1 ]; do
182 ident="${ident} "
183 level=$(( ${level} - 1 ))
184 done
185
186 echo "${ident}"
187 }
188
189 function cli_yesno() {
190 local message="$@ [y/n] "
191 local yesno
192
193 while true; do
194 printf "\n${message}"
195 read yesno
196
197 # Check for "yes".
198 if listmatch ${yesno} y Y yes YES Yes; then
199 return ${EXIT_TRUE}
200
201 # Check for "no".
202 elif listmatch ${yesno} n N no NO No; then
203 return ${EXIT_FALSE}
204 fi
205 done
206 }
207
208 function cli_get_key() {
209 local key="${1%%=*}"
210 echo "${key/--/}"
211 }
212
213 function cli_get_val() {
214 echo "${@##*=}"
215 }
216
217 function cli_usage() {
218 local command="$@"
219 local basename="$(basename ${0})"
220
221 if ! isset command; then
222 command="${basename} help"
223 fi
224
225 echo "The given command was not understood by ${basename}." >&2
226 echo "Please run '${command}' for detailed help." >&2
227 }
228
229 function cli_show_man() {
230 local manpage=${1}
231 assert isset manpage
232
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."
236 exit ${EXIT_ERROR}
237 fi
238
239 man ${manpage}
240 }