Dies ist eine alte Version des Dokuments!
An adjustable configuration switcher.
This script can be used to switch between several different preconfigured settings. Use it for example to quickly adjust the network settings of your notebook for office or home use.
add more info!
We want to use the script to switch between office and home network settings. On Slackware for example you could also use netconfig
for this task (or Yast on Suse) but this is too cumbersome for daily usage. Fortunately on linux under the hood all configuration settings are read from text files and netconfig
does nothing more than to edit some of them. sbreconfig
does the same, but just copies the content from template files. We'll stick with netconfig
in this example, but any other config tool on any other linux distro could also be used.
/usr/local/sbin/sbreconfig
and make it runnablenetconfig
to configure your network for office use.cp -a /etc /etc.bak
netconfig
again to configure your network for home usage/etc/rc.d/rc.inet1.conf /etc/HOSTNAME /etc/hosts /etc/networks /etc/resolv.conf
sbreconfig
will copy to the real config files later:cp /etc/rc.d/rc.inet1.conf /etc/rc.d/rc.inet1.conf.home cp /etc/HOSTNAME /etc/HOSTNAME.home cp /etc/hosts /etc/hosts.home cp /etc/networks /etc/networks.home cp /etc/resolv.conf /etc/resolv.conf.home cp /etc.bak/rc.d/rc.inet1.conf /etc/rc.d/rc.inet1.conf.office cp /etc.bak/HOSTNAME /etc/HOSTNAME.office cp /etc.bak/hosts /etc/hosts.office cp /etc.bak/networks /etc/networks.office cp /etc.bak/resolv.conf /etc/resolv.conf.office
Note: It is also possible to create default template config files. They have the extension .dflt
. They will be read if the specific template config file is not found. Thus the same config file can be used for different configurations. For the hostname the default template config file would be/etc/HOSTNAME.dflt
. But this feature is only useful for more than 2 different configurations.
/etc.bak
/etc/sbreconfig
/etc/sbreconfig/network
(any other name possible) and fill it with the following content:#!/bin/bash # /etc/sbreconfig/network # List of allowed configurations. This corresponds to the extensions of the # config template files conflist='home office' # List of configuration files to be manipulated filelist='/etc/wpa_supplicant.conf /etc/rc.d/rc.inet1.conf /etc/HOSTNAME /etc/hosts /etc/networks /etc/resolv.conf' # optional: What to do before copying the files: dobefore='' # optional: What to do after copying the files: doafter='/etc/rc.d/rc.inet1 restart' # restart the network to apply the changes
From now on you can switch to office network settings just by issuing the command sbreconfig network office
and back to home network settings by sbreconfig network home
.
sbreconfig does this by copying the template files to their real counterparts specified in the filelist variable. In this expample cp /etc/rc.d/rc.inet1.conf.home /etc/rc.d/rc.inet1.conf; cp /etc/HOSTNAME.home /etc/HOSTNAME; …
If you need a 3rd network setting later, just create a bunch of config files with a proper extension: /etc/rc.d/rc.inet1.conf.melanie /etc/HOSTNAME.melanie …
#!/bin/bash # A adjustable configuration switcher # This script can be used to switch between several different # preconfigured settings. # Use it for example to quickly adjust the network settings of your notebook # for office or home use. # More info at http://wiki.think-deep.com/becki:sources:sbreconfig function usage() { if [ "$2" ]; then echo -e "ERROR: $2!"; fi echo "Usage: $(basename $0) filelistfile configname" echo "Look at http://wiki.think-deep.com/becki:sources:sbreconfig for more info" exit $1 } instructions=$1 configuration=$2 # Check if instructions file is ok: if [ ! "$instructions" ]; then usage 1 "No filelist file specified" fi instructions="/etc/sbreconfig/$instructions" if [ ! -r "$instructions" ]; then usage 2 "Cant read \"$instructions\"" fi . $instructions if [ ! "$filelist" ]; then usage 3 "Filelist in filelistfile missing" fi # Check if configuration identifier is given in the command line: if [ ! "$configuration" ]; then #echo "filelist: $filelist" usage 4 "Configuration name is missing" fi # Check if given configuration identifier is allowed: err=1 for conf in $conflist; do if [ "$conf" == "$configuration" ]; then err=0 break fi done if (( $err )); then usage 6 "Configuration \"$configuration\" is not allowed.\nAllowed configurations are: \"$conflist\"" fi # Check if all configuration file templates are present: err=0 for cfile in $filelist; do if [ ! -r "$cfile.$configuration" ]; then if [ -r "$cfile.dflt" ]; then echo "HINT: $cfile.$configuration not found, using $cfile.dflt" else echo "ERROR: Neighter $cfile.$configuration nor $cfile.dflt found!" err=1 fi fi done if (( $err )); then usage 5; fi # Do the work: eval $dobefore for cfile in $filelist ; do if [ -r "$cfile.$configuration" ]; then cp $cfile.$configuration $cfile else cp $cfile.dflt $cfile fi done eval $doafter