LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2012, 07:00 AM   #1
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 130

Rep: Reputation: 20
Question creating VLAN setup script - help needed


hello

i am trying to create a script that will setup 3 VLANs
i have tried to create a script (below) it fails miserably.

if somebody could provide me with suggestion on how to correct it ..


thanks,


Code:
#! /bin/bash

#
# SETTING UP CONNECTION TO DEVELOPMENT ENVIRONMENT

export ADAPTER=$1

#
# if there is no input - set it to the default 'eth0'
if[[ $ADAPTER ]] && export ADAPTER=eth0


# set VLAN 21
if [[ ! $( ifconfig $ADAPTER.21 | grep $ADAPTER.21 ) ]] 
then 
	vconfig add $ADAPTER 21
fi

sh $(ip addr add 192.168.21.19/24 dev $ADAPTER.21)


# set VLAN 201
if [[ ! $( ifconfig $ADAPTER.201 | grep $ADAPTER.201 ) ]] 
then
	vconfig add $ADAPTER 201
fi
sh $(ip addr add 192.168.201.19/24 dev $ADAPTER.201)


# set VLAN 202
if [[ ! $( ifconfig $ADAPTER.202 | grep $ADAPTER.202 ) ]] 
then
	vconfig add $ADAPTER 202
fi

sh $(ip addr add 192.168.202.19/24 dev $ADAPTER.202)
 
Old 09-16-2012, 08:38 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
It would help a lot if you could be a bit more specific as to what isn't working, and what error messages you're getting, if any.

I can see at least two problems with your script:
  1. You're running ip by spawning instances of sh (why?) that won't return control to your script
  2. Your adding VLANs with vconfig without checking whether VLANs are supported by the kernel (you may have to load the 802.1q VLAN module with modprobe 8021q)
 
Old 09-16-2012, 10:09 AM   #3
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 130

Original Poster
Rep: Reputation: 20
thanks

since i running this script on installation that i control.
i did not think about testing for the module.

but i do not understand what you meant by 'running ip by spawning instances of sh (why?) that won't return control to your script'

- what would be your suggestion to do this properly??
 
Old 09-16-2012, 11:25 AM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
Quote:
Originally Posted by nicolasdiogo View Post
but i do not understand what you meant by 'running ip by spawning instances of sh (why?) that won't return control to your script'

- what would be your suggestion to do this properly??
Well, you're doing this:
Code:
sh $(ip addr add 192.168.21.19/24 dev $ADAPTER.21)
That starts a new instance of the shell (sh) and runs the ip command. Unfortunately, the new shell instance will stick around until someone types "exit".

Instead, you could simply do this:
Code:
ip addr add 192.168.21.19/24 dev $ADAPTER.21
 
Old 09-16-2012, 11:53 AM   #5
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 130

Original Poster
Rep: Reputation: 20
thanks a lot for that!!

i was getting these 'hangs' without understanding why; and now i know

another problem that i find if the check to see the interface already exists.
i try

Code:
if [ ! -z "$( ifconfig $ADAPTER.21 | grep -i "link" )" ] ;
then
    vconfig add $ADAPTER 21
    ip addr add 192.168.21.19/24 dev $ADAPTER.21
fi
which works find when there is an interface running - but it fails first time as the interface is not valid and 'ifconfig' issues and error message.

any suggestion on that?

thanks,

Last edited by nicolasdiogo; 09-16-2012 at 11:54 AM.
 
Old 09-16-2012, 12:56 PM   #6
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
Quote:
Originally Posted by nicolasdiogo View Post
another problem that i find if the check to see the interface already exists.
i try

Code:
if [ ! -z "$( ifconfig $ADAPTER.21 | grep -i "link" )" ] ;
then
    vconfig add $ADAPTER 21
    ip addr add 192.168.21.19/24 dev $ADAPTER.21
fi
which works find when there is an interface running - but it fails first time as the interface is not valid and 'ifconfig' issues and error message.
If you're thinking of the ifconfig command in the if test, there's really nothing really wrong about that. You're checking to see if the interface exists, and if it doesn't, ifconfig will complain. You can redirect stderr to /dev/null to hide the error message:
Code:
if [ ! -z "$( ifconfig $ADAPTER.21 2>/dev/null | grep -i "link" )" ] ;
 
Old 09-25-2012, 03:37 PM   #7
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 130

Original Poster
Rep: Reputation: 20
thanks Ser Olmy for helping me out with this script

and for also teaching me a couple of things along the way.

with regards,
 
  


Reply



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
Network Hardware - Switch needed with gigabit speed and VLAN in UK nicolasdiogo Linux - Networking 4 05-21-2012 10:24 AM
Creating simple VLAN between two linux machines mrtoph12 Linux - Networking 2 02-04-2012 04:47 PM
Bash script needed for creating csv matrix out of separate txt files ligtvoet Other *NIX 2 10-14-2011 06:24 PM
How to setup VLAN? your_shadow03 Linux - Newbie 1 12-25-2009 01:10 AM
VLAN Setup with RHEL3 jmouzon Linux - Networking 1 08-07-2006 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration