LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   creating VLAN setup script - help needed (https://www.linuxquestions.org/questions/programming-9/creating-vlan-setup-script-help-needed-4175427494/)

nicolasdiogo 09-16-2012 07:00 AM

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)


Ser Olmy 09-16-2012 08:38 AM

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)

nicolasdiogo 09-16-2012 10:09 AM

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??

Ser Olmy 09-16-2012 11:25 AM

Quote:

Originally Posted by nicolasdiogo (Post 4781648)
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

nicolasdiogo 09-16-2012 11:53 AM

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,

Ser Olmy 09-16-2012 12:56 PM

Quote:

Originally Posted by nicolasdiogo (Post 4781694)
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" )" ] ;

nicolasdiogo 09-25-2012 03:37 PM

thanks Ser Olmy for helping me out with this script

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

with regards,


All times are GMT -5. The time now is 04:19 AM.