I was recently tasked with adding 100+ ips to server at work. Normally we deal with smaller networks, like /28 or /29, but this guy had a /25 which is 128 IPs (125 usable) (Side Note: before you argue and tell me it should be 126 Usable, In our line of work, there are 3 unusable IPs instead of 2… Normally you have Network (first IP in range) and Broadcast (last IP in range), in our line, we add a Gateway (Second IP in range) which is not usable by a server) SO taking away all the unusable IPs, and of course the primary IP assigned to the NIC card, that results in 124 IPs needing to be assigned.

Now in CentOS system, this is easy, you simply edit the ifcfg-eth0-range0 file, add the first IP, last IP and clonenum start value, and voila, CentOS does the rest. Debian tho…. Not so easy… You have to add each device separately. Can you imagine adding each entry… Or copying and pasting and editing every entry? Trust me, its a waste of time….

So I decided to make myself a little script. It started off as a simple PHP script which I used on my WAMP installation on my work laptop. If you have a WAMP setup on your work machine, you can use it (copy the next bit of code) and just change the variables. It outputs the code you see above without having to manually type in each line. Just copy and paste.

I then realized that not everyone has a WAMP installation, so how better to progress the script than to make it a Bash script which will enable you to run the script right on the server you are adding the IPs to.

Its a rough script, not real validation (use caution) but it does make a backup of the original file, in case you don’t heed my warning. It also allows you to change the file you want to modify, it defaults to the actual path of the interfaces file for Debian, but you can change to say /home/user/text.txt if you are afraid or making a mistake… then you can copy and past it in using your favourite editor (vim is mine, F-U Nano/Pico).

[UPDATE] I updated the file to have some validation, so people are not adding letter values or extremely large values to IP ranges. [/UPDATE]
Here is the code for the bash script:
Note: make sure to either name it “debian-add-ips.sh” or change line 138 to be the files name.

Now again, its a rough script… Definitely can be improved. But it gets the job done.