I am a bit rusty on my scripting abilities and am trying to find a good starting point for creating a script that creates files (generating new file names) and edits the IP address in the file by incrementing it by +1My scenario is like so - We have about 50 IP addresses to add to our network-scripts each with an incrementing IP. I could do this by hand, however I thought this would be a good time to try and get up to speed on my scripting skills.The naming scheme is like so:ifcfg-eth0:1, ifcfg-eth0:2, ifcfg-eth0:3 ...*n*The content of these files is:DEVICE=eth0:1NETMASK=255.255.255.0IPADDR=10.2.7.148BOOTPROTO=noneONBOOT=yesDNS1=10.2.53.150PEERDNS=yesDNS2=10.2.53.250GATEWAY=10.2.7.1TYPE=EthernetUSERCTL=noIPV6INIT=noWhere IPADDR=10.2.7.148 should be increased by +1 in each file. For example:ifcfg-eth0:2 file would be the exact same except that the IPADDR would be 10.2.7.149 and so on.I am fairly certain I should invoke sed to find and replace the IP addresses. For instance, searching for an IP address using sed could be accomplished like so:sed -rn '/(IPPADDR=)((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/p' fileWhat steps need to be taken to generate a new file with the eth0:N name incremented by +1 as well as the IP address in the file.Thanks in advance!EDIT:Let me clarify the names of the files should not contain the IP address.I am needing file1 to be copied over to file2 eth0:1 -> eth0:2 with same file contents as above, except the fact that the IPADDR= field on the generated file should be incremented by one. Note: The file name should also be incremented by +1 IE. cp ifcfg-eth0:1 ifcfg-eth0:2
Generate networking configuration files using sed and bash
bash;shell script;networking;sed
As far as I'm aware of sed cannot do calculations on numbers, I would use awk$ awk -F'.' '$1~IPADDR{$NF++;OFS=.}1' fileDEVICE=eth0:1NETMASK=255.255.255.0IPADDR=10.2.7.149BOOTPROTO=noneONBOOT=yesDNS1=10.2.53.150PEERDNS=yesDNS2=10.2.53.250GATEWAY=10.2.7.1TYPE=EthernetUSERCTL=noIPV6INIT=noInclude this into a loop of files to be changed.Explanation:-F'.': use . a field separator$1~IPADDR{}: if first field contains IPADDR pattern run what is inside{}$NF++;OFS=.: increment number in last field (NF denotes number of fields in the record).1: print everything