[SOLVED] Shell script to increment IP variables into ranges using /29 /28 /26 etc?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Shell script to increment IP variables into ranges using /29 /28 /26 etc?
So, I've been a member of this forum for a while and, as the header pointed out when I logged in today, I've never actually asked a questions. Usually I just scrape through the forums and google until I find someone who has already asked the question I have and piece together my own answers but I can't seem to make that happen this time so here goes...
I am trying to write a shell script that I thought would be relatively simple but isn't really coming together.
What I need is a script that adds an IP in what I assume would be to variables based on the "read -e" input and then a third variable thats the subnet shorthand (/29 or whatever also from read -e). Once thats in I'd like to use what I assume would be a while loop to not only increment the last octet of the IP but also run the command to add that IP to a vps the number of usable IP's in the subnet. So increment the last octet 5 times and run the command 5 times so I can just add a first IP and give its range. Doable? Any help or suggestions or even just pointing me toward a pertinent tut/tuts would be much appreciated.
Here's what I came up with. Seems pretty dirty so if anyone has any best practice tips or whatever to help clean it up I'd welcome them gladly. Virtuozzo doesn't have a shell command to add IP ranges to VPS's so if anyone else uses it, here ya go.
joe
Code:
#!/bin/bash
echo -n "Please enter container name(ie ctxxxxx): "
read CT
echo -n "Please enter first IP in range: "
read IP
echo -n "Please enter subnet(ie /29): "
read SUBNET
OCT1=$(echo $IP | cut -d "." -f1)
OCT2=$(echo $IP | cut -d "." -f2)
OCT3=$(echo $IP | cut -d "." -f3)
OCT4=$(echo $IP | cut -d "." -f4)
if [ "$SUBNET" == "/29" ]
then
END=$(($OCT4 + 4))
for IPFULL in `seq $OCT4 $END`
do
vzctl set $CT --ipadd $OCT1.$OCT2.$OCT3.$IPFULL --save
done
elif [ "$SUBNET" == "/28" ]
then
END=$(($OCT4 + 12))
for IPFULL in `seq $OCT4 $END`
do
vzctl set $CT --ipadd $OCT1.$OCT2.$OCT3.$IPFULL --save
done
fi
Last edited by joecam1673; 08-19-2011 at 05:18 PM.
3. The above leads me to question how you were calculating the END??
4. An alternative to your echo and cut routine:
Code:
set - ${IP//./ }
The above allows you to now refer to each segment using the positional parameter numbers, ie OCT1 = $1 (This of course assumes that IP has been checked and is a correct address format)
I would still maintain that using read is the cleaner option, since you don't have to modify IFS globally. It's generally a good idea to avoid doing that, since it usually means you also have to remember to change it back afterwards, and forgetting to do so can lead to unpleasant and difficult to diagnose side-effects.
It's also probably slightly faster, as only a single command is executed. Finally, as I mentioned, you can replace the array with individual variables, if it suits the script flow better.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.