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 # Parse the command line
23 while [ $# -gt 0 ]; do
33 [ -n "${action}" ] && break
36 .
/usr
/lib
/network
/functions
38 function cli_config
() {
39 if cli_help_requested $@
; then
40 cli_show_man network-config
44 if [ -n "${1}" ]; then
52 function cli_device
() {
57 if ! isset device
; then
58 cli_show_man network-device
62 assert device_exists
${device}
66 cli_device_discover
${device} $@
69 cli_device_status
${device}
72 cli_show_man network-device
79 function cli_device_status
() {
81 assert device_exists
${device}
83 # Save the type of the device for later.
84 local type=$
(device_get_type
${device})
86 cli_headline
1 "Device status: ${device}"
87 cli_print_fmt1
1 "Name" "${device}"
89 # Print the device status.
90 device_is_up
${device} &>/dev
/null
95 status
="${COLOUR_GREEN}UP${COLOUR_NORMAL}"
98 status
="${COLOUR_RED}DOWN${COLOUR_NORMAL}"
102 cli_print_fmt1
1 "Status" "${status}"
103 cli_print_fmt1
1 "Type" "${type}"
104 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
107 # Print the link speed for ethernet devices.
110 cli_print_fmt1
1 "Link" \
111 "$(device_get_speed ${device}) MBit/s $(device_get_duplex ${device}) duplex"
115 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
118 # This section will print statistical data from the device.
119 local packets bytes errors
121 cli_headline
2 "Statistics"
122 local format
="%-10s %9d packets %6s (%d errors)"
125 packets
=$
(device_get_rx_packets
${device})
126 bytes
=$
(device_get_rx_bytes
${device})
127 errors
=$
(device_get_rx_errors
${device})
129 cli_print
2 "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
132 packets
=$
(device_get_tx_packets
${device})
133 bytes
=$
(device_get_tx_bytes
${device})
134 errors
=$
(device_get_tx_errors
${device})
136 cli_print
2 "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
139 # Print some more information.
140 device_has_carrier
${device} &>/dev
/null
141 cli_print_fmt1
1 "Has carrier?" "$(cli_print_bool $?)"
143 device_is_promisc
${device} &>/dev
/null
144 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
147 # Print all virtual devices.
148 local virtuals
=$
(device_get_virtuals
${device})
149 if [ -n "${virtuals}" ]; then
150 cli_headline
2 "Virtual devices"
153 for virtual
in ${virtuals}; do
154 cli_print
2 "* %-6s - %s" "${virtual}" "$(device_get_address ${virtual})"
161 function cli_device_discover
() {
165 local device_type
=$
(device_get_type
${device})
166 if [ "${device_type}" != "real" ]; then
172 while [ $# -gt 0 ]; do
182 device_is_up
${device} && up
=1
183 device_set_up
${device}
185 enabled raw ||
echo "${device}"
190 for hook
in $
(hook_zone_get_all
); do
191 out
=$
(hook_zone_exec
${hook} discover
${device})
194 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
202 echo "${hook}: ${line}"
207 echo "${hook}: FAILED"
213 echo " ${hook} was successful."
221 echo " ${hook} failed."
229 [ "${up}" = "1" ] || device_set_down
${device}
232 function cli_hostname
() {
233 if cli_help_requested $@
; then
240 if [ -n "${hostname}" ]; then
241 config_hostname
${hostname}
242 log INFO
"Hostname was set to '${hostname}'."
243 log INFO
"Changes do only take affect after reboot."
247 echo "$(config_hostname)"
251 function cli_port
() {
252 if cli_help_requested $@
; then
253 cli_show_man network-port
260 if port_exists
${1}; then
280 port_
${action} ${port} $@
283 error
"Unrecognized argument: ${action}"
296 error
"Unrecognized argument: ${action}"
303 function cli_zone
() {
304 if cli_help_requested $@
; then
305 cli_show_man network-zone
312 if zone_name_is_valid
${1}; then
331 config|down|edit|port|status|up
)
332 zone_
${action} ${zone} $@
335 error
"Unrecognized argument: ${action}"
336 cli_show_man network-zone
352 cli_list_hooks zone $@
355 if [ -n "${action}" ]; then
356 error
"Unrecognized argument: '${action}'"
360 cli_show_man network-zone
367 # Removes a zone either immediately, if it is currently down,
368 # or adds a tag that the removal will be done when the zone
369 # is brought down the next time.
370 function cli_zone_remove
() {
371 if cli_help_requested $@
; then
372 cli_show_man network-zone
377 assert zone_exists
${zone}
379 if zone_is_up
${zone}; then
380 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
383 echo "Removing zone '${zone}' now..."
384 zone_remove_now
${zone}
390 function cli_list_hooks
() {
394 if cli_help_requested $@
; then
395 cli_show_man network-zone
399 local hook_dir
=$
(hook_dir
${type})
402 for hook
in ${hook_dir}/*; do
403 hook
=$
(basename ${hook})
404 if hook_exists
${type} ${hook}; then
410 function cli_start
() {
411 if cli_help_requested $@
; then
416 local zones
=$
(zones_get $@
)
419 for zone
in ${zones}; do
423 wait # until everything is settled
426 function cli_stop
() {
427 if cli_help_requested $@
; then
432 local zones
=$
(zones_get $@
)
435 for zone
in ${zones}; do
439 wait # until everything is settled
442 function cli_restart
() {
443 if cli_help_requested $@
; then
450 # Give the system some time to calm down
451 sleep ${TIMEOUT_RESTART}
456 function cli_status
() {
457 if cli_help_requested $@
; then
462 # When dumping status information, the debug
463 # mode clutters the console which is not what we want.
464 # Logging on the console is disabled for a short time.
465 local log_disable_stdout
=${LOG_DISABLE_STDOUT}
466 LOG_DISABLE_STDOUT
="true"
468 local zones
=$
(zones_get $@
)
471 for zone
in ${zones}; do
476 LOG_DISABLE_STDOUT
=${log_disable_stdout}
479 function cli_reset
() {
480 if cli_help_requested $@
; then
485 warning_log
"Will reset the whole network configuration!!!"
487 # Force mode is disabled by default
490 while [ $# -gt 0 ]; do
499 # If we are not running in force mode, we ask the user if he does know
501 if ! enabled force
; then
502 if ! cli_yesno
"Do you really want to reset the whole network configuration?"; then
508 for zone
in $
(zones_get
--all); do
513 for port
in $
(ports_get
--all); do
517 # Re-run the initialization functions
523 # Process the given action
529 config|hostname|port|device|zone|start|stop|restart|status|
reset)
539 error
"Invalid command given: ${action}"
540 cli_usage
"network help"
541 exit ${EXIT_CONF_ERROR}