ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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:
You're running ip by spawning instances of sh (why?) that won't return control to your script
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)
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".
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.