Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Oct 2001 23:53:27 +0000
From:      Michael W.Collette <metrol@metrol.net>
To:        FreeBSD <freebsd-mobile@FreeBSD.org>
Subject:   Network Profiler (netprof)
Message-ID:  <20011012065913.6BE2937B403@hub.freebsd.org>

index | next in thread | raw e-mail

[-- Attachment #1 --]
Howdy folks,

Since I've gotten FreeBSD installed on this laptop of mine here one of the 
biggest issues I've had is with changing locations.  Going from work to home, 
or even between buildings at work proves to be a hassle.  Dealing with the IP 
address, DNS servers, Windows workgroup settings, differing ipfw needs, and 
all that.  This isn't about my complaining, but rather what I'm working on to 
try and simplify this process.

Goals:
1. To be able to switch network configurations on the fly.  Should be quick 
and easy to set up, and generic enough for most any laptop user to put to use.

2. Use generic shell scripting so as not to require any dependancies, or 
allow me to get too clever with how this is done.

Where This Is At:
Attached to this mail is a script I called "netprof".  I put it in the /sbin 
directory allowing for execution by root only.  Anywhere in your path should 
work just as well.  Before running it you'll need to edit some of the 
variables at the very top of the script to match your system.  Things like 
which network interface you're working with, what config files you want 
backed up, and where netprof should store it's files.

The first time you run it, it will create a directory called "/etc/netprof" 
(if you left it defaulted) to archive your present network settings.  This 
simply copies the config files you specified with a "-default" tacked on to 
the file name.  Once run the first time you can then type...

netprof help

...to get the basic help text on usage.  To create a new profile you'd just 
go into your various config files like "inetd.conf" and "rc.conf" and make 
the tweaks that fit your location.  Once you're happy with the config just 
give netprof the name.

netprof AtWork

If the profile "AtWork" doesn't exist, netprof asks if you want to create it. 
If that profile does exist, then all your config files are updated to that 
profile and network services are restarted.  If you make a change to your 
network config files and you want to update "AtWork" then just type...

netprof update AtWork

You can then get a list of your stored profiles with...

netprof list

Got laid off and want to delete that profile?

netprof delete AtWork

What I Know This Still Needs:
I need to do more work with restarting both pppd and sendmail.  At this time 
netprof assumes that the only thing you've got is an ethernet connection.

At some point I may work up a seperate config file so you don't have to edit 
the script itself.  At the moment I just wanted to keep it all in one file as 
I work on it.

There needs to be some better error checking to insure the user doesn't use a 
profile name that'd goof things up.  Using a hyphen in a profile name would 
mess things up a bit, and some error checking should be in place to prevent 
this.

Why Am I Sending To This List?:
Just looking for some feedback, pointers, or whatever.  I've personally found 
this to be very useful for me going between networks as it is now, though I 
can see that other capabilities will probably be needed.  I was hoping that 
others might find this of use, and perhaps might even make a port out of it 
someday once I get in all the other stuff I'd like to see in it.

So, what ya think?

Later on,
-- 
"Outside of a dog, a book is man's best friend. Inside of a dog, it's too 
dark to read."
 - Groucho Marx
[-- Attachment #2 --]
#!/bin/sh
# Network Profiler 0.1 - 12Oct2001
#
# by: Michael Collette  - metrol@metrol.net
#
# The goal netprof is to provide a quick and simple means to
# change the basic network settings of a FreeBSD box.  This
# is meant to make life a bit easier for folks using FreeBSD
# on a laptop or some other mobile device where different
# locations and network settings are common.


#################################
# Basic Config Variables (Change these to your needs)
Interface="ed0"
NetProfDir="/etc/netprof"


#################################
# Files to be worked with
x="/etc/rc.conf"
x="$x /etc/resolv.conf"
x="$x /etc/hosts"
x="$x /etc/firewall/fwrules"
x="$x /etc/ppp/ppp.conf"
x="$x /usr/local/etc/smb.conf"


##############################
# From here out it's all code

ConfigFiles="$x"
Seperator="-"

NetProfVer="0"
NetProfRev="1"
NetProfDate="12 Oct 2001"

# Check to make sure this is run as 'root'
if [ `whoami` != "root" ]; then
  echo
  echo
  echo "##                      Warning                    ##"
  echo "##                                                 ##"
  echo "## You need to be logged in as root to run netprof ##"
  echo "##                                                 ##"
  echo "##                      Warning                    ##"
  echo
  echo "Exiting..."
  echo
  exit
fi

######################################
# User response verification funtions
TestAns () {
  case $1 in
    [Yy])
      return 2
    ;;
    [Nn])
      return 1
    ;;
    *)
      return 0
    ;;
  esac
}
AskUser () {
  while [ $? -eq 0 ]; do
    echo -n "Answer y or n, then press RETURN: "
    read ans
    TestAns $ans
  done
  return $?
}

