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. Thanks for readin' joe |
could you maybe give us something easier to read.
For example you might give us how the script would run: Code:
Input your network address: 192.168.10.0/24 |
As per previous post, if you supply examples of your inputs and outputs it makes it much easier to assist.
The idea sounds fairly trivial (based on current information), so here is a link to help with learning scripting. |
Basically I'm looking for this on a superficial level;
Please enter the name of the container to add IPs:ctxxxx Please enter the first usable IP in the range:192.168.10.50 Please enter the subnet:/29 Then it would use the vzctl set ctxxxx --ipadd 192.168.10.50 --save (5 times, incrementing that last octet each time.) Thanks for responding. I'll read over your links. They are much appreciated. joe |
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 |
Initial suggestions are as follows:
1. You do not seem to check any of the user input 2. Are you never allowing more than 2 subnets? If we assume more, you can remove the ifs altogether: Code:
(( END = OCT4 + ( 2 ** (32 - ${SUBNET#/} )))) 4. An alternative to your echo and cut routine: Code:
set - ${IP//./ } 5. You can also use bash builtin substitution: Code:
vzctl set $CT --ipadd ${IP%$4}$IPFULL --save Hope some of this helps. |
I know it's been ages since I originally posted this but I did find your response very helpfull. Thanks for the advise.
|
great
nice thread. thanks for sharing that information. it was very informative.
|
Here's a simpler way to chop up the address string:
Code:
$ address=192.168.10.0/24 string manipulations in bash |
Good point. If you're going to change the field separator, this would be simpler still.
Code:
IFS='.' |
Actually, whilst simpler you have missed the subnet option at the end which is after '/' hence your last array element would be something like:
Code:
echo ${IP[3]] |
ohh, touche
|
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. Code:
IFS='./' read -r oct1 oct2 oct3 oct4 subnet <<<"$address" |
All times are GMT -5. The time now is 05:02 PM. |