#!/bin/bash #set -x # # The purpose of this script is to ease switching between network profiles # version=1.3 netprofmgrdir="/etc/netprofmgr" id=`whoami` if [[ "$id" != 'root' ]];then echo -e "Error: $0 must be run as root."; exit 1;fi # # Initialize all variables # bootproto='dhcp' newip='' newdom='' newgw='' newnm='' newessid='ANY' # # Define appropriate usage # usage() { errmsg=$1 if [ -n "$errmsg" ]; then echo "$errrmsg";fi echo "Usage: $0 or Usage: $0 -a -p -a # Action to be performed where valid actions include the following: use # Switch the network configuration to use a new profile del # Delete a network profile list # List available profiles install # Setup the /etc/netprofmgr profiles directory and add some default profiles -p # Profile name # The following optional parameters are used only for adding a new profile -I -i -n -g -N -D -e -K -H " exit 1 } # # List profiles # list_profiles() { echo "Here are the available profiles:" cd "$netprofmgrdir/profiles" ls -1 | sed -e "s/^/ /g" | sort echo -e "\nUse -h flag to see proper usage" exit 0 } # # Validate usage # if [[ -z "$*" ]];then list_profiles; fi # # Find network interfaces # ifs=`ifconfig -a | grep Link | awk '{ print $1 }' | egrep -v "^cipsec0$|^lo$|^inet6$|^sit0$|^$"` # # Use this profile # useprofile() { profile="$1" profiledir="$netprofmgrdir/profiles/$profile" if [ -d "$profiledir" ] && [ -n "$ifs" ] then # # Stop networking with the existing profile # /etc/init.d/network stop # # Remove all existing network interface configurations # cd /etc/sysconfig/network-scripts cfgs=`ls -1 ifcfg-* | grep -v ifcfg-lo` for cfg in $cfgs keys-* do rm -f $cfg done # # Copy the config files for this profile into their # proper system locations # if [ -f "$profiledir/network" ] then cp "$profiledir/network" /etc/sysconfig/network fi if [ -f "$profiledir/resolv.conf" ] then cp "$profiledir/resolv.conf" /etc/resolv.conf fi for if in $ifs do if [ -f "$profiledir/ifcfg-$if" ] then cp "$profiledir/ifcfg-$if" /etc/sysconfig/network-scripts fi if [ -f "$profiledir/keys-$if" ] then cp "$profiledir/keys-$if" /etc/sysconfig/network-scripts fi done # # Start up networking with the new profile # /etc/init.d/network start else usage fi } mk_profile() { profile="$1" thisif="$2" profiledir="$netprofmgrdir/profiles/$profile" # # Make the profile directory # if [[ -d "$profiledir" ]] then true else mkdir -p "$profiledir" fi for if in $ifs do # # If not the selected interface set onboot to 'off' # if [[ "$if" == "$thisif" ]] then onboot='on' isenabled='enabled' else onboot='off' isenabled='disabled' fi # # If IP address isn't specified, assume dhcp # if [[ -n "$newip" ]] then bootproto='static' fi if [[ "$isenabled" == 'enabled' ]] then echo "Adding $isenabled $bootproto interface $if to profile $profile" else echo "Adding $isenabled interface $if to profile $profile" fi # # Get the network type and hardware MAC address # nettype=`ifconfig $if | grep encap: | awk '{ print $3 }' | cut -d: -f2` hwaddr=`ifconfig $thisif | grep HWaddr | awk '{ print $5 }'` # # Create interface config file # Wireless='' Wireless=`iwlist $if power 2>&1 | grep "Current mode"` if [[ -n "$Wireless" ]] then # # If hex key not set, convert ascii to hex # if [[ -z "$hexkey" ]] then #if [[ -z "$asciikey" ]] #then # hexkey=`ascii2hex "$asciikey"` #fi hexkey='' fi # # Wireless # cat > $profiledir/ifcfg-$if < $profiledir/keys-$if < $profiledir/ifcfg-$if < $profiledir/network # # Setup name resolution # if [[ "$bootproto" == 'static' ]] then if [[ -n "$domain" ]] then echo "domain $domain" > $profiledir/resolv.conf else cp /dev/null $profiledir/resolv.conf fi if [[ -n "$newns" ]] then for ns in $newns do echo "nameserver $newns" >> $profiledir/resolv.conf done fi fi fi } install_netprofmgr() { # # Make directories # mkdir -p "$netprofmgrdir/profiles/off" "/usr/local/bin" 2> /dev/null # # Copy netprofmgr to /usr/local/bin # cp -p $0 /usr/local/bin if [[ -f "/usr/local/bin/netprofmgr" ]] then true else cp ./netprofmgr /usr/local/bin fi # # Add the off profile # echo "Adding a network profile called off to disable all networking." if [[ -f "$netprofmgrdir/off/network" ]] then true else echo -e "HOSTNAME=$hostname\nNETWORKING=no" > $netprofmgrdir/profiles/off/network fi # # Make dhcp profile for each interface # for newif in $ifs do mk_profile "dhcp-$newif" "$newif" done } ############################################################################## # # Define error message routine # error_message() { errmsg=$1 if [ -n "$errmsg" ] then echo -e "Error: $errmsg" echo "Use -h flag to see proper usage" exit 1 fi } # # If hostname not set, set to system default # hostname=`hostname` if [[ -z "$hostname" ]] then hostname='localhost.localdomain' fi ############################################################################## # # Evaluate parameters # while getopts a:I:si:n:g:N:D:e:K:H:p:hv OPT do case $OPT in a|+a) if [ -z "$OPTARG" ];then action='use';else action="$OPTARG"; fi ;; I|+I) if [ -z "$OPTARG" ];then error_message "Must provide an interface name with the -I flag";fi newif="$OPTARG" ;; i|+i) if [ -z "$OPTARG" ];then error_message "Must provide an IP address with the -i flag";fi newip="$OPTARG" ;; n|+n) if [ -z "$OPTARG" ];then error_message "Must provide a netmask with the -n flag";fi newnm="$OPTARG" ;; g|+g) if [ -z "$OPTARG" ];then error_message "Must provide a default gateway with the -g flag";fi newgw="$OPTARG" ;; N|+N) if [ -z "$OPTARG" ];then error_message "Must provide a nameserver with the -N flag";fi newns="$OPTARG" ;; D|+D) if [ -z "$OPTARG" ];then error_message "Must provide a domain with the -D flag";fi newdom="$OPTARG" ;; e|+e) if [ -z "$OPTARG" ];then error_message "Must provide an ESSID with the -e flag";fi newessid="$OPTARG" ;; K|+K) if [ -z "$OPTARG" ];then error_message "Must provide a ascii key with the -K flag";fi newkey="$OPTARG" ;; H|+H) if [ -z "$OPTARG" ];then error_message "Must provide a hex key with the -H flag";fi newhexkey="$OPTARG" ;; p|+p) if [ -z "$OPTARG" ];then error_message "Must provide a profile name with the -n flag";fi profile="$OPTARG" profiledir="$netprofmgrdir/profiles/$profile" ckprof=`echo "$profile" |sed -e "s/[a-Z0-9\-]//g"` if [ -n "$ckprof" ];then error_message "Profile can only contain alphanumeric characters.";fi ;; h|+h) usage;; v|+v) echo "netprofmgr version $version"; exit 0;; *) usage;; esac done shift `expr $OPTIND - 1` # # If just the profile is provided then just use it # if [[ -n "$1" ]] && [[ -d "$netprofmgrdir/profiles/$1" ]] then useprofile "$1" exit fi case "$action" in 'use') useprofile "$profile";; 'list') list_profiles;; 'install') install_netprofmgr ;; 'add') senderr='false' if [[ -z "$newif" ]] then senderr='true' else if [[ -n "$newip" ]] then if [[ -z "$newnm" ]] || [[ -z "$newgw" ]] || [[ -z "$newns" ]] then senderr='true' fi fi fi if [[ "$senderr" == 'true' ]] then error_message "Error: must provide the following information when adding a network\nprofile:\n Network interface\n\nIf the network interface is to be configured as a static IP address, then the following additional information\nalso needs to be supplied:\n TCP/IP Address\n Network Mask\n Default Gateway\n Primary DNS Server\n\nOptionally you can also provide the following:\n Domain name\n ESSID for wireless networking\n Hex WEP encryption key for wireless networking" else mk_profile "$profile" "$newif" fi ;; 'del') if [[ -n "$profile" ]] && [[ -d "$profiledir" ]] then rm -fr "$profiledir" fi ;; *) error_message "\"$action\" is not a valid action";; esac