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….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
auto eth0:0 iface eth0:0 inet static address 192.168.1.3 netmask 255.255.255.0 auto eth0:1 iface eth0:1 inet static address 192.168.1.4 netmask 255.255.255.0 ... ... ... auto eth0:123 iface eth0:123 inet static address 192.168.1.126 netmask 255.255.255.0 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $x = 3; # first 4th octet of the ip $y = 126; # last 4th octent of the ip $z = 0; # the first eth0:# you wish to use, normally 0 $ip = ‘192.168.1.’; # first 3 octets of IP address to use (dont forget the last period ".") $nm = ‘255.255.255.0’; # Netmask to use while($x <= $y) { echo "auto eth0:$z<br/>" ; echo "iface eth0:$z inet static<br/>"; echo " address $ip$x<br/>"; echo " netmask $nm<br/>"; echo "<br/>"; $x++; $z++; } ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
#!/bin/bash # Name: Virtual Ethernet Device IP Generator # # Description: This script is used to generate proper entried for the /etc/network/interfaces file # creating virtual devices like eth0:0. It promts you for all the required information before outputing # to which every file you wish. # # This file must be named debian-add-ips.sh to work properly. # # Author: Matthew Koster clear echo -e 'E[1;31;40m'"===================================================================" echo -e 'E[1;31;40m'"= =" echo -e 'E[1;31;40m'"= E[1;37;40mThis applicaiton is used to generate virtual ethernet devices E[1;31;40m=" echo -e 'E[1;31;40m'"= E[1;37;40mon Debian based system. These sytems have entries added into E[1;31;40m=" echo -e 'E[1;31;40m'"= E[1;37;40mthe E[1;36;40m/etc/network/interfaces E[1;37;40mfile. This script will add the E[1;31;40m=" echo -e 'E[1;31;40m'"= E[1;37;40mappropriate lines tot he file for you, making it easier to E[1;31;40m=" echo -e 'E[1;31;40m'"= E[1;37;40mmultiple virtual interfaces all at once. E[1;31;40m=" echo -e 'E[1;31;40m'"= E[1;37;40mTo exit, use ctrl+c E[1;31;40m=" echo -e 'E[1;31;40m'"= =" echo -e 'E[1;31;40m'"= E[1;37;40mCreated by: E[1;33;40mMatthew Koster E[1;31;40m=" echo -e 'E[1;31;40m'"= =" echo -e 'E[1;31;40m'"==================================================================="; tput sgr0 echo " " echo " " # Ask for File location echo -e 'E[1;33;40m'"Enter the file you wish to insert the ips into, followed by [ENTER]"; tput sgr0 read -e -p "File: " -i "/etc/network/interfaces" file echo "" # Ask for virtual Device name (eth0, em0, etc) echo -e 'E[1;33;40m'"Enter the device you wish to add IPs to (ex: eth0), followed by [ENTER]"; tput sgr0 read -p "Device: " netdevice echo " " # ASk for staring numeber (eth0:0) echo -e 'E[1;33;40m'"Enter the first virtual number# (normally 0), followed by [ENTER]"; tput sgr0 read -p $netdevice":" start echo " " while [[ ! "$start" =~ ^[0-9]+$ ]] do echo -e 'E[1;31;40m'"!!ERROR!!"; tput sgr0 echo -e 'E[1;33;40m'"This value has to be a number"; tput sgr0 read -p $netdevice":" start echo "" done # Ask for the first 3 octets of the IP Address (ex 192.168.1) echo -e 'E[1;33;40m'"Enter the first 3 octets of an the IP Address, folloed by [ENTER]"; tput sgr0 echo -e 'E[1;32;40m'"Example, 192.168.1 (no trailing .)"; tput sgr0 read -p "Partial IP Address: " ipaddr echo " " # Ask for the last octet of first IP address in sequence echo -e 'E[1;33;40m'"Enter the last octent of first IP address, followed by [ENTER]:"; tput sgr0 echo -e 'E[1;32;40m'"Example, for 192.168.1.2 you would enter 2"; tput sgr0 read -p "Start Octet: " first echo " " while [[ ! "$first" =~ ^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$ ]] do echo -e 'E[1;31;40m'"!!ERROR!!"; tput sgr0 echo -e 'E[1;33;40m'"The value has to be a number between 1 and 254"; tput sgr0 read -p "Start Octet: " first echo "" done # Ask for the last octet of the last IP address in sequence echo -e 'E[1;33;40m'"Enter the last octent of last IP address, followed by [ENTER]:"; tput sgr0 echo -e 'E[1;32;40m'"Example, for 192.168.1.254 you would enter 254"; tput sgr0 read -p "End Octet: " last echo " " while [[ ! "$last" =~ ^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$ ]] do echo -e 'E[1;31;40m'"!!ERROR!!"; tput sgr0 echo -e 'E[1;33;40m'"The last IP address has to be a number between 1 and 254"; tput sgr0 read -p "End Octet: " last echo "" done # If the last IP address is lower than the first, issue and error while [ "$last" -le "$first" ] do echo -e 'E[1;31;40m'"!!ERROR!!"; tput sgr0 echo -e 'E[1;33;40m'"The last IP address has to be greater than the first..."; tput sgr0 read -p "End Octet: " last echo "" while [[ ! "$last" =~ ^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]] do echo -e 'E[1;31;40m'"!!ERROR!!"; tput sgr0 echo -e 'E[1;33;40m'"The last IP address has to be a number between 0 and 255"; tput sgr0 read -p "End Octet: " last echo "" done done # Ask for the last octet of the netmask echo -e 'E[1;33;40m'"Enter the last octent of the Netmask, followerd by [ENTER]"; tput sgr0 echo -e 'E[1;32;40m'"Example, for 255.255.255.240, you would use 240"; tput sgr0 read -p "Netmask: " netmask echo " " while [[ ! "$netmask" =~ ^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$ ]] do echo -e 'E[1;31;40m'"!!ERROR!!"; tput sgr0 echo -e 'E[1;33;40m'"The last IP address has to be a number"; tput sgr0 read -p "End Octet: " netmask echo "" done # Verifiy info with user, if incorrect, restart script clear echo -e 'E[1;31;40m'"=========================================="; tput sgr0 echo -e 'E[1;31;40m'"= PLEASE REVIEW THE FOLLOING CAREFULLY ="; tput sgr0 echo -e 'E[1;31;40m'"=========================================="; tput sgr0 echo "" echo -e 'E[1;33;40m'"Here is the info you entered:"; tput sgr0 echo -e 'E[1;33;40m'"File to be modified: c"; tput sgr0 echo $file echo -e 'E[1;33;40m'"First Virtual interface: c"; tput sgr0 echo $netdevice":"$start echo -e 'E[1;33;40m'"IP Range: c"; tput sgr0 echo $ipaddr"."$first "-" $ipaddr"."$last echo -e 'E[1;33;40m'"Netmask: c"; tput sgr0 echo "255.255.255."$netmask echo " " echo -e 'E[1;33;40m'"Is this correct? [Y/N]: c"; tput sgr0 read corr # User must enter Y or y to continue, otherwise script restarts if [ "$corr" != y ]; then if [ "$corr" != Y ]; then clear ./debian-add-ips.sh fi fi # Back up the file if it exists, we all make mistakes. if [ -f "$file" ]; then cp $file $file.bak bkup=1 fi # Loop the output for as many IPs as requsted x=$start y=$first z=$last while [ "$y" -le "$z" ] do echo "" >> $file echo "auto " $netdevice:$x >> $file echo "iface "$netdevice:$x" inet static" >> $file echo " address "$ipaddr"."$y >> $file echo " netmask 255.255.255."$netmask >> $file y=$[$y+1] x=$[$x+1] done clear # Let them know where the back up is (if they have one) and cat the file for inspction echo -e 'E[1;33;40m'"All done - Press enter to CAT the file for final inspection"; tput sgr0 if [ "$bkup" == 1 ]; then echo -e 'E[1;33;40m'"Note - A back up for the file has been created as: c"; tput sgr0 echo $file".bak" fi read -p "Press any key to continue... " -n1 -s clear echo " " echo -e 'E[1;33;40m'"Out put of file c"; tput sgr0 echo $file echo "" echo -e "E[1;31;40m============================================================================================"; tput sgr0 echo "" cat $file echo "" echo -e "E[1;31;40m============================================================================================"; tput sgr0 echo "" echo "Virtual Ethernet IP Generation complete." echo "" |
Now again, its a rough script… Definitely can be improved. But it gets the job done.