Skip to Content.
Sympa Menu

sm-discuss - Re: [SM-Discuss] netconf.sh - a network configration tool

sm-discuss AT lists.ibiblio.org

Subject: Public SourceMage Discussion List

List archive

Chronological Thread  
  • From: Casey Harkins <charkins AT upl.cs.wisc.edu>
  • To: nealbirch <nealbirch AT attbi.com>
  • Cc: sm-discuss AT lists.ibiblio.org
  • Subject: Re: [SM-Discuss] netconf.sh - a network configration tool
  • Date: Mon, 28 Oct 2002 12:38:32 -0600 (CST)


Ahh, one of those days. This time I've attached the new version, not the
old one.

-casey

On Mon, 28 Oct 2002, Casey Harkins wrote:

>
> Another new version, this time it uses the <devicename>.dev filename
> scheme all the time, not just when it feels up to it. ;-)
>
> -casey
>
>
#!/bin/bash
#
# netconf.sh - a bash/dialog based network configuration tool
#
# Copyright (c) 2002 Casey Harkins <charkins AT pobox.com>
#
# This 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 software 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 software; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# ----------------------- Testing Tips ------------------------------ #
# For testing purposes, it may be useful to set ROOTCHECK=0 to allow
# the script to run as any user. NETDEVDIR should then be set to some
# testing directory where the user has appropriate permissions. This
# will prevent the script from modifying your *real* network settings.

# ROOTCHECK - if set to 1, check if UID is 0, if not su to root first
ROOTCHECK=1

# NETDEVDIR - specifies the location for network device files
NETDEVDIR=/etc/sysconfig/network

# button labels
BTN_ACCEPT='Accept'
BTN_SELECT='Select'
BTN_CANCEL='Cancel'
BTN_EXIT='Exit'
BTN_BACK='Back'

# BACKTITLE - shown at the top of the screen
BACKTITLE='SourceMage Network Configuration Tool - version 0.3'

# DIALOG - dialog program used, could be taken from /etc/sorcery/config
DIALOG=dialog

# zeroreturn()
#
# Returns zero. Used before 'while [ $? == 0 ]' to ensure the condition
# is true the first time through the loop. This essentialy makes up for
# no do-while type of looping structure in Bash.
#
# POST-CONDITION:
# returns 0
#
zeroreturn() {
return 0;
}

# getnetmods()
#
# Parses output of 'modprobe -l |grep net' and
# constructs two arrays of available network modules.
#
# POST-CONDITIONS:
# NETMODS - array of network device files
# NETMODSMENU - array of "id" "network device file" pairs
#
getnetmods() {
unset NETMODSMENU NETMODS
id=0
for i in `modprobe -l |grep net`; do
modname=`basename $i .o`
NETMODSMENU[$id]="${id} ${modname}"
NETMODS[$id]="${modname}"
id=$((id+1))
done
unset id
GETNETMODS=1
}

# getnetdevs()
#
# Checks NETDEVDIR for network device files and
# constructs two arrays of these files.
#
# POST-CONDITIONS:
# NETDEVS - array of network device files
# NETDEVSMENU - array of "id" "network device file" pairs
#
getnetdevs() {
unset NETDEVSMENU NETDEVS
id=0
for i in `ls $NETDEVDIR | grep dev$ | cut -d. -f1`; do
NETDEVSMENU[$id]="${id} `basename $i .dev`"
NETDEVS[$id]="${NETDEVDIR}/${i}"
id=$((id+1))
done
unset id
}

# loadnetdev()
#
# Unsets network device variables then loads the
# network device file.
#
# PARAMETERS: network device file
#
# POST-CONDITIONS:
# MODULE - kernel module to load, or blank for static driver
# MODE - 'dynamic' for dhcp, or 'static' for static
# IP - IP address
# BROADCAST - broadcast address
# NETMASK - netmask
# GATEWAY - gateway address
#
loadnetdev() {
if [ -z "$1" ]; then
echo "Error: loadnetdev() called with no parameter"
exit 1;
fi
unset MODULE MODE IP BROADCAST NETMASK GATEWAY
if [ -r $1 ]; then
source $1
fi
}