############################
# File Copying Fucntions
ArchiveConfigs () {
  ProfName=$1
  
  for WorkFile in $ConfigFiles; do
    if [ -e $WorkFile ]; then
      ArchFile="${NetProfDir}/${WorkFile##*/}$Seperator$ProfName"
      cp -vf ${WorkFile} ${ArchFile}
    fi
  done
}

ArchiveList () {
  ls $NetProfDir | cut -d- -f2 | sort -u
}

DeployProfile () {
  ProfName=$1

  for WorkFile in $ConfigFiles; do
    if [ -e $WorkFile ]; then
      ArchFile="$NetProfDir/${WorkFile##*/}$Seperator$ProfName"
      cp -vf ${WorkFile} ${ArchFile}
    fi
  done
}

####################
# Give a howdy
clear
echo 
echo "#---------------------------------------------------------#"
echo "#--------------- Network Profile Manager -----------------#"
echo "# Version $NetProfVer.$NetProfRev                                 $NetProfDate #"
echo "#                                    by: Michael Collette #"
echo "#                                       metrol@metrol.net #"
echo "#---------------------------------------------------------#"
echo


# Check for a first time run of the script
if [ ! -d $NetProfDir ]; then
  echo "It looks like this is the first time you've run netprof."
  echo
  echo "To continue netprof will need to create" $NetProfDir
  echo "and copy your network config files into it.  Is this okay"
  echo "for me to do now?"
  AskUser
  if [ $? = 2 ]; then
    echo 
    echo "Creating Directory"
    echo "----------------------------------------------------"
    mkdir -v -m 700 $NetProfDir
    echo 
    echo "Creating copies of your present network configuration"
    echo "----------------------------------------------------"
    ArchiveConfigs default
    echo 
    echo "  A profile called 'default' has been created for you."
    echo "To reset your network settings to the present time simply"
    echo "type 'netprof default'.  To later update this profile you'd"
    echo "then type 'netprof update default'."
    echo "  A full breakdown of commands and usage can be accessed by"
    echo "typing 'netprof help'.  Have fun with it!"
    echo
    echo "Exiting...."
    echo
    exit
  fi
fi

###############################
# Command Parser

