#!/bin/bash # ------- # File: server-nic-detection # Description: LliureX Config Packages Script to stop and start llxcfg apt scripts # Author: Luis Garcia Gisbert # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # -------- set -e PATH="/usr/sbin:/usr/bin:/sbin:/bin" # lib LIB_FILE="/usr/share/lliurex/lliurex-srv-common/server-netfuncs.sh" [ -e "$LIB_FILE" ] || exit 1 . ${LIB_FILE} # funcs error_msg(){ zenity --error --text "$1" --title "$MSG_TITLE" } ask_msg(){ if zenity --question --text "$1" --title "$MSG_TITLE" ; then return 0 fi return 1 } retry_msg(){ RETRY_MSG="$(gettext "It has not been possible to identify the network interface.\n Would you like to try again the detection process?")" if ask_msg "$RETRY_MSG" ; then return 0 fi return 1 } info_msg(){ zenity --info --text "$1" --title "$MSG_TITLE" } check_down() { for CARD in $@ ; do [ "$(get_link_status $CARD)" != "UP" ] || return 1 done return 0 } unique_up() { NUM_UP=0 LAST_UP="" for CARD in $NETWORK_CARDS ; do if [ "$(get_link_status $CARD)" = "UP" ] ; then NUM_UP=$(($NUM_UP + 1)) LAST_UP="$CARD" fi done if [ $NUM_UP -eq 1 ] ; then echo "$LAST_UP" fi return 0 } wait_for_down(){ EXIT_TIME=$(($SECONDS + $TIME_OUT)) while ! check_down $NETWORK_CARDS && [ $SECONDS -lt $EXIT_TIME ] ; do echo "foo" sleep $SLEEP_TIME done } wait_for_up(){ EXIT_TIME=$(($SECONDS + $TIME_OUT)) while [ -z "$(unique_up)" -a $SECONDS -lt $EXIT_TIME ] ; do echo "foo" sleep $SLEEP_TIME done } and_the_oscar_goes_to(){ info_msg "$1" if [ "$RETURN_VARIABLE_NAME" ] ; then llxcfg-setvars "${RETURN_VARIABLE_NAME}=$THE_CHOSEN_ONE" fi } # main # gettext hacks TEXTDOMAIN="server-nic-detection" export TEXTDOMAIN [ -z "$LANG" ] || LANGUAGE="${LANG}:${LANGUAGE}" # some vars MSG_TITLE="$(gettext "Network interface detection")" MSG_DETECTED="$(gettext "The selected network interface is")" TIME_OUT=20 SLEEP_TIME=1 THE_CHOSEN_ONE="" if [ "$1" ] && echo "$1" |grep -q "^[[:alpha:]][_[:alnum:]]*$" ; then RETURN_VARIABLE_NAME="$1" fi # some tests if [ $(id -u) -ne 0 ] ; then error_msg "$(gettext "Administration level permissions are needed to run this program")" exit 1 fi while true ; do NETWORK_CARDS="$(srv_nic_list)" NUM_CARDS=$(echo "$NETWORK_CARDS" |wc -l) case $NUM_CARDS in 0) error_msg "$(gettext "No network interfaces has been detected")" exit 1 ;; 1) THE_CHOSEN_ONE="$NETWORK_CARDS" MSG_ONLY_ONE="$(gettext ", because this is the unique network interface available")" and_the_oscar_goes_to "$MSG_DETECTED \'$THE_CHOSEN_ONE\'$MSG_ONLY_ONE" exit 0 ;; esac MSG_BEGIN1="$(gettext "The detected network interfaces are:")" MSG_BEGIN2="$(gettext "To begin identification of the correct one,\nyou must CONNECT ONLY the network cable from the INTERNAL CLASSROOM LAN into the desired network card\nof this server and remove network cables from any other interfaces.\n Please verify that the classroom switch is powered\nand both of the cable plugs are fully inserted.")" info_msg "$MSG_BEGIN1\n$NETWORK_CARDS\n\n$MSG_BEGIN2" MSG_INSERT="$(gettext "Identifying network interfaces.\n Please connect ONLY the network cable from the internal classroom")" (wait_for_up | zenity --progress --pulsate --text "$MSG_INSERT" --title "$MSG_TITLE" --auto-close --auto-kill ) THE_CHOSEN_ONE="$(unique_up)" if [ "$THE_CHOSEN_ONE" ] ; then and_the_oscar_goes_to "$MSG_DETECTED \'$THE_CHOSEN_ONE\'" exit 0 else if ! retry_msg ; then exit 1 fi fi done