# cfg_module_enter()
#
# Prompts user to enter the kernel module name (without the .o)
#
# POST-CONDITIONS:
# MODULE - kernel module to load
#
cfg_module_enter() {
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Configure MODULE"
\
--ok-label "$BTN_ACCEPT"
\
--cancel-label "$BTN_CANCEL"
\
--inputbox
\
"Enter the name of the kernel module which your
\
device uses, without the trailing .o"
\
0 0`

if [ $? == 0 ]; then
MODULE=$COMMAND
fi
return 0
}

# cfg_module_choose()
#
# Prompts user to select the kernel module from a list
# of available network drivers (uses modprobe -l | grep net)
#
# POST-CONDITIONS:
# MODULE - kernel module to load
#
cfg_module_choose() {
if [ -z "$GETNETMODS" ]; then
getnetmods
fi

COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Configure MODULE"
\
--ok-label "$BTN_ACCEPT"
\
--cancel-label "$BTN_CANCEL"
\
--menu
\
"Select the network module from the following list,
\
or choose 'Enter' if the module is not shown."
\
0 0 0
\
${NETMODSMENU[*]}
\
"Enter" "Enter a module name"`


if [ $? != 0 ]; then
return 0
fi

case $COMMAND in
'Enter') cfg_module_enter ;;
*) MODULE=${NETMODS[${COMMAND}]} ;;
esac
return 0
}

# cfg_module()
#
# Prompts user for MODULE setting. Three options are presented:
# 1) Enter module name
# 2) Select from list (modprobe -l |grep net)
# 3) None (use static driver)
#
# POST-CONDITIONS:
# MODULE - kernel module to load, or blank for static driver
#
cfg_module() {
if [ -z "$MODULE" ]; then
dmodule="None (use static driver)"
else
dmodule=$MODULE
fi

COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Configure MODULE"
\
--ok-label "$BTN_SELECT"
\
--cancel-label "$BTN_BACK"
\
--menu
\
"Choose how to select the kernel module,
\
select 'None' if the driver is compiled into
\
the kernel, or select '$BTN_BACK' to keep the
\
current setting (${dmodule})"
\
0 0 0
\
"Enter" "Enter a module name"
\
"Choose" "Choose from a list of available network
modules"\
"None" "Use a static driver"`


if [ $? != 0 ]; then
return 0
fi

case $COMMAND in
'Enter') cfg_module_enter ;;
'Choose') cfg_module_choose ;;
'None') MODULE='' ;;
esac
return 0
}

# cfg_mode()
#
# Prompts user for MODE setting. Two options are presented:
# 1) dynamic (use dhcp)
# 2) static
#
# POST-CONDITIONS:
# MODE - 'dynamic' for dhcp, or 'static' for static
#
cfg_mode() {
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Configure MODE"
\
--ok-label "$BTN_SELECT"
\
--cancel-label "$BTN_BACK"
\
--menu
\
"Choose which mode this network device uses.
\
Select 'dynamic' to use dhcp, select 'static'
\
to specify IP, BROADCAST, NETMASK, GATEWAY, or
\
select '${BTN_BACK}' to keep current setting
\
(${MODE})"
\
0 0 0
\
"dynamic" "use dhcp"
\
"static" "specify network settings"`

if [ $? != 0 ]; then
return 0
fi

case $COMMAND in
'dynamic') MODE='dynamic' ;;
'static') MODE='static' ;;
esac

return 0
}

# cfg_ip()
#
# Prompts user to enter the ip address.
#
# POST-CONDITIONS:
# IP - IP address
#
cfg_ip() {
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Configure IP address"
\
--ok-label "$BTN_ACCEPT"
\
--cancel-label "$BTN_CANCEL"
\
--inputbox
\
"Enter the IP address for this network device"
\
0 0`

if [ $? == 0 ]; then
IP=$COMMAND
fi
return 0
}


# cfg_broadcast()
#
# Prompts user to enter the broadcast address.
#
# POST-CONDITIONS:
# BROADCAST - broadcast address
#
cfg_broadcast() {
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Configure BROADCAST address"
\
--ok-label "$BTN_ACCEPT"
\
--cancel-label "$BTN_CANCEL"
\
--inputbox
\
"Enter the broadcast address for this network
device" \
0 0`

if [ $? == 0 ]; then
BROADCAST=$COMMAND
fi
return 0
}


# cfg_netmask()
#
# Prompts user to enter the netmask.
#
# POST-CONDITIONS:
# NETMASK - netmask
#
cfg_netmask() {
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Configure NETMASK"
\
--ok-label "$BTN_ACCEPT"
\
--cancel-label "$BTN_CANCEL"
\
--inputbox
\
"Enter the netmask for this network device"
\
0 0`

if [ $? == 0 ]; then
NETMASK=$COMMAND
fi
return 0
}


# cfg_gateway()
#
# Prompts user to enter the gateway address.
#
# POST-CONDITIONS:
# GATEWAY - gateway address
#
cfg_gateway() {
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Configure GATEWAY address"
\
--ok-label "$BTN_ACCEPT"
\
--cancel-label "$BTN_CANCEL"
\
--inputbox
\
"Enter the gateway address for this network device"
\
0 0`

if [ $? == 0 ]; then
GATEWAY=$COMMAND
fi
return 0
}

# remove_device()
#
# Prompts user to confirm removing the device file.
#
# PARAMETERS: network device file
#
# POST-CONDITIONS:
# device file is removed
#
remove_device() {
if [ -z "$1" ]; then
echo "Error: remove_device() called with no parameter"
exit 1;
fi

COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Confirm device removal"
\
--yesno
\
"Are you sure you want to remove device file: $1?"
\
0 0`

if [ $? == 0 ]; then
rm -f $1
return 1 # return 1 to return back to main menu
fi

return 0
}

# save_device()
#
# Prompts user to confirm saving the device file.
#
# PARAMETERS: network device file
#
# POST-CONDITIONS:
# device file is saved
#
save_device() {
if [ -z "$1" ]; then
echo "Error: save_device() called with no parameter"
exit 1;
fi

COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Confirm device saving"
\
--yesno
\
"Are you sure you want to save device file: $1?"
\
0 0`

if [ $? == 0 ]; then
echo "# `basename $1 .dev` configuration" > $1
echo "# created by: ${BACKTITLE}" >> $1
echo "# last modified: `date`" >> $1
echo "MODULE=${MODULE}" >> $1
echo "MODE=${MODE}" >> $1
echo "IP=${IP}" >> $1
echo "BROADCAST=${BROADCAST}" >> $1
echo "NETMASK=${NETMASK}" >> $1
echo "GATEWAY=${GATEWAY}" >> $1
return 1 # return 1 to return back to main menu
fi

return 0
}

# add_device()
#
# Prompts user for device name and passes control
# to cfg_device.
#
add_device() {
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Add new network device"
\
--ok-label "$BTN_ACCEPT"
\
--cancel-label "$BTN_CANCEL"
\
--inputbox
\
"Enter the name for this device (e.g. eth0, plip1)"
\
0 0`

if [ $? != 0 ]; then
return 0
fi

if [ -z "$COMMAND" ]; then
return 0
fi

cfg_device "${NETDEVDIR}/${COMMAND}.dev"
return 0
}

cfg_device() {
if [ -z "$1" ]; then
echo "Error: cfg_device() called with no parameter"
exit 1;
fi

# load the network device file
loadnetdev "$1"

zeroreturn
while [ $? == 0 ]; do

if [ -z "$MODULE" ]; then
dmodule="None (use static driver)"
else
dmodule=$MODULE
fi

if [ `echo $MODE` = 'dynamic' ]; then
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Modify, Remove Network Device"
\
--ok-label "$BTN_SELECT"
\
--cancel-label "$BTN_BACK"
\
--menu
\
"Choose a setting to modify, or
select Remove to \
remove the device"
\
0 0 0
\
"MODULE" "${dmodule}"
\
"MODE" "dynamic (use dhcp)"
\
"Remove" "Remove this network
device" \
"Save" "Save these settings to the
device file"`
else
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Modify, Remove Network Device"
\
--ok-label "$BTN_SELECT"
\
--cancel-label "$BTN_BACK"
\
--menu
\
"Choose a setting to modify, or
select Remove to \
remove the device"
\
0 0 0
\
"MODULE" "${dmodule}"
\
"MODE" "static"
\
"IP" "${IP}"
\
"BROADCAST" "${BROADCAST}"
\
"NETMASK" "${NETMASK}"
\
"GATEWAY" "${GATEWAY}"
\
"Remove" "Remove this network
device" \
"Save" "Save these settings to the
device file"`
fi


if [ $? != 0 ]; then
unset dmodule
return 0
fi

case $COMMAND in
'MODULE') cfg_module ;;
'MODE') cfg_mode ;;
'IP') cfg_ip ;;
'BROADCAST') cfg_broadcast ;;
'GATEWAY') cfg_gateway ;;
'NETMASK') cfg_netmask ;;
'Remove') remove_device "$1" ;;
'Save') save_device "$1" ;;
esac

done
return 0
}

main() {
unset GETNETMODS
zeroreturn
while [ $? == 0 ]; do
getnetdevs
COMMAND=`$DIALOG --backtitle "$BACKTITLE"
\
--stdout
\
--title "Add, Modify, Remove Netowrk
Devices" \
--ok-label "$BTN_SELECT"
\
--cancel-label "$BTN_EXIT"
\
--menu
\
"Choose a device to modify or remove, or
select Add \
to add a new device"
\
0 0 0
\
${NETDEVSMENU[*]}
\
"Add" "Add new network device"`

if [ $? != 0 ]; then
# Exit
exit 1;
fi

case $COMMAND in
'Add') add_device ;;
*) cfg_device "${NETDEVS[$COMMAND]}" ;;
esac
done
}


# --- entry point --- #
if [ "$ROOTCHECK" == 1 ]; then
if [ "$UID" != 0 ]; then
echo "Enter the root password, please."
su - -c "DISPLAY=$DISPLAY PATH=$PATH $0 $*"
fi
fi

main $*



Archive powered by MHonArc 2.6.24.

Top of Page