if [ $# -gt 0 ]; then

  #
  # help command
  #
  if [ $1 = "help" ]; then
    echo "netprof usage..."
    echo ""
    echo "Create a new profile:        netprof {profilename}"
    echo "Switch to existing profile:  netprof {profilename}"
    echo "List all available profiles: netprof list"
    echo "Delete a profile:            netprof delete {profilename}"
    echo "Update an existing profile:  netprof update {profilename}"
    echo ""
    echo "  Replace {profilename} with the name of the profile you wish"
    echo "to work with."
    echo "  For example, let's say that the user wants to set up a network"
    echo "profile for home, and one for work.  To do this the user would"
    echo "while at work log in as root and do the following..."
    echo ""
    echo "> netprof"
    echo "  This creates the default archive.  Then type..."
    echo "> netprof work"
    echo "  Now a 'work' profile is created.  When at home the user alters the"
    echo "config files as needed then types..."
    echo "> netprof home"
    echo "  To switch between the profiles, simply repeat the same command and the"
    echo "the network settings will switch to the profile specified."
    echo ""
    exit
  fi

  #
  # update command
  #
  if [ $1 = "update" ]; then
    if [ $# -gt 1 ]; then
      # Do we have a profile by the name requested?
      if [ ! -z `ls $NetProfDir | cut -d- -f2 | sort -u | grep -w $2` ]; then
        echo
        echo "Update network profile $2 ??"
        echo
        AskUser
 
        if [ $? -eq 2 ]; then
          echo "Updating profile $2 with your present network configuration"
          echo "---------------------------------------------------------"
          ArchiveConfigs $2
          echo
          exit
        else
          exit
        fi
 
      else
        echo
        echo "Can't locate a profile called $2."
        echo "Would you like to create one?"
        echo
        AskUser
 
        if [ $? -eq 2 ]; then
          echo "Creating profile $2 of your present network configuration"
          echo "---------------------------------------------------------"
          ArchiveConfigs $2
          echo
          exit
        else
          exit
        fi
      fi
    else
      echo "You have asked for an update but you did not specify"
      echo "which profile you wish to update."
      echo
      echo "For example, to update a profile called 'work' you'd enter..."
      echo "> netprof update work"
      echo
      echo "Exiting..."
      echo
      exit
    fi
    exit
  fi

  #
  # delete command
  #
  if [ $1 = "delete" ]; then
   # Make sure there's a second parameter
    if [ $# -gt 1 ]; then

      # Do we have a profile by the name requested?
      if [ ! -z `ls $NetProfDir | cut -d- -f2 | sort -u | grep -w $2` ]; then
        echo
        echo "You really sure you want to delete the profile '$2'?"
        echo
        AskUser
        if [ $? -eq 2 ]; then
          echo
          echo " Deleting profile $2"
          echo "---------------------"
          rm -fv $NetProfDir/*-$2
          echo
          exit
        else
          echo
          echo "Profile $2 was NOT deleted"
          echo
          echo "Exiting..."
          echo
          exit
        fi
      else
        echo
        echo "Can't find a profile called $2.  Try doing a 'netprof list'"
        echo "to find the profile you wish to delete."
        echo
        exit
      fi
    else
      echo
      echo "You have requested a delete, but you have not specified a"
      echo "profile name.  For example, to delete a profile called 'work'"
      echo "you would type..."
      echo 
      echo "> netprof delete work"
      echo
      exit
    fi
  fi

  #
  # list command
  #
  if [ $1 = "list" ]; then
    GotCommand=1
    echo "  Profiles presently stored"
    echo "-----------------------------"
    ArchiveList
    echo
    exit
  fi
fi


##################################
# Profile Manager

ResetNetwork=0
if [ $# -gt 0 ]; then
  # Do we have a profile by the name requested?
  if [ ! -z `ls $NetProfDir | cut -d- -f2 | sort -u | grep -w $1` ]; then
    # Switch to the profile requested
    echo
    echo "  Deploying Profile $1"
    echo "------------------------------"
    DeployProfile $1
    echo
    ResetNetwork=1
  else
    # Create a new profile
    echo
    echo "Create network profile $1 ??"
    echo
    AskUser

    if [ $? -eq 2 ]; then
      echo "Creating profile $1 with your present network configuration"
      echo "-----------------------------------------------------------"
      ArchiveConfigs $1
      echo
      exit
    fi
    exit
  fi

else
  echo "In order to make use of netprof you need to pass along"
  echo "some instructions.  For more information type..."
  echo
  echo "> netprof help"
  echo
  exit
fi

##############################
# Reset the networking

if [ $ResetNetwork -eq 1 ]; then

  # Suck in the configuration variables.
  if [ -z "${source_rc_confs_defined}" ]; then
    if [ -r /etc/defaults/rc.conf ]; then
      . /etc/defaults/rc.conf
      source_rc_confs
    elif [ -r /etc/rc.conf ]; then
      . /etc/rc.conf
    fi
  fi

  # Change the IP info
  if [ "${pccard_ifconfig}" = "DHCP" ]; then
    dhclient
  else
    ifconfig ${Interface} ${pccard_ifconfig}
    route -n add default ${defaultrouter}
  fi
  
  if [ $firewall_enable = "YES" ]; then
    echo "   Updating firewall rules"
    echo "-----------------------------"
    $firewall_script
    echo
  fi
  
  # Restart Samba if installed
  if [ -e /usr/local/sbin/smbd ]; then
    if [ -e /var/run/smbd.pid ]; then
      echo "Restarting Samba....."
      echo
      kill `cat /var/run/smbd.pid`
      kill `cat /var/run/smbd.pid`
      /usr/local/sbin/smbd -D
      /usr/local/sbin/nmbd -D
    else
      echo "Starting Samba....."
      echo
      /usr/local/sbin/smbd -D
      # /usr/local/sbin/nmbd -D
    fi
  fi
  
  if [ $inetd_enable = "YES" ]; then
    if [ -e /var/run/inetd.pid ]; then
      echo "Restarting inetd....."
      killall -HUP inetd
      echo
    else
      inetd
    fi
  fi
  
  echo
  echo "Network reset to profile '$1'"
  echo
fi

exit
home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20011012065913.6BE2937B403>