#!/bin/bash #set -x # # The purpose of this script (netprofmgr) is to ease switching between network profiles # # Copyright (C) 2006 Brad Diggs # # This program 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 program 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 can obtain a copy of the GNU General Public License via the # following URL http://www.gnu.org/licenses/gpl.txt or in writing # via the Free Software Foundation, Inc., 51 Franklin Street, # Fifth Floor, Boston, MA 02110-1301, USA. # version=1.6 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: negmgr-gui This is the graphical netprofmgr profile selector. or Usage: $0 -a -p -a # Action to be performed where valid actions include the following: add # Add a new network profile 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 -h # Display usage -l # Display license # The following optional parameters are used only for adding a new profile -I -i -n -g -N -D -e -K -H " exit 1 } # # Define appropriate usage # showlicense() { echo "The purpose of this script (netprofmgr) is to ease switching between Fedora/RedHat network profiles Copyright (C) 2006 Brad Diggs This program 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 program 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 can obtain a copy of the GNU General Public License via the following URL http://www.gnu.org/licenses/gpl.txt or in writing via the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA." exit } ############################################################################## # # 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 } # # Ascii to hex converter # myascii2hex() { # Thanks to http://jipe.homelinux.org/eg_scripts/ascii2hex for this example ASCII=$1 echo -e "KEY=\c" for ((a=0; a<${#ASCII}; a++)) do b=${ASCII:$a:1} case $b in $'\0') c=00 ;; $'\1') c=01 ;; $'\2') c=02 ;; $'\3') c=03 ;; $'\4') c=04 ;; $'\5') c=05 ;; $'\6') c=06 ;; $'\7') c=07 ;; $'\10') c=08 ;; $'\11') c=09 ;; $'\12') c=0A ;; $'\13') c=0B ;; $'\14') c=0C ;; $'\15') c=0D ;; $'\16') c=0E ;; $'\17') c=0F ;; $'\20') c=10 ;; $'\21') c=11 ;; $'\22') c=12 ;; $'\23') c=13 ;; $'\24') c=14 ;; $'\25') c=15 ;; $'\26') c=16 ;; $'\27') c=17 ;; $'\30') c=18 ;; $'\31') c=19 ;; $'\32') c=1A ;; $'\33') c=1B ;; $'\34') c=1C ;; $'\35') c=1D ;; $'\36') c=1E ;; $'\37') c=1F ;; $'\40') c=20 ;; $'\41') c=21 ;; $'\42') c=22 ;; $'\43') c=23 ;; $'\44') c=24 ;; $'\45') c=25 ;; $'\46') c=26 ;; $'\47') c=27 ;; $'\50') c=28 ;; $'\51') c=29 ;; $'\52') c=2A ;; $'\53') c=2B ;; $'\54') c=2C ;; $'\55') c=2D ;; $'\56') c=2E ;; $'\57') c=2F ;; $'\60') c=30 ;; $'\61') c=31 ;; $'\62') c=32 ;; $'\63') c=33 ;; $'\64') c=34 ;; $'\65') c=35 ;; $'\66') c=36 ;; $'\67') c=37 ;; $'\70') c=38 ;; $'\71') c=39 ;; $'\72') c=3A ;; $'\73') c=3B ;; $'\74') c=3C ;; $'\75') c=3D ;; $'\76') c=3E ;; $'\77') c=3F ;; $'\100') c=40 ;; $'\101') c=41 ;; $'\102') c=42 ;; $'\103') c=43 ;; $'\104') c=44 ;; $'\105') c=45 ;; $'\106') c=46 ;; $'\107') c=47 ;; $'\110') c=48 ;; $'\111') c=49 ;; $'\112') c=4A ;; $'\113') c=4B ;; $'\114') c=4C ;; $'\115') c=4D ;; $'\116') c=4E ;; $'\117') c=4F ;; $'\120') c=50 ;; $'\121') c=51 ;; $'\122') c=52 ;; $'\123') c=53 ;; $'\124') c=54 ;; $'\125') c=55 ;; $'\126') c=56 ;; $'\127') c=57 ;; $'\130') c=58 ;; $'\131') c=59 ;; $'\132') c=5A ;; $'\133') c=5B ;; $'\134') c=5C ;; $'\135') c=5D ;; $'\136') c=5E ;; $'\137') c=5F ;; $'\140') c=60 ;; $'\141') c=61 ;; $'\142') c=62 ;; $'\143') c=63 ;; $'\144') c=64 ;; $'\145') c=65 ;; $'\146') c=66 ;; $'\147') c=67 ;; $'\150') c=68 ;; $'\151') c=69 ;; $'\152') c=6A ;; $'\153') c=6B ;; $'\154') c=6C ;; $'\155') c=6D ;; $'\156') c=6E ;; $'\157') c=6F ;; $'\160') c=70 ;; $'\161') c=71 ;; $'\162') c=72 ;; $'\163') c=73 ;; $'\164') c=74 ;; $'\165') c=75 ;; $'\166') c=76 ;; $'\167') c=77 ;; $'\170') c=78 ;; $'\171') c=79 ;; $'\172') c=7A ;; $'\173') c=7B ;; $'\174') c=7C ;; $'\175') c=7D ;; $'\176') c=7E ;; $'\177') c=7F ;; $'\200') c=80 ;; $'\201') c=81 ;; $'\202') c=82 ;; $'\203') c=83 ;; $'\204') c=84 ;; $'\205') c=85 ;; $'\206') c=86 ;; $'\207') c=87 ;; $'\210') c=88 ;; $'\211') c=89 ;; $'\212') c=8A ;; $'\213') c=8B ;; $'\214') c=8C ;; $'\215') c=8D ;; $'\216') c=8E ;; $'\217') c=8F ;; $'\220') c=90 ;; $'\221') c=91 ;; $'\222') c=92 ;; $'\223') c=93 ;; $'\224') c=94 ;; $'\225') c=95 ;; $'\226') c=96 ;; $'\227') c=97 ;; $'\230') c=98 ;; $'\231') c=99 ;; $'\232') c=9A ;; $'\233') c=9B ;; $'\234') c=9C ;; $'\235') c=9D ;; $'\236') c=9E ;; $'\237') c=9F ;; $'\240') c=A0 ;; $'\241') c=A1 ;; $'\242') c=A2 ;; $'\243') c=A3 ;; $'\244') c=A4 ;; $'\245') c=A5 ;; $'\246') c=A6 ;; $'\247') c=A7 ;; $'\250') c=A8 ;; $'\251') c=A9 ;; $'\252') c=AA ;; $'\253') c=AB ;; $'\254') c=AC ;; $'\255') c=AD ;; $'\256') c=AE ;; $'\257') c=AF ;; $'\260') c=B0 ;; $'\261') c=B1 ;; $'\262') c=B2 ;; $'\263') c=B3 ;; $'\264') c=B4 ;; $'\265') c=B5 ;; $'\266') c=B6 ;; $'\267') c=B7 ;; $'\270') c=B8 ;; $'\271') c=B9 ;; $'\272') c=BA ;; $'\273') c=BB ;; $'\274') c=BC ;; $'\275') c=BD ;; $'\276') c=BE ;; $'\277') c=BF ;; $'\300') c=C0 ;; $'\301') c=C1 ;; $'\302') c=C2 ;; $'\303') c=C3 ;; $'\304') c=C4 ;; $'\305') c=C5 ;; $'\306') c=C6 ;; $'\307') c=C7 ;; $'\310') c=C8 ;; $'\311') c=C9 ;; $'\312') c=CA ;; $'\313') c=CB ;; $'\314') c=CC ;; $'\315') c=CD ;; $'\316') c=CE ;; $'\317') c=CF ;; $'\320') c=D0 ;; $'\321') c=D1 ;; $'\322') c=D2 ;; $'\323') c=D3 ;; $'\324') c=D4 ;; $'\325') c=D5 ;; $'\326') c=D6 ;; $'\327') c=D7 ;; $'\330') c=D8 ;; $'\331') c=D9 ;; $'\332') c=DA ;; $'\333') c=DB ;; $'\334') c=DC ;; $'\335') c=DD ;; $'\336') c=DE ;; $'\337') c=DF ;; $'\340') c=E0 ;; $'\341') c=E1 ;; $'\342') c=E2 ;; $'\343') c=E3 ;; $'\344') c=E4 ;; $'\345') c=E5 ;; $'\346') c=E6 ;; $'\347') c=E7 ;; $'\350') c=E8 ;; $'\351') c=E9 ;; $'\352') c=EA ;; $'\353') c=EB ;; $'\354') c=EC ;; $'\355') c=ED ;; $'\356') c=EE ;; $'\357') c=EF ;; $'\360') c=F0 ;; $'\361') c=F1 ;; $'\362') c=F2 ;; $'\363') c=F3 ;; $'\364') c=F4 ;; $'\365') c=F5 ;; $'\366') c=F6 ;; $'\367') c=F7 ;; $'\370') c=F8 ;; $'\371') c=F9 ;; $'\372') c=FA ;; $'\373') c=FB ;; $'\374') c=FC ;; $'\375') c=FD ;; $'\376') c=FE ;; $'\377') c=FF ;; esac echo -n "$c" done echo } # # List profiles # list_profiles() { if [[ -f "$netprofmgrdir/current_profile" ]] then ap=`cat $netprofmgrdir/current_profile` echo -e "The current profile is: $ap\n" fi 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 } # # List profiles for gui # gnome-gui-selector() { cd "$netprofmgrdir/profiles" list=`ls -1 | sed -e "s/^/x /g"` profile=`zenity --list --list-text "Select netprofmgr profile to use. Then click on \"Ok\"." --column "" --column "Profiles" --radiolist --print-column "1" $list` if [[ -n "$profile" ]] && [[ -d "/etc/netprofmgr/profiles/$profile" ]] then out=`netprofmgr $profile | sed -e "s/\[//g" -e "s/\]//g" -e "s/\//g" -e "s/60G//g" -e "s/0;//g" -e "s/3[129]m//g"` out=`echo -e "Network profile manager switching to profile \"$profile\"\n\n$out\n\nSwitch to profile \"$profile\" complete."` zenity --info --info-text "$out" exit $? else error_message "Must select a profile." fi exit 0 } # # See if being called in gui mode # progname=`basename $0` if [[ "$progname" == 'netprofmgr-gui' ]] then gnome-gui-selector fi # # Validate usage # if [[ -z "$*" ]];then list_profiles; fi # # Find network interfaces # ifs=`ifconfig -a 2> /dev/null| 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 -f "$profiledir/network" /etc/sysconfig/network fi if [ -f "$profiledir/resolv.conf" ] then cp "$profiledir/resolv.conf" /etc/resolv.conf fi if [ -f "$profiledir/ntp.conf" ] then cp -f "$profiledir/ntp.conf" /etc/ntp.conf fi for if in $ifs do if [ -f "$profiledir/ifcfg-$if" ] then cp -f "$profiledir/ifcfg-$if" /etc/sysconfig/network-scripts fi if [ -f "$profiledir/keys-$if" ] then cp -f "$profiledir/keys-$if" /etc/sysconfig/network-scripts fi done # # Start up networking with the new profile # /etc/init.d/network start else usage fi echo "$profile" > $netprofmgrdir/current_profile } 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 2> /dev/null| grep encap: | awk '{ print $3 }' | cut -d: -f2` hwaddr=`ifconfig $thisif 2> /dev/null| grep HWaddr | awk '{ print $5 }'` # # Create interface config file # WirelessTxPower=`iwlist $if txpower 2> /dev/null | grep Current | cut -d: -f2 | awk '{ print $1 }'` if [[ $WirelessTxPower -gt 0 ]] then # # If hex key not set, convert ascii to hex # if [[ -z "$newhexkey" ]] then if [[ -n "$newasciikey" ]] then newhexkey=`myascii2hex "$newasciikey"` else newhexkey='' fi 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 -f /dev/null $profiledir/resolv.conf fi if [[ -n "$newns" ]] then for ns in $newns do echo "nameserver $newns" >> $profiledir/resolv.conf done fi fi # # Grab a copy of the current ntp.conf # if [ -f "/etc/ntp.conf" ] then cp /etc/ntp.conf "$profiledir/ntp.conf" > /dev/null 2>&1 fi fi } install_netprofmgr() { # # Make directories # mkdir -p "$netprofmgrdir/profiles/off" "/usr/local/bin" 2> /dev/null # # Copy netprofmgr to /usr/local/bin # cp -fp $0 /usr/local/bin if [[ -f "/usr/local/bin/netprofmgr" ]] then true else cp -f ./netprofmgr /usr/local/bin fi # # Set up link for gui link # ln -s /usr/local/bin/netprofmgr /usr/local/bin/netprofmgr-gui # # 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 } # # 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:hlv 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 newasciikey="$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;; l|+l) showlicense;; 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 if [[ -d "$profiledir" ]] then error_message "Profile \"$profile\" already exists. Please use a different profile\nname or delete the existing \"$profile\" profile." else mk_profile "$profile" "$newif" fi fi ;; 'del') if [[ -n "$profile" ]] && [[ -d "$profiledir" ]] then rm -fr "$profiledir" fi ;; *) error_message "\"$action\" is not a valid action";; esac