From 863e8311cfc2ec7dc98fdb43abaa8499dd3414a4 Mon Sep 17 00:00:00 2001 From: Judd Vinet Date: Thu, 30 Jun 2005 23:57:54 +0000 Subject: added new netcfg stuff for roaming network profiles --- netcfg | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100755 netcfg (limited to 'netcfg') diff --git a/netcfg b/netcfg new file mode 100755 index 0000000..d9166f9 --- /dev/null +++ b/netcfg @@ -0,0 +1,244 @@ +#!/bin/bash + +. /etc/rc.conf +. /etc/rc.d/functions +[ -f /etc/conf.d/dhcpcd ] && . /etc/conf.d/dhcpcd + +NETCFG_VER=0.1 +PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH" + +PROFILE_DIR="/etc/network-profiles" +STATE_DIR="/var/run/net" + +version() +{ + echo "netcfg v$NETCFG_VER" +} + +usage() +{ + version + echo + echo "usage: netcfg " + echo " netcfg --stop " + echo " netcfg --menu [--timeout ]" + echo " netcfg --stopall" + echo + echo "Network profiles are stored in $PROFILE_DIR" + echo +} + +stop_profile() +{ + if [ "$1" = "" ]; then + echo "error: missing interface name (eg, eth0)" + exit 1 + fi + INTERFACE=$1 + [ -f $STATE_DIR/$INTERFACE ] || return + + unset GATEWAY + . $STATE_DIR/$INTERFACE + + stat_busy "Shutting down interface: $INTERFACE" + + # bring down the default route (gateway) + [ "$GATEWAY" ] && route del default gw $GATEWAY + + # bring down the interface + ifconfig $INTERFACE down + + rm -f $STATE_DIR/$INTERFACE + + stat_done +} + +stop_all() +{ + [ -d $STATE_DIR ] || return + for prof in `ls $STATE_DIR`; do + unset INTERFACE + . $prof + stop_profile $INTERFACE + done +} + +start_profile() +{ + if [ "$1" = "" ]; then + echo "error: missing profile name" + exit 1 + fi + if [ ! -f $PROFILE_DIR/$1 ]; then + echo "error: $PROFILE_DIR/$1 is missing" >&2 + exit 1 + fi + + # Read the profile + . $PROFILE_DIR/$1 + + # Shut down any profiles tied to this interface + stop_profile $INTERFACE + + stat_busy "Starting network profile: $1" + + # Re-read the profile (stop_profile might have overwritten our settings) + unset DESCRIPTION INTERFACE IFOPTS IWOPTS WIFI_INTERFACE + unset GATEWAY HOSTNAME DOMAIN DNS1 DNS2 + . $PROFILE_DIR/$1 + + # Configure wireless settings, if necessary + [ "$WIFI_INTERFACE" ] || WIFI_INTERFACE=$INTERFACE + if [ "$IWOPTS" ]; then + iwconfig $WIFI_INTERFACE $IWOPTS + [ $? -ne 0 ] && stat_fail && return + fi + + if [ "$IFOPTS" = "dhcp" -o "$IFOPTS" = "DHCP" ]; then + dhcpcd $DHCPCD_ARGS $INTERFACE + [ $? -ne 0 ] && stat_fail && return + else + # bring up the interface + ifconfig $INTERFACE $IFOPTS up + [ $? -ne 0 ] && stat_fail && return + + # bring up the default route (gateway) + if [ "$GATEWAY" ]; then + route add default gw $GATEWAY + [ $? -ne 0 ] && stat_fail && return + fi + fi + + # set the hostname + if [ "$HOSTNAME" ]; then + hostname $HOSTNAME + [ $? -ne 0 ] && stat_fail && return + fi + + # Generate a new resolv.conf + if [ "$DNS1" ]; then + : >/etc/resolv.conf + [ $? -ne 0 ] && stat_fail && return + [ "$DOMAIN" ] && echo "domain $DOMAIN" >>/etc/resolv.conf + [ "$DNS1" ] && echo "nameserver $DNS1" >>/etc/resolv.conf + [ "$DNS2" ] && echo "nameserver $DNS2" >>/etc/resolv.conf + fi + + # Save the info in /var/run so we can shut it down later + mkdir -p $STATE_DIR + cp $PROFILE_DIR/$1 $STATE_DIR/$INTERFACE + stat_done +} + +menu() +{ + if [ "`ls $PROFILE_DIR 2>/dev/null | grep -v ^template$`" = "" -o ! -d $PROFILE_DIR ]; then + echo "No profiles found. Add profiles in $PROFILE_DIR" + return + fi + # scan all profiles + unset profiles + DEFAULT= + i=0 + for prof in `ls $PROFILE_DIR`; do + # ignore the template + [ "$prof" = "template" ] && continue + NAME=$prof + + # if there's a profile called "main", use that as default + [ "$NAME" = "main" ] && DEFAULT=$NAME + unset DESCRIPTION + . $PROFILE_DIR/$NAME + if [ "$DESCRIPTION" ]; then + profiles[$i]=$NAME + i=$((i+1)) + profiles[$i]=$DESCRIPTION + i=$((i+1)) + fi + done + + if [ ${#profiles} -eq 0 ]; then + echo "No profiles were found in $PROFILE_DIR" + return + fi + + # if no default yet, use the first entry + [ "$DEFAULT" = "" ] && DEFAULT=${profiles[0]} + + ANSWER=`mktemp` + + if [ "$TIMEOUT" != "" ]; then + dialog \ + --timeout $TIMEOUT \ + --default-item $DEFAULT \ + --menu "Select the network profile you wish to use\n\n (timeout in $TIMEOUT seconds)" \ + 13 50 6 \ + "${profiles[@]}" 2>$ANSWER + ret=$? + else + dialog \ + --default-item $DEFAULT \ + --menu "Select the network profile you wish to use" \ + 13 50 6 \ + "${profiles[@]}" 2>$ANSWER + ret=$? + fi + + case $ret in + 1) ;; # cancel - do nothing + 255) start_profile $DEFAULT ;; # timeout - use default + 0) start_profile `cat $ANSWER` ;; # user selection + # abnormal + *) echo "abnormal ret code from dialog: $ret" ;; + esac + + rm $ANSWER +} + +# +# Begin +# + +if [ "`id -u`" != "0" ]; then + echo "This script should be run as root." + exit 1 +fi + +# Parse command line +MODE="profile" +PROFILE= +IFACE= +TIMEOUT= +while [ $# -ne 0 ]; do + case $1 in + --version) MODE="ver" ;; + --help) MODE="usage" ;; + --menu) MODE="menu" ;; + --stopall) MODE="stopall" ;; + --stop) MODE="stop" + shift + IFACE=$1 ;; + --timeout) shift + TIMEOUT=$1 ;; + --*) MODE="usage" ;; + -*) MODE="usage" ;; + *) PROFILE=$1 ;; + esac + shift +done + +if [ "$MODE" = "profile" -a "$PROFILE" = "" ]; then + MODE="usage" +fi + +# Figure out what we're doing... +[ "$MODE" = "ver" ] && version +[ "$MODE" = "usage" ] && usage +[ "$MODE" = "profile" ] && start_profile $PROFILE +[ "$MODE" = "stop" ] && stop_profile $IFACE +[ "$MODE" = "stopall" ] && stop_all +[ "$MODE" = "menu" ] && menu + +exit 0 + +# vim: set ts=2 noet: -- cgit v1.2.3