LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 05-27-2008, 02:29 AM   #1
mlsx
LQ Newbie
 
Registered: Oct 2006
Location: china
Distribution: RedFlag
Posts: 16

Rep: Reputation: 0
multiple network profile manager script


Different network configuration profile in differect network environment!

So,we need some utilities to manage more than one network configuration profiles.

I have written a simple scripts to reach it .It dirty but works in Asianux 3.0 distro and Ubuntu 8.0.4 at present.

Code:
#!/bin/bash
#ip switch for Linux
#author: mlsx
#blog: http://mlsx.xplore.cn
#Date: 2008.5.27

#directory save ip profile
dir="/etc/.inet"
function usage
{
  echo "Usage: $0 args \
	args means:
	  -a, --add : add an interface profile
	  -p, --apply: apply this profile 
	  -d, --del <name> : delete an interface profile
	  -l, --list [name] : list all profile,then specify a name"
  exit 67
}

function add_interface
{
  while true
  do
	echo -n "Please a profile name(DO NOT include space)[new]:"
	read name
	[ "$name" == "" ] && name="new"
	if [ -f "$dir/$name" ];then
	  echo "Sorry,the profile name is exists!"
	else
	  break;
	fi
  done
  echo 
  echo  -n "Please your interface[eth0]:"
  read inet
  if [ "$inet" == "" ];then inet="eth0"; fi
  echo 
  while true
  do
	echo "Please Choice:"
	echo " 1) dhcp "
	echo " 2) static ip"
	echo -n "choose your number[1]:"
	read ch
	[ "$ch" == "" ] && ch="1"

	if [ "$ch" != "1" ] && [ "$ch" != "2" ];then
	  echo "incorrect choice"
	else
	  break
	fi
  done
  if [ "$ch" == "2" ];then
	echo -n "IP Address: "
	read ip
	echo -n "netmask: "
	read netmask
	echo -n "gateway: "
	read gateway
	exec 6>&1
	exec >"$dir/$name"
	echo "auto $inet"
	echo "iface $inet inet static"
	echo -e "\t address $ip \n
			 \t netmask $netmask \n
			 \t gateway $gateway" 
  else
	echo "auto $inet " >"$dir/$name"
	echo "iface $inet inet dhcp" >>"$dir/$name"
  fi
  exit 0
}

function del_interface ()
{
  if [ -f "$dir/$1" ];then
    rm -f "$dir/$1"
  else
	echo "No such profile name"
	exit 68
  fi
  exit 0
}

function list_interface()
{
  if [ $# -gt 0 ];then
	while [ $# -ne 0 ]
	do
	if [ -f $dir/$1 ];then  
	  echo "detail profile of $1:"
	  cat $dir/$1
	else
	  echo "$1 : no such profile "
	fi
	  shift
	done
  else
    ls -w 1 $dir
  fi
  exit 0
}

function apply_interface
{
  if [ ! -f "$dir/$1" ]; then
	echo "profile $1 not exists!"
	exit 69
  fi
  inet=`head -n 1 "$dir/$1" | awk '{print $2}'`
  #for debian distros
  grep -i 'ubuntu' /proc/version
  ret1=$?
  grep -i 'debian' /proc/version
  ret2=$?
  if [ $ret1 -eq 0 ] || [ $ret2 -eq 0 ];then
	#first,offline interface
	ifdown $inet 2>/dev/null
	ifup -i "$dir/$1" $inet
  else
	#LSB debian
	#is dhcp?
	grep 'inet dhcp' "$dir/$1"
	if [ $? -eq 0 ];then
	  ifconfig $inet down
	  killall dhclient
	  dhclient $inet
	else
	  #static ip
	  ipaddress=`sed -n '/address/p' xinfang  |cut -d' ' -f2`
	  netmask=`sed -n '/netmask/p' xinfang  |cut -d' ' -f2`
	  gateway=`sed -n '/gateway/p' xinfang  |cut -d' ' -f2`
	  ifconfig $inet down
	  ifconfig $inet $ipaddress netmask $netmask up
	  route add default gw $gateway dev $inet
	fi
  fi
  #SuSe
  exit 0
}

if [ $UID -ne 0 ];then
echo "this utility required root privilege"
exit 65
fi
[ -d $dir ] || mkdir $dir

if [ ! -f $file ];then
  echo "$file not exists! exit"; 
  exit 66
fi

[ $# -eq 0 ] && usage

TEMP=`getopt -o ap:d:l:: --long add,apply,del:,list:: -n $0 -- "$@"`

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

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
	case "$1" in
		-a|--add) add_interface; break;;
		-p|--apply) shift;apply_interface  $1; break;;
		-d|--del) shift; del_interface $1; break;;
		-l|--list)  shift 3; list_interface $*; break;;	  
		*) usage ;;
	esac
done
 
  


Reply

Tags
ip, manager, switch


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bypassing Profile manager in Mozilla Kilahchris Linux - Software 1 10-16-2004 02:27 AM
Mozilla 1.4 and profile manager linux_pioneer Linux - Software 5 07-22-2004 01:28 PM
Profile Manager raysr Linux - Newbie 8 06-04-2004 11:26 PM
mozilla profile manager HE|| again! itsjustme Linux - General 3 02-07-2004 09:15 AM
Profile Manager for Mozilla 1.2 Qebex Linux - Software 2 09-22-2002 03:41 AM


All times are GMT -5. The time now is 01:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration