[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: mehrere IP-Adressen an eth0 verwenden



On Tue, 28 Oct 2003 15:16:30 +0100, Michael Ott wrote:

> Hallo Andre!
> 
>> Ich mache folgendes (funktioniert auch bei vielen Konfigurationen):
>> Ich habe mir ein Skript geschrieben, was bereits im von rc.sysinit noch
>> vor dem Setzen des Hostnamens (und dem Starten des Netzwerks) ausgeführt
>> wird. In diesem frage ich nach, welche Netzwerkkonfiguration geladen
>> werden soll, und ändere in Abhängigkeit von der Auswahl entsprechende
>> Dateien (etc/hostname, /etc/resolv.conf und /etc/network/interfaces).
>> Das System liest dann beim weiteren Starten die Dateien und startet das
>> Netzwerk ganz normal mit den Einstellungen aus /etc/network/interfaces.
>> 
>> Bei Interesse kann ich das Skript mal posten.
> Ich habe Interesse 
>

OK, dann hier also das Skript (/etc/init.d/netselect). Es war so
ziemlich mein erstes Skript, daher vielleicht manchmal etwas
umständlich. Ich kommentiere immer mal etwas zusätzlich... (#COMMENT)

----------------------------- Start /etc/init.d/netselect ----------------
 #!/bin/bash
# Change network profiles at startup
# @author: Andre Kalus [akalus@gmx.de] # @date: 2003-03-28

#COMMENT: in PROFILE_DIR liegen die Dateien, die die Netzwerkprofile
# enthalten, die zwei weiteren Dirs sind Debian-Standard, dann die Anzahl
# der allgemeinen (z.B. Hostname) und der interface-Spezifischen (bei
# mehreren Netzwerkkarten) Einträge (z.B. IP-Adresse)

PROFILE_DIR=/etc/net.profiles
NETWORK_CONF_DIR=/etc/sysconfig
INIT_DIR=/etc/init.d
ENTRIES_GENERAL=5
ENTRIES_PER_IF=7

#COMMENT: Die nächsten drei sind Hilfesfunktionen, die im Skript
# verwendet werden

#####################################################
# getValue: $DATA contains lines containing
# var=value; this function assigns value to $VALUE
#####################################################
getValue() {
RELLINE=`echo "$DATA" | grep -i "$1="`
VALUE=`echo "$RELLINE" | sed 's/[[:print:]]*=//'`
}

#####################################################
# printValue: Writes pairs of var and value to
# $ACT_FIle. A separator between var and value can
# be specified as optional third argument; if not
# specified, it's empty
#####################################################
printValue() {
getValue "$1"
if [ -n "$VALUE" ]; then
echo "$2$3$VALUE" >> $ACT_FILE
fi
}

#####################################################
# getPromptValue: read /proc/cmdline and write the
# value of netprofile into the var PROMPTVALUE
#####################################################
getPromptValue() {

 flag="netprofile"
 i=1
 expr=`cat /proc/cmdline | awk '{print $'"${i}"'}'`
 while [ -n "$expr" ]
 do
  if [ `echo $expr | grep $flag` ]
  then
   PROMPTVALUE=`echo $expr | sed "s/$flag=//"`
  fi

  let "i += 1"
  expr=`cat /proc/cmdline | awk '{print $'"${i}"'}'`
 done
}


#####################################################
# Begin script: Get profile name set as kernel option.
# If none, ask for profile, then set symlink to
# selected profile and write its values to default
# files.
#####################################################

#COMMENT: Prüfe, ob Netzwerk-Konfiguration in der Boot-Zeile bereits
# angegeben (für häufig benutzte habe ich einen eigenen Eintrag im
# Grub-Menü

cd "$PROFILE_DIR"
getPromptValue
if [ -z "$PROMPTVALUE" ]; then

#COMMENT: Wenn kein Profil in boot-Zeile angegeben, zeige Auswahl

# Print selection menue: get available profiles
echo -e "\e[32m-------- Network selection ---------\e[0m"

#COMMENT: Alle Profil-Definitionen fangen mit net an (z.B. net.Home);
# angezeigt wird nur der Teil hinter net (z.B. Home)
profiles=(`ls net.* | sed 's/net.//'`)

count=1
num_profiles=${#profiles[@]}

while [ "$count" -le "$num_profiles" ]; do
 profile=${profiles[$count-1]}
 echo -n "$count: "
 echo "$profile" | sed 's/_/ /g'
 let "count += 1"
done

echo -e "\e[32m-- e for exit (keep old settings) --"
echo -e "------------------------------------\e[0m"

# Read until valid input
read -s -n1 sel_num
while [ "$sel_num" \< 1 -o "$sel_num" \> "$num_profiles" ]; do
 if [ "$sel_num" = "e" ]; then
  echo "Keeping old settings"
  exit 0
 fi
 echo "Wrong input"
 read -s -n1 sel_num
done

echo
NEW_PROFILE=`echo "${profiles[$sel_num-1]}" | sed 's/ /_/g'`

else
NEW_PROFILE="$PROMPTVALUE"
fi

echo "Using profile $NEW_PROFILE"
PROFILE_FILE=`echo net.$NEW_PROFILE`

# Write general data
DATA=`head -n $ENTRIES_GENERAL $PROFILE_FILE`

ACT_FILE=/etc/hostname
rm -f $ACT_FILE
printValue "HOSTNAME" "" ""

ACT_FILE=/etc/resolv.conf
rm -f $ACT_FILE
printValue "NAMESERVER" "nameserver" " "
printValue "DOMAIN" "domain" " "

# Write interface-specific data per interface
IFCOUNT=`grep --count IF_NAME $PROFILE_FILE`
IFLIST=(`grep IF_NAME $PROFILE_FILE`)
i=0
ACT_FILE=/etc/network/interfaces
echo "auto lo
iface lo inet loopback" > $ACT_FILE
while [ $i -lt $IFCOUNT ]; do
echo "" >>$ACT_FILE
DATA=`grep -A $ENTRIES_PER_IF ${IFLIST[i]} $PROFILE_FILE`
getValue "IF_ONBOOT"
if [ "$VALUE" = "yes" ]; then
 printValue "IF_NAME" "auto" " "
fi
getValue "IF_NAME"
printValue "IF_TYPE" "iface $VALUE inet" " "
printValue "IF_IP" "	address" " "
printValue "IF_NETMASK" "	netmask" " "
printValue "IF_NETWORK" "	network" " "
printValue "IF_BROADCAST" "	broadcast" " "
getValue "IF_TYPE"
if [ "$VALUE" != "dhcp" ]; then
DATA=`head -n $ENTRIES_GENERAL $PROFILE_FILE`
printValue "GATEWAY" "	gateway" " "
fi
let 'i=i+1'
done

#COMMENT: Hier können Profil-spezifische Befehle bei Ende des Bootens
# angegeben werden (z.B. mount-Befehle für Netzlaufwerke): Falls für das
# gewählte Profil eine startlocal-Datei (z.B. startlocal.Home) existiert,
# wird /etc/init.d/startlocal mit dieser verlinkt. Wenn keine
# startlocal-Datei existiert, wird /etc/init.d/startlocal als leere Datei
# erstellt


# Profile-dependant startlocal if it exists
rm $INIT_DIR/startlocal
if [ -e $PROFILE_DIR/startlocal.$NEW_PROFILE ]; then
 ln -s $PROFILE_DIR/startlocal.$NEW_PROFILE $INIT_DIR/startlocal
else
 touch $INIT_DIR/startlocal
 chmod 755 $INIT_DIR/startlocal
fi

----------------------------- End /etc/init.d/netselect ----------------


So, eine Konfigurationsdatei sieht dann wie folgt aus:

----------------------------- Start /etc/net.profiles/net.Home -----------
HOSTNAME=mobil
DOMAIN=akalus.test
NAMESERVER=192.168.0.254
GATEWAY=192.168.0.254
GATEWAY_IF=eth0

### Interfaces ###

IF_NAME=eth0
IF_ONBOOT=yes
IF_TYPE=static
IF_IP=192.168.0.1
IF_NETMASK=255.255.255.0
IF_NETWORK=192.168.0.0
IF_BROADCAST=192.168.0.255
----------------------------- End /etc/net.profiles/net.Home -----------

Das geht auch mit zwei Netzwerkinterface (LAN und WLAN). Eth1 ist eine
PCMCIA-Karte, deswegen ONBOOOT=no

----------------------------- Start /etc/net.profiles/net.Uni -----------

OSTNAME=mobil
DOMAIN=andre.de
NAMESERVER=
GATEWAY=
GATEWAY_IF=eth0

### Interfaces ###

IF_NAME=eth0
IF_ONBOOT=yes
IF_TYPE=static
IF_IP=192.168.10.4
IF_NETMASK=255.255.255.0
IF_NETWORK=192.168.10.0
IF_BROADCAST=192.168.10.255

IF_NAME=eth1
IF_ONBOOT=no
IF_TYPE=dhcp
IF_IP=
IF_NETMASK=
IF_NETWORK=
IF_BROADCAST=

----------------------------- End /etc/net.profiles/net.Uni -----------

Startlocal-Dateien enthalten normale Bash-Befehle z.B. 

mount -t nfs //server/home /home

Hinweis: Nicht zu jedem Profi muss eine startlocal-Datei existieren

Damit das läuft, muss in /etc/rcS.d ein Link S36netselect angelegt
werden, der auf ../init.d/netselect zeigt.
Für startlocal braucht man in /etc/rc2.d einen Link S95startlocal auf
../init.d/startlocal.


Das war jetzt relativ viel. Ich stelle das Skript mal auf meine Homepage
(http://home.pages.at/andre-kalus)

Viele Grüße
Andre



Reply